Is there a way to avoid copying objects and still explicity typecasting them so they work with #pragma strict? I.E. is there a way to make the variables “tempCopy2” and “tempCopy” the JavaScript equivalent of “pointers” rather than copies of the original object?
This isn’t the code I’m working with as that’s 1000 lines, but trying to get Arrays to function properly with #pragma strict is making C++ feel easy at the moment. Is there some sort of syntax I’m missing to allow me to manipulate arrays of class objects without the compiler complaining…
The following code doesn’t work properly because the “tempCopy” variables don’t function as pointers, but are copies. I’d rather not explicitly copy an object out of an array and back in each time I want to access it while still maintaining explicit typecasting. I’m only putting one kind of variable into each of the arrays. If there’s no way to get JS to function properly with their “Array” class, then I don’t even know why they put them there. It’s like if Bill Gates offered you $1,000,000 and then told you he’s just messing around with you…
#pragma strict
private var chickenPens = new ArrayList();
class myClass {
private var numChickens : int = 0;
private var meanChickenVelocity : Vector3;
private var individualVelocity = new ArrayList();
function myClass() {
}
function AddVelocity(tempVec : Vector3) {
individualVelocity.Add(tempVec);
numChickens += 1;
UpdateMeanVelocity();
}
function UpdateMeanVelocity() {
var n : int = 0;
var sum : Vector3;
while (n < numChickens) {
var temp : Vector3 = individualVelocity[n];
sum += temp;
n++;
}
meanChickenVelocity = sum/numChickens;
}
function GetAvgVelocity() : Vector3 {
return meanChickenVelocity;
}
}
function Start() {
var tempCopy : myClass;
var tempCopy2 : myClass;
chickenPens.Add(new myClass());
chickenPens.Add(new myClass());
tempCopy = chickenPens[0];
tempCopy.AddVelocity(Vector3(5,3,2));
tempCopy.AddVelocity(Vector3(2,3,-5));
tempCopy = chickenPens[1];
tempCopy.AddVelocity(Vector3(2,1,1));
tempCopy = chickenPens[0];
tempCopy2 = chickenPens[1];
Debug.Log(tempCopy.GetAvgVelocity() + " " + tempCopy2.GetAvgVelocity());
tempCopy.AddVelocity(Vector3(-3,0,2));
tempCopy2 = chickenPens[0];
tempCopy = chickenPens[1];
Debug.Log(tempCopy2.GetAvgVelocity() + " " + tempCopy2.GetAvgVelocity());
}