Skip to main content
gamedev.net
🔒 Locked

Programming game entities

Started by stevenson May 22, 2003 at 10:06 PM 3 replies 250+ views
Original Post
stevenson
stevenson
I have been working on a game project and was wondering how I would go about spawning entities such as players, bombs, rockets, etc. All of these entities need to be updated in the main loop. I can create class structures and create entities that way, but I need a way to enter a function (say SpawnEntity) that creates an instance. The main problem I am having trouble with is, how do I know all the names of all the instances created of a class, so I can loop through them and manage them. I am trying to think of a way to use unique ID numbers, like a relational database, but I cannot think of any ways. Any advise on how to set this up would be greatly appreciated. Thanks.
DerMeister
DerMeister
Well I''m working on something like that also (beggining with it) and my idea is to use a double linked list (also thought about a STL vector) anyway... I think it is a good choice to use IDs for the type of objects so as before said you could update every object in the main loop depending on their type (specified by the ID).
Something like this: (pseudocode)


  
from listHead to listTail
{
switch(CurrentObjectInTheList.ID)
{
case OBJ_PROJECTILE: CurrentObject.GetCollision()
case OBJ_ASTEROID: CurrentObject.Orbit()
case OBJ_SPACESHIP: UpdatePosition()
and so on...
}
}


The trick is in polymorphism... asumming you are using C++ you should derive every class to be part of the list from the list class...

i.e: class Asteroid : public ListNode

Maybe this is not what you asked but it may open your mind and find a better solution than mine...


No matter where you go... &this
No matter where you go... &this
Dak Lozar
Dak Lozar
As an alternative to what DerMeister suggested, I like to keep my objects of like type in their own containers. (I''m working on an RTS, and RTSes have tons of objects create and destroyed throughout the life of the game so... ) I do this for two primary reasons.

1) Having containers for each specific object type helps me to code the update cycle of the game. On even frames I update all objects in containers A and B and on odd frames containers C and D. Make sense? It also encapsulates my de/serialization and object registration for save games.

2) I have created a Win32 heap class that provides me with less fragmentation and faster object instantiation. This really has nothing to do with the way I use my container class but seeing as the memory for like objects is a psuedo container I thought it worth mentioning.

It may seem tedious and a bit overkill but I''ve found it to be intuitive... especially when you are away from the code for a few weeks and come back to re-learn... I always say to myself, "Wow, this is pretty slick" - hey, everybody needs a pat on the back sometime... even if it comes from yourself



Dave "Dak Lozar" Loeser
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
Cahaan
Cahaan
std::map has HUGE advantages over linked lists and vectors. I would use it instead.

Why ? fast search, fast adding, fast removing elements with simple complexity.
Darkhaven Beta-test stage coming soon.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.