RTS unit tracking

Started by
6 comments, last by dsage 21 years, 9 months ago
What do people use to keep track of rts units stas like hp mp? I read something about linked lists? thnx in adavance!
Advertisement
Units'' data is commonly kept in a class or structure that contains all the data about that unit. HP might be stored as an int member variable, although the data type might be different depending on the algorithms, etc., that are used. A linked list is basically an array of variables that can expand or contract whenever necessary. This works in the following way: each node in the linked list has the variable(in this case, a class containing info about a unit) and a pointer to the next node. In this way, nodes can be added, and is such suitable for units, which may be created or destroyed at any given time. I hope I did a decent job of explaining this.
-----------------------------Weeks of programming can save you hours of planning.There are 10 kinds of people in this world-- those who understand binary and those who don't.
A very inflexible design:
struct unit{DWORD type;VECTOR Position, Velocity;float health;...unit *next,*previous;}; 


This works but you have to rewrite all the linked list code any time you want to do the same for other types. C++ has Vector templates (and probably straight lists) in the Standard Template Library, or in stright C for flexibility (though more care needed to avoid errors)

struct LinkedList{LinkedList *next,*previous;int DataType; //not really needed if you are carefulvoid *Data;}; 


Then set the Data pointer to point to your custom structure or even a simple int or float.


John 3:16
Hmmm, I read this question before leaving work and on the drive home I continued to think about it... I myself have been designing an RTS and have been planning to use one of the STL Associative Containers – like the map.

There are many benefits to using an Associative Container over the more normal Sequence Containers. A map ensures that all the objects inserted are unique. This may or may not be important – also, I believe that the map is implemented using red black trees (depending on your implementation of the STL) and therefore the numerous searches that an RTS will perform gain an increase in speed. Of course, the hit is taken when objects are inserted or removed from the container but it is only O(lg n) .

You can find many examples on the net showing the use of maps or any of the Associative Containers. – Good Luck.

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
In the new RTS book I am working on I use a static array of units with a unit manager managing the active units.

I prefer this method as you have less "new" and "delete" overhead. It also makes the multiplayer implementation a bit easier (for sending absolute unit IDs, etc.)

LostLogic
www.lostlogic.com
Author, Multiplayer Game Programming
Author, Strategy Game Programming with Direct X 9 (Not yet released)


[edited by - LostLogic on July 18, 2002 11:09:36 PM]

LostLogicwww.GamerOutfit.comXBox 360 Community Games Reviews and NewsExisled - 2D/3D Shooter for XBox 360

For my RTS, I do pretty much the same as what Lost Logic does (array of units with a unit manager). One extra thing I have done, which you may or may not want to do is have a reference unit for every unit type which stores things like max health, speed, weapon type etc. This means that instead of having every unit storing a copy of those, they simply have a pointer to their reference unit. The upside is that if you want to upgrade all unit of a specific type, you only need to change the reference unit, the downside is that units lose individuality and you can''t have one unit of a kind learn how to move faster or something without all of its friends learning the same.

Trying is the first step towards failure.
Trying is the first step towards failure.
ragonastick hit the proverbial nail on the head. In addition to a unit manager I also set reference units I refer to as static units.

They are loaded at run-time and there is only one unit for each type in the game. Each unit that is created during the game is tagged as a dynamic unit and each one contains a reference of their "static" unit type.

The same holds true for buildings, npc creatures, etc. They all work as entities and just have different data attributes.

To get around the individuality problem ragonastick speaks about I also give units a few ability modifiers. For example, each unit may have a speed, health, offense, and defense bonus. This allows the engine to give a single unit a bonus. It works well for experience systems (veteran units.)


LostLogic
www.lostlogic.com
Author, Multiplayer Game Programming
Author, Strategy Game Programming with Direct X 9 (Not yet released)

LostLogicwww.GamerOutfit.comXBox 360 Community Games Reviews and NewsExisled - 2D/3D Shooter for XBox 360

If you''re dealing with linked lists, you might consider adding a "dead" anchor. Rather than allocating and deallocating the memory, you send the object somewhere where it won''t get counted, and reset the values when the dead objects get added back to the list. If the graveyard was empty, though, you''d still need to make New objects.
Saves a little overhead, though -- you''d only be changing four pointers.

-Sta7ic

This topic is closed to new replies.

Advertisement