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.
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.