Advertisement

storing my objects

Started by October 28, 1999 07:26 AM
4 comments, last by thmleo 24 years, 7 months ago
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

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->AI_routine(); // actually calls different functions<BR> }<P><BR>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.<P>Good luck.

-the logistical one-http://members.bellatlantic.net/~olsongt
Advertisement
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?

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

<b>/NJ</b>
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

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

This topic is closed to new replies.

Advertisement