- Home /
[SOLVED]From superior script to subordinate script
Hey guys,
today I have been working around this issue like half of a day. I would like to see some comparison between some methods. First I will try to explain you what did I do. I am developing Tower-defense game. Currently I am creating new tower system. My basic vision is this: 1 type of towers - 1 superior script. This script would contain every attribute of every upgrade level. (Sale price, price, range, firerate ...) and single towers (gameobjects) - tower script. This tower script would get attributes from superior script from the first instance of this. E.g. price of the first level. And then if someone press the button Upgrade on the same tower it gets again attributes from superior script but from the second instance. E.g. dmg of second level. I want to spare disk, memory space and performance so I figured out that I have to have one source and general script on every tower that can load everything from source. But I am wondering whats the best solution for this and certainly its not the GetComponent method. Trust me I tried it and it sucks. I figured out how to make it happen with static method and variables of the class in the source (superior script). Basically everytime player wants to build or upgrade the tower then it calls a static method of superior script with index. Next the method will load appropriate (by index) values to static variables. Last the variables of subordinate script will set their values based on static variables.
The question is: Is really neccessary to create a class with static variables or is better to use maybe static two dimensional array. One two-dimension array can represent one type of towers. I mean whats the best solution for this static solution? In comparison with speed, space usage and so on...
Btw. I will share here this tower system once it is done. (I guess in 2 days, I have no time tomorrow so..)
Answer by robertbu · Jun 20, 2014 at 10:36 PM
First your disk, memory and performance statement is misplaced. No matter what you do here, this code will take only a small fraction of all three compared to displaying one button, or one icon, or other small texture to the screen. A good design of this code will depend on the nature and implementation of multiple tower types. Given a single tower type, you can do something simple like this:
public struct TowerEntry {
public TowerEntry(float s, float p, float r, float f) {
sale = s;
price = p;
range = r;
firerate = f;
}
public float sale;
public float price;
public float range;
public float firerate;
}
public static class TowerInfo {
private static TowerEntry[] towerData = new TowerEntry[] {
new TowerEntry(1.0f, 1.1f, 1.2f, 1.3f),
new TowerEntry(2.0f, 2.1f, 2.2f, 2.3f),
new TowerEntry(3.0f, 3.1f, 3.2f, 3.3f),
new TowerEntry(4.0f, 4.1f, 4.2f, 4.3f),
new TowerEntry(5.0f, 5.1f, 5.2f, 5.3f),
new TowerEntry(6.0f, 6.1f, 6.2f, 6.3f)
};
public static TowerEntry GetData(int level) {
if (level >= towerData.Length || level < 0)
level = 0;
return towerData[level];
}
}
You would access the data for any given level by:
TowerEntry te = TowerInfo.GetData (2);
Debug.Log (te.range+","+te.price);
Note that nothing here is derived from MonoBehaviour, and that you access the info with the class name (no instance required).
For multiple tower types, you might want to derive all your towers from a base class, and have a static array as part of each individual derived class. That way each tower type would know how to upgrade itself.
Thank you, this is really smooth solution and clever. I will try it asap and let you know. $$anonymous$$uch appreciated :)