- Home /
Locking the value of a variable, or something similar
So I'm writing a program and have run into a problem with variables.
var someArray : array;
var someVar : CustomClass;
function Update()
{
var tempVar = someVar;
someArray.Push(tempVar);
//call a function which resets someVar
}
What I want is for 'tempVar' to be put into the array with the values that 'someVar' has during that particular moment. But after I put it into the array it keeps updating itself to whatever the current value of 'someVar' is. How do I make this do what I want it to?
I tried this a second ago and it seems to work fine. Is there something more I need to know?
var someArray:Array; var someVar:int = 0;
function Start()
{
someArray = new Array();
}
function Update()
{
var tempVar:int = someVar; someArray.Push(tempVar); //call a function which resets someVar print(someArray); someVar++;
}
This gives more detail on what I'm trying to do.
var action : TestClass;// 'TestClass' contains a single variable 'number' which is a string
var actionArray : Array = new Array();
var num: int = 0;
function resetAction() // reset all properties for action to default
{
action.number = "";
}
function Update()
{
num++;//increase num by one
action.number = ""+num;//set action.number to be the string of num
var tempAction : TestClass = action;// create a varriable'tempAction' also of class 'TestClass'
actionArray.Push(tempAction);// 'tempAction' to the 'actionArray' array
resetAction(); //reset the value of 'action.number'
if (actionArray.length == 3)// check the property 'number' for the instances of 'action' in the
{ // 'actionArray' when in contains three instances of 'tempAction'
var action1 : TestClass = actionArray[0];
var action2 : TestClass = actionArray[1];
var action3 : TestClass = actionArray[2];
Debug.Log(""+action1.number+action2.number+action3.number);//this currently displays the
} //value of 'number' for all three to be empty but I want them to retain their values
}
Please for gods sake, learn some basic program$$anonymous$$g before attempting to make a game... and learn about references
Answer by treep78 · Feb 13, 2012 at 04:45 AM
Your code worked for that, but when I tried to apply the same technique to a scaled down version of the code I'm working with it didn't work and the values I was trying to save were erased when I cleared the var 'action'.
var action : ActionClass;
var actionArray : Array = new Array();
var num: int = 0;
function resetAction() // reset all properties for action to default
{
action.doer = null;
for(var i :int = 0; i < action.target.length; i++)
{
var resetTarget : CharacterClass = action.target[i];
action.target.RemoveAt(i);
resetTarget.targeted = false;
i -= 1;
}
action.target.Clear();
action.actionType = "";
action.attackAction = null;
action.stage = "building";
}
function Update()
{
num++;
action.actionType = ""+num;
var tempAction : ActionClass = action;
actionArray.Push(tempAction);
resetAction();
for(var i:int = 0; i < actionArray.length; i++)
{
var tempActionInfo : ActionClass = actionArray[i];
Debug.Log(tempActionInfo.actionType);
}
}
Answer by Kaze_Senshi · Feb 13, 2012 at 01:00 AM
Well I don't know the types of your variables, but I think that you can try to make a "clone" of your variable. For example, let be a Vector3, instead of just attribute the variable with the tempVar, you can make tempVar be a new Vector3 and just copy its informations .
var someArray : array;
var someVar : Vector3;
function Update()
{
var tempVar : Vector3 = Vector3( someVar.x, someVar.y, someVar.z ); // Change here
someArray.Push(tempVar);
//call a function which resets someVar
...
}
for some reason this doesn't seem to work with custom classes
Your answer

Follow this Question
Related Questions
Getting a variable from an object 1 Answer
GameObject [,] variable will it be addresses? 1 Answer
Is it possible to store variables in arrays? 1 Answer
How do variable arrays work? 2 Answers
Organizing variables in the inspector 5 Answers