Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Making and handling entities dynamically  (Read 1533 times)

0 Members and 1 Guest are viewing this topic.

somerandompiggo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Making and handling entities dynamically
« 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!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Making and handling entities dynamically
« Reply #1 on: February 17, 2021, 09:08:09 am »
You probably want to wrap that button in a class, then your factor 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

somerandompiggo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Making and handling entities dynamically
« Reply #2 on: February 17, 2021, 11:01:18 am »
Thank you! I never really understood why variables weren't passed over between functions.