SFML community forums

Help => Graphics => Topic started by: tobcode on October 12, 2023, 09:20:52 pm

Title: [Solved] Editing multiple Objects with a function
Post by: tobcode on October 12, 2023, 09:20:52 pm
Hello everyone,

i want to set Properties of multiple objects in an function outside of main().
The problem is that if the objects are created in main(), then it is not possible to send them to the function as a parameter.

Example:


int main(){
sf::RectangleShape test[50];
initialize(test);
}


void initialize(sf::RectangleShape test[]){
  for(int j = 0; j<50; j++)
  {
    test[j].setOutlineColor(something);
  }
}



my only solution to this Problem is to make the objects global. when they are defined outside of main it works of course, but that is not what i want to do.
It is possible to pass am object by reference into a function.
But how about an array of objects?

i know the example Code doesn't run but how do i pass an array of RectangleShapes into a function?
Title: Re: [Solved] Editing multiple Objects with a function
Post by: Hapax on October 13, 2023, 08:53:00 pm
You are passing them to the function by value i.e. it makes a copy of it and uses that locally.

You need to pass them by reference or by pointer.

An sf::RectangleShape passed by reference (&):
void setRectangleOutlineColorToYellow(sf::RectangleShape& rectangle)
{
    rectangle.setOutlineColor(sf::Color::Yellow);
}

An sf::RectangleShape passed by pointer (*):
void setRectangleOutlineColorToGreen(sf::RectangleShape* rectangle)
{
    rectangle->setOutlineColor(sf::Color::Green);
}
Title: Re: [Solved] Editing multiple Objects with a function
Post by: eXpl0it3r on October 16, 2023, 10:30:48 am
Additionally, you can wrap everything into a class, so you can share objects within the class scope and don't need to have global objects.
Title: Re: [Solved] Editing multiple Objects with a function
Post by: tobcode on October 16, 2023, 05:06:26 pm
Additionally, you can wrap everything into a class, so you can share objects within the class scope and don't need to have global objects.

How would such a class look like? Sorry if this is a dumb question, but i have never worked with classes. Up until now i was able to get it working by global variables, however this is not the cleanest approach.
Title: Re: [Solved] Editing multiple Objects with a function
Post by: Hapax on October 16, 2023, 07:34:58 pm
A simple version would be something like this:
struct Everything
{
    std::vector<sf::CircleShape> circles(50); // 50 circles in a vector
    std::size_t numberOfRectangles{ 50u }; // we need a way of knowing how many rectangles are in this type of array
    sf::RectangleShape rectangles[50]; // 50 rectangles in an array
}

void setAllCircleOutlines(Everything& everything)
{
    for (auto& circle : everything.circles)
        circle.setOutlineColor(sf::Color::Green);
}

void setAllRectangleOutlines(Everything& everything)
{
    for (std::size_t i{ 0u }; i < everything.numberOfRectangles; ++i)
        everything.rectangles[i].setOutlineColor(sf::Color::Yellow);
}

int main()
{
    Everything everything; // this is where all of the things are actually created and stored (they are not global).

    setAllCircleOutlines(everything);
    setAllRectangleOutline(everything);
}

As an aside, you may want to consider using an std::vector (https://en.cppreference.com/w/cpp/container/vector) instead of a raw (C-style) array. You can see how simpler, neater and clearer the circle code looks. You could also use an std::array (https://en.cppreference.com/w/cpp/container/array) if you definitely don't need to change its size.


(click to show/hide)