- Home /
Storing game information in Array
Ok, I have seen questions that are similar to mine, but not getting the answer / information I'm looking for so here I go.
(Also note I'm not going for optimization at this point, just trying to get it to work)
My game is your average tower defence. I want to somehow store some information for each turret like so:
Turret ID = int
Turret Cost = int
Turret CanUpgrade = boolean
Turret HoverModel = transform
Turret PlacedModel = transform
Turret Tooltip = transform
I have 2 scripts: GlobalStatistics.js and GUI_inGame_VISUALS.js
GUI_inGame_VISUALS.js has a section in Update() that checks between two ints and changes a text mesh colour based on the result:
//turret counts
var turretSouls : TextMesh[];
var c : int = 0;
for (c = 0; c < turretSouls.length; c++){
var number : int = GlobalStatistics.turretTable[Mathf.Pow(0,c)][1];
if (number < GlobalStatistics.Souls){
turretSouls[c].renderer.material.color = Color.black;
} else {
turretSouls[c].renderer.material.color = Color.green;
}
}
GlobalStatistics.js has an Array that basically fills out like this:
public static var turretTable : Array[];
//MTG 2013 POPULATE
function Awake(){
turretTable = new Array[1];
turretTable[0] = new Array[6];
turretTable[0][0] = 0;
turretTable[0][1] = 5;
turretTable[0][2] = true;
turretTable[0][3] = whataverTransform_1;
turretTable[0][4] = whataverTransform_2;
turretTable[0][5] = whataverTransform_3;
}
Now my current way of doing it does not work because I cannot do Object to Int for the comparison, and aside from that I've read lots of times that using Arrays is a crap idea.
Possibly I could store that information in a script attached to the turrets themselves, but I would prefer to have some kind of list like above.
My question is: What is the best way to store such information? I don't want seperate storage arrays for the ints/transforms/whatever, they would preferably all be together in the same kind of "storage" and be accessible from it.
Will edit if I need to clarify anything. Any and all help is definitely appreciated since I am not the best at coding.
Answer by nixcs2512 · Aug 05, 2013 at 11:41 PM
I think you only need to create a new class to store all information about Turret,like:
class Turret
{
var ID:int;
var Cost:int;
var CanUpgrade:boolean;
var HoverModel:Transform;
var PlacedModel:Transform;
var Tooltip:Transform;
}
and you only need to store and array of Turret type, like:
var turretTable Turret[];
function Awake()
{
turretTable = new Turret[1];
turretTable[0].ID = ...;
...
}
Hope it works. Because i don't know much about JS.
Right so in the end I've got this:
class turret {
var id : int = 0;
var cost : int = 0;
var canUpgrade : boolean = false;
var hover$$anonymous$$odel : Transform = null;
var placed$$anonymous$$odel : Transform = null;
var toolTip : Transform = null;
}
And this builds me list:
var newTurd : turret[];
function Awake(){
newTurd = new turret[10];
newTurd[0] = new turret();
newTurd[0].id = 0;
newTurd[0].cost = 5;
newTurd[0].canUpgrade = true;
newTurd[0].hover$$anonymous$$odel = null;
newTurd[0].placed$$anonymous$$odel = null;
newTurd[0].toolTip = null;
GlobalStatistics.turretListNEW = newTurd;
}
The reason it didn't work is I tried to populate it while accessing newTurd from another script. Ins$$anonymous$$d I populate, and then load it into the global variable.
Thank you for the class suggestion, it honestly did not occur to me. Cheers~
Your answer
Follow this Question
Related Questions
Set Playerpref("String" + Int)?? 1 Answer
Storing away monsters 1 Answer
Referencing a struct/class From Another Script 3 Answers
How to create a table, array of array? 1 Answer
Load entire table from MySQL 1 Answer