Strategy Game Unit Stats

BlitzMax Forums/BlitzMax Beginners Area/Strategy Game Unit Stats

Czar Flavius(Posted 2007) [#1]
Hello. I am making a grid-based turn-based strategy game. My question is concerning how the statistics for each unit should be stored and accessed.

I originally created a unit type having fields such as x, y, attack, defence etc. and when a new unit was created, the correct stats were given to it from some store, probably in an array. Then I realised that would be a redundant usage of memory and I might as well get the stats for each unit from an array when needed.

So if I create a unitstats type containing field such as attack, defence and put them in an array where each unit has an id number, I can access the stats like so: stats[unit.id].attack. The units will be read from a file so not knowing which id is for which unit doesn't matter as the computer will process it.

Then the actual unit type will only contain the information unique to that instance, such as x, y, player, hitpoints, movementpoints. Its id number will allow me to gain access to its statistics.

An alternate idea I had was each individual unit had attack, defence etc fields but these were points to the array elements. Then I could do unit.attack[0], which points to stats[unit.id].attack. However this will use memory which isn't needed (though not much) also I don't like dereferencing pointers with [0]. Is there another way? It seems a bit inelegant because unit.attack isn't being used as an array.

So, there's my plan of action. Any suggestions or comments would be much appreciated. Thanks.


TaskMaster(Posted 2007) [#2]
How many units will be alive at a time? Thousands? Millions? I can't see the memory used for a few stats in each unit being that big of a deal. And, putting those stats in each unit leaves you the opportunity to change them individually in the future. Lets say after 1 unit kills 5 enemy units he raises a level and gains 1 attack point. Or, if the player upgrade a facility in his game, then the ground forces created by that facility have a higher attack or defense...

In other words, I think leaving that stat as part of the unit doesn't use that much memory and leaves you the choice to change it in the future.


Czar Flavius(Posted 2007) [#3]
The number of units would be a couple of dozen at most.


FlameDuck(Posted 2007) [#4]
Wow. Talk about overengineering a simple problem. Why not just have the stats as Static (Global in blitz) fields in your type?


Czar Flavius(Posted 2007) [#5]
That's my problem though. I have a unit type, but the unit could be any um type of unit. Say I have 3 unit types: goblin, dwarf and giant. These will be represented by the same unit type. I don't want to hardcode loads of different types for the same unit.

But their stats must be stored somewhere. I was thinking of giving each unit type and id, and storing their stats in an array. Then accessing these stats from the array on the fly. So a goblin unit would be the same as a giant unit, only a different id number leading them to a different statistic (and image). Because the current position, owning player etc are the only statistics unique to a unit and not related to its stats, I think they should be the only ones stored on a per unit basis.

Uhh, is this making any sense?


TaskMaster(Posted 2007) [#6]
Make a baseunit, then extend it with other type, dwarf, goblin, giant, etc...

Give the base unit the attack and defense stats. In the New() method for the dwarf/goblin/giant, set those stats.


technician(Posted 2007) [#7]
I think FlameDuck provided you with the best solution.

Create a unit type, extend your goblins and giants out of that, and put some global fields in those extended types. Should work just fine, although TaskMaster has the the more flexible solution, since you would be able to "upgrade" the stats in-game.

You could do it the way you're talking in your last post, but that seems a bit too complex for such an issue, and it would be pretty limiting. K.I.S.S. ;)


Czar Flavius(Posted 2007) [#8]
Noooo I don't want to hard code the unit types in my code.


H&K(Posted 2007) [#9]
create a type "UnitType", which has in it all the standard things that each unit of that type would have. (At min Unit type name).

Then when you have a Unit it Contains a field UnitType. This is only a pointer to one of the UnitTypes you define, so you dont have to worry about using memory loads.

If you concider how you would do a tilemap, you wouldnt have an array with all the data for each square would you? No you would have a pointer to what type of terrain each square was. This is all you do with Units


Czar Flavius(Posted 2007) [#10]
.