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

Author Topic: [Solved] Editing multiple Objects with a function  (Read 1523 times)

0 Members and 1 Guest are viewing this topic.

tobcode

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] Editing multiple Objects with a function
« 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?
« Last Edit: October 16, 2023, 10:29:40 am by eXpl0it3r »

Hapax

  • Hero Member
  • *****
  • Posts: 3383
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Solved] Editing multiple Objects with a function
« Reply #1 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);
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11039
    • View Profile
    • development blog
    • Email
Re: [Solved] Editing multiple Objects with a function
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tobcode

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: [Solved] Editing multiple Objects with a function
« Reply #3 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3383
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [Solved] Editing multiple Objects with a function
« Reply #4 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 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 if you definitely don't need to change its size.


(click to show/hide)
« Last Edit: October 16, 2023, 07:36:40 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything