Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

thmleo

storing my objects

This topic is 6769 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Use pointers for the values that should
be shared by all objects, and point those
at the same variable. Then when you change
that variable, it will change in all object.

Weehee, this is the 1000 post (unless some
other guy posted while I wrote this), don't
I get price of something

Greets Tobias

Share this post


Link to post
Share on other sites
Advertisement
If you're using C++ you should take a look at virtual functions. You can create a generic class like SPRITES or whatever. Then you can create classes(eg. BIG_BAD_GUY, BULLET, whatever) that inherit the base class (SPRITES) and override any virtual functions that you need to for that particular class.

Then you create a linked list or array of SPRITE pointers , NEW any of the classes that are derived from the base class, and you can call the virtual functions. C++ takes care of figuring out which function should be called.

sprite[0] = new Bad_Guy;
sprite[1] = new Player_sprite;
sprite[2] = new Heat_Seeking_Missle;

for(i=0; i<3; i++)
{
sprite[i]->AI_routine(); // actually calls different functions
}


Virtual functions are one of those things that seemed too abstract to have a practical use when I started with C++ but now I wouldn't trade them for the world.

Good luck.

Share this post


Link to post
Share on other sites
Hi all,

Say I have a program with a bunch of different enemies of different kinds. What would be the best way to store them so that I can easily blit and do calculations for each one without manually doing everything for every single one?

Share this post


Link to post
Share on other sites
I recommend using data to define the behaviour of entities rather than code.

E.g. if you have a "bad guy", a "really bad guy" and a "whimp" enemy entities, and define C++ classes to handle each of these, you can't change their behaviour without recompiling, and adding new enemy types becomes a real pain. On the other hand, if you define each type of enemy through a number of properties such as "strength", "moral", "movementspeed", "weapon", "IQ" etc., you only need one piece of code to handle all enemy types, and you can change their behaviour on the fly.

Obviously, there are hybrids, but I'd lean toward more data, and less code when defining behaviour.

/Niels

Share this post


Link to post
Share on other sites
sprite[0] = new Bad_Guy;
sprite[1] = new Player_sprite;
sprite[2] = new Heat_Seeking_Missle;

for(i=0; i<3; i++)
{
sprite->AI_routine(); // actually calls different functions
}

how would you add new objects to the sprite list, and change the loop limit dynamically to account for objects being removed/added?

if there are some generic methods which manipulate arrays (add/remove element) that i could use please let me know

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
pindrop:

 How about using linked list or dynamic array? STL provides you ''list''(linked list) and ''vector''(dynamic array). (I guess that the reason why logistix used the array in his post is to explain polymorphism simply.)

  std::list< SPRITES* > list_Sprites;

  list_Sprites.push_back( new Bad_Guy );
  list_Sprites.push_back( new Player_sprite );
  list_Sprites.push_back( new Heat_Seeking_Missile );

  std::list< SPRITES* >::iterator i;
  for( i = list_Sprites.begin(); i != list_Sprites.end(); i++ ){
      ( *i )->AI_routine();
  }

 There is no difficulty to use it!

Kwanji - a game developer in Japan

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!