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

Author Topic: Multiple Object generation  (Read 5045 times)

0 Members and 1 Guest are viewing this topic.

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Multiple Object generation
« on: September 03, 2012, 12:35:43 am »
Hey, I'm new to OOP, and I want to make multiple asteroid sprites. So how would I do that. Would using a class be the best way? You would still have to type all the names for each object. Could it be done in a for loop? but then how would each have its own name?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10837
    • View Profile
    • development blog
    • Email
Re: Multiple Object generation
« Reply #1 on: September 03, 2012, 12:55:28 am »
There are many ways to achive such a thing.
The idea behind a nice code design is, to have a good abstraction level. For example you could implement a class and then go and manually instanciate 100 diffrent objects where the variable name is the name of each astroid, this is quite a bad design. On the other hand you could go and let the class have a name property, then you could go and instanciate the classes in a loop and set the names, again hard coded in your source. This gives a better abstraction, but isn't that nice either. You could then export the names into a seperate file and parse it on the fly and add the name on instantiation to the object.
If the name is actually not a property of the astroid itself, i.e. it's just to call the diffrent ones, then you could use a std::map (or std::unordered_map) and add the name as key and the class as value.

Then again, you can pick any possibility as long as it fits your needs. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Multiple Object generation
« Reply #2 on: September 03, 2012, 01:13:11 am »
Ok, the asteroid's name won't be a property, just something to instantiate each of them with, but if I use a map, will I still have to name each one?
Would it be possible to concatenate a number to the end of a string in a loop and then make an object every iteration?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10837
    • View Profile
    • development blog
    • Email
Re: Multiple Object generation
« Reply #3 on: September 03, 2012, 01:19:55 am »
Ok, the asteroid's name won't be a property, just something to instantiate each of them with, but if I use a map, will I still have to name each one?
Would it be possible to concatenate a number to the end of a string in a loop and then make an object every iteration?
If you just want to number them, then use a std::vector which is similar to an array and uses integers as index.
Although it highly depends on your use case. If you have to insert/remove items at random positions (in the vector) then you maybe want to use std::list, but if you have to access items are random possition very often or other reasons, then std::vector is quite good.

You should probably take again a closer look at your C++ book and its section on the STL containers. If it doesn't have such a section, get a better C++ book. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Multiple Object generation
« Reply #4 on: September 03, 2012, 02:24:03 am »
Oh ok, that's what I was thinking, I just haven't used vectors in a while. Thanks!
But how do I delete a sprite? In my game, when you crash into an asteroid, your ship goes away.

Also, how would I add to my vector?
        sf::Image asteroid;
        asteroid.LoadFromFile("ball.png");

        std::vector<sf::Sprite> asteroidV;
        for(int i=0; i<5; i++)
        {
                asteroidV.push_back(
        }
 

What would I put in the push_back argument? The argument makes a copy of whatever I give it, so do I put an sf::Sprite?

Edit: Never mind, I got it to work
        sf::Image asteroid;
        asteroid.LoadFromFile("ball.png");

       

        sf::Sprite spriteAsteroid;
        spriteAsteroid.SetImage(asteroid);
        spriteAsteroid.SetPosition((float)sf::Randomizer::Random(50, 780), (float)sf::Randomizer::Random(-5, -50));
        spriteAsteroid.SetCenter(spriteAsteroid.GetSize().x/2, spriteAsteroid.GetSize().y/2);

        std::vector<sf::Sprite> asteroidV;
        for(int i=0; i<5; i++)
        {
                asteroidV.push_back(spriteAsteroid);
        }

But still, how do i delete a sprite?
« Last Edit: September 03, 2012, 02:40:26 am by rasen58 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10837
    • View Profile
    • development blog
    • Email
Re: Multiple Object generation
« Reply #5 on: September 03, 2012, 10:35:48 am »
I very strongly advice you against the use of SFML 1.6, because it's over 2 years old and there have been many new features in SFML 2. Also there are already binaries for the RC release on the download page, so you won't need to compile SFML 2 yourself. ;)

As for removing an element from a vector, you could've looked it up in your C++ book I was refering to earlier or in an online reference. ;)
But as I said before if you have to remove elements at random position, then maybe you should use a std::list, because removing elements in a vector, leads to copying the whole vector which is quite an expensive task.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Multiple Object generation
« Reply #6 on: September 04, 2012, 12:07:25 am »
No, but my ship isn't in a vector, it is just a sprite made in my main.cpp. So how would I delete that?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10837
    • View Profile
    • development blog
    • Email
Re: Multiple Object generation
« Reply #7 on: September 04, 2012, 12:26:16 am »
No, but my ship isn't in a vector, it is just a sprite made in my main.cpp. So how would I delete that?
Then don't keep a copy of the sprite in the main function, e.g.
        std::vector<sf::Sprite> asteroidV;
        for(int i=0; i<5; i++)
        {
                asteroidV.push_back(sf::Sprite());
                asteroidV.back().SetImage(asteroid);
                asteroidV.back().SetPosition((float)sf::Randomizer::Random(50, 780), (float)sf::Randomizer::Random(-5, -50));
                asteroidV.back().SetCenter(spriteAsteroid.GetSize().x/2, spriteAsteroid.GetSize().y/2);
        }
And then you can simply call eares e.g.:
asteroidV.erase(asteroidV.begin()+5);
« Last Edit: September 04, 2012, 12:28:27 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Multiple Object generation
« Reply #8 on: September 06, 2012, 05:55:16 am »
A hint if you're going to use names for sprites and that's to have some kind of setup like this.

ClassName + IDTag

Simply for the default setup you'd have the class's name alongside a randomly generated ID tag added onto it. Only use unique names for things when it comes to something like player names or something in which case the format would look like this.

PlayerName + IDTag.

The IDTag is still there for the event you need it for something and also helps when it comes to a hard to pull off at first but very useful form of collision system when you've got multiple sides and/or players in game.
I have many ideas but need the help of others to find way to make use of them.

rasen58

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Multiple Object generation
« Reply #9 on: September 09, 2012, 04:39:09 pm »
Oh ok thanks. That helped a lot!
But how would I delete my SHIP sprite without making that into a vector?
« Last Edit: September 09, 2012, 04:46:59 pm by rasen58 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10837
    • View Profile
    • development blog
    • Email
Re: Multiple Object generation
« Reply #10 on: September 09, 2012, 04:53:59 pm »
But how would I delete my SHIP sprite without making that into a vector?
I'm not qutie sure what you mean by 'delete', if it shouldn't be displayed, just don't draw it. ;)

For example:
bool DrawMySprite = true;
// ...
DrawMySprite = false; // "delete" sprite
// ...
if(DrawMySprite)
    window.draw(MySprite);

If you mean to get rid of the variable itself; that's not how things work in C++. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/