SFML community forums

Help => General => Topic started by: somerandompiggo on February 16, 2021, 08:06:37 pm

Title: Making and handling entities dynamically
Post by: somerandompiggo on February 16, 2021, 08:06:37 pm
I'm not really sure how to word the question, so I can't really get any answers from Google, but it goes like this.

For a screen-capture program I'm working on, I'm making a custom UI library for it written in C++/SFML. I want to have a function that creates a RectangleShape and Text entities for each button, but I'm not sure how to change the name so it comes from the function call, an example would be makeButton("buttonname", 25, 10);

I don't really have any code since I have no way to implement it, but here is kind of what I want.

int makeButton(string name, int x, int y) {
        sf::RectangleShape button;
        sf::Text buttonText;
        button.setSize(sf::Vector2f(25.f, 5.f));
        button.setFillColor(sf::Color::Blue);
        // other code here
}
 

Thanks!
Title: Re: Making and handling entities dynamically
Post by: eXpl0it3r on February 17, 2021, 09:08:09 am
You probably want to wrap that button in a class, then your factor method (https://refactoring.guru/design-patterns/factory-method) (i.e. makeButton) can return a new instance of a button, which you then may want to store in a std::vector so you can have an arbitrary amount of buttons.
Title: Re: Making and handling entities dynamically
Post by: somerandompiggo on February 17, 2021, 11:01:18 am
Thank you! I never really understood why variables weren't passed over between functions.