can i convert a string into a refeernce of a variable
for example is their a way in java script for me to do the following
var temp:String = "health";
var temp2:String = "Magic";
var health:int = 100;
var Magic:int = 100;
function StatLoss(test:String){
var [test]-=10;// basically one function to reference any variable without directly referencing it by name, except in the function call
}
function Awake(){
StatLoss(temp);
StatLoss(temp2);
}
also if you have a better suggestion on how to do this type of function by all means please suggest it, but the next best method that’s coming to mind is a swicth with like 40 posibilities.
well the arrays don’t seem to store the actual variable of the int, so when i increase it or manipulate it, it doesn’t change the base variable.
It seems to me then that the only way to do it would be to just store it in an array.
i tried this -
//the stats in this system are supposed to be unlockable
var Agi:int = 0
var Dex:int = 0;
var Con:int = 0;
var BaseStats:Array = new Array();
function UnlockStat(stat:int){
BaseStats.Push(stat);
}
function Awake(){
UnlockStat(Agi);
UnlockStat(Dex);
UnlockStat(Con);
//i did it this way cause in the game i'm doing the stats are supposed to be unlocked as they level up.
}
the end result was that i couldn’t update the variable directly only the variables inside the array, i had hoped that after putting the variables inside the array i could manipulate the variables inside the array.
anyway my game has about 40-50 unlockable stats, and i want to leave it open to add more later, using an array to keep track of all those stats seems like it would get confusing to manage later, i was thinking referencing them directly by String would be a much simpler way to manage it over time.
well sounds like the best choice is an array though, it’s better then a switch for sure. and i could always tell it to update the main stats with a loop statement i suppose.
Don’t know
To me its a shame that there are still “programmers” that even consider using such measures if there are things like Dictionary<> and alike.
The days of sluggish buggy code was a decade ago or so, by now people should be more savy on basics of typesafeness etc and their benefits on error prevention, performance of development and runtime performance.