- Home /
Best way to store a lot of data by script
Hi there!
I am making a kinda Age of Empires game.(I know this is too big for me but nvm) and I would like to store the data of all the different units inside of a script. The point is that most of the units can be made by most of the civilizations but not all units can be made by all civilizations. Also I would like to store the sprites in this script, but eacht civilization should have a different sprite for maybe the same units. For example: I would like to have a Turkish worker with a different sprite as a Roman worker.
Should I use structs or classes? And should I do this in 1 class of just in 1 struct? I think I should prefer the class because of the cheap data usage but I'm not an expert. :P And would it be smart to use the Resources folder for this?
Any help is appreciated!
PS: C# plz
Answer by nirharpaz · Feb 07, 2015 at 08:58 PM
I would create a class 'Unit' with the basic abilities and functions (like moving)
from 'Unit' inherit from there the classes 'Worker', 'CombatUnit' with their unique stats and stuff
from 'CombatUnit' inherit the archers and melee, catapults and whatever you want with their difenrences (like range, AOE and whatever)
And from each individual unity inherit the specific unit of each civ which defer in sprite basically.
learn about C# inheritance rules first.
It basically means that if you inherit class B from class A - or class B extends A, then B is some kind of A.
To make it more clear, all units are some kind of 'Unit' So when you do Class Worker extends Unit you actually gave worker all the characteristics f Unit w/o writing all the code twice.
Then 'Class TurkishWorker extends Worker' cause Turkish worker is some kind of worker and has the worker's ability. The final results are the complete unit and not an abstact representation of what you want the unit to do (there is no general unit appearing in the game or general worker), so they are finally able to receive the sprite that allow them to look - while still holding all the abilities of their ancestors: Worker and Unit.
hope it helps
Thank you for posting! At first I'm very glad u introduced me to inheritance, I've looked up some more information and it's so useful! And I have never used it before. ($$anonymous$$akes me feel noob :P) I have used it but I'm not sure it if I've used it right:
public Worker worker = new Worker();
public GaulWorker gaulWorker = new GaulWorker();
public Spear$$anonymous$$an spear$$anonymous$$an;
public GaulSpear$$anonymous$$an gaulSpear$$anonymous$$an = new GaulSpear$$anonymous$$an();
public class Unit{
public int costs;
public int health;
public int attack;
public int moveSpeed;
public int creationTime;
public Sprite unitSprite;
}
public class Worker : Unit{
public int workSpeed;
}
public class CombatUnit : Unit{
public int range;
public int runEnergy;
public int morals;
}
public class Spear$$anonymous$$an : CombatUnit{
}
public class Archer : CombatUnit{
}
public class GaulWorker : Worker{
}
public class GaulSpear$$anonymous$$an : Spear$$anonymous$$an{
}
void Start(){
gaulWorker.costs = 50;
gaulWorker.health = 60;
gaulWorker.attack = 3;
gaulWorker.moveSpeed = 5;
gaulWorker.creationTime = 50;
gaulWorker.unitSprite = Resources.Load ("Units/Buildable/Gaul/worker", typeof(Sprite)) as Sprite;
gaulWorker.workSpeed = 5;
spear$$anonymous$$an.costs = 60;
spear$$anonymous$$an.health = 60;
spear$$anonymous$$an.attack = 5;
spear$$anonymous$$an.creationTime = 50;
spear$$anonymous$$an.runEnergy = 12;
gaulSpear$$anonymous$$an.unitSprite = Resources.Load ("Units/Buildable/Gaul/spear$$anonymous$$an", typeof(Sprite)) as Sprite;
gaulSpear$$anonymous$$an.range = 2;
gaulSpear$$anonymous$$an.morals = 10;
}
I'd like to know what I can improve about this because I've got the feeling that it isn't exactly the way u ment.
I go to bed now, Ill have another look tomorrow as soon as I can :)
looks fine to me the specific classes can allow you to have unique sprites and also a room for future unique racial abilities.
3 comments about what you have done:
attack - can workers attack in self defense? if so, fine. if not move it to CombatUnit.
I see you also gave the spearman hitting range. when i offered you, I viewed the melee units hitting at collider range and not needing range. So, since you have done it this way, note that there is no more difference between spearman and archer. the classes Spear$$anonymous$$an and Archer I$$anonymous$$O are usless and you can inherit them straight from CombatUnit.
public class GaulSpear$$anonymous$$an : CombatUnit{
}
last, I see all your values are int. note that if 4 and 9 are int then 4/9=0; numbers in int are always rounded down (to keep them int). so if you are going to calc and * or / calculations to deter$$anonymous$$e damage, dodge or whatever, change it to float.
Thank you for your help! It is true that I want workers to be able to defend themselves.
I am not sure if it would be smart to remove the Spearman and Archer classes since it spares me a lot of time coding. This because all civilizations can make archers and spearmans but only the sprites are different. With the archer and spearman class I can fill this data for all spearmans of all civilizations.
And about the int comment: I think ill just use $$anonymous$$athf.Round() for the calculations, this shouldnt give any problems.
As long as you are aware that's fine. some things like chances cannot be calced easily using only 0 or 1. In 1 of my games i had some terrible issues with errors for dividing things by 0, till i discovered it was an int variable for the score that made all the mess.
Another few things:
Remember you can also inherit functions.
Remember you can also give default variables, like
int life = 10;
in 'unit' so every new unit, unless stated otherwise in its 'Start' will have 10 life as standard, rather then 0; (and not just the variable).
Last, lets say you made the function 'Attack()' in 'Unit' that gives a punch. Suddenly you want the archer to release a projectile at target position which is a different action... You can create, in the 'Archer' class (now you have a reason to have it) an OVERRIDING function of 'Attack()' telling it to fire a projectile at the target.
After doing this you will be able to tell both spear-man and archer to 'Attack()' and watch each 1 of them doing his thing. moreover, (check how to do it) if a unit gets too close to the archer, there is an option to tell the archer to use his ancestor's 'Attack()' function and start punching rather than shooting.
This combo of inheriting vars and functions and the ability to override them is the true power.
hope it helps
Your answer
Follow this Question
Related Questions
Lists and Structs instead of Arrays? 4 Answers
C# ArrayList access from other script 1 Answer
Multiple Cars not working 1 Answer
How to make classes/structs public to all scripts 0 Answers
Distribute terrain in zones 3 Answers