- Home /
Losing array in class after Array.ToBuiltin?
Hi. Got a problem.
I've got this code (troll is a type of tank if anyone asks):
class Indinces {
var i = new Array ();
function Indinces (things : GameObject[], startIn : Array) {
if (things.Length > 0) {
for (thing in things) {
var thingVar = Diggers (thing);
i.Push (thingVar);
}
for (t = 0; t < i.length; t++) {
for (sv in startIn) {
i[t].v.Add (sv);
}
}
}
}
}
class Diggers {
var o : GameObject;
var v = new Array ();
var e = new Array ();
function Diggers (oo : GameObject) {
o = oo;
}
}
function Update () {
tanks = GameObject.FindGameObjectsWithTag ("Tank");
trollRockets = GameObject.FindGameObjectsWithTag ("RocketTroll");
var indinces = new Array (); //contains ints
var iTanks = Indinces (tanks, indinces);
var iRockets = Indinces (trollRockets, indinces);
for (/*stuff*/) {
var iTanks2 : Diggers[] = iTanks.i.ToBuiltin (Diggers) as Diggers[];
for (tankClear in iTanks.i) {
tankClear.v.Clear ();
}
var iRockets2 : Diggers[] = iRockets.i.ToBuiltin (Diggers) as Diggers[];
for (rocketClear in iRockets.i) {
rocketClear.v.Clear ();
}
//this is where problems start*
*after this, the "o" object is still there, but the "v" array is empty. Maybe a chance that it was lost while Array.ToBuiltin ()?
Thanks for reading this far.
EDIT: Same problem with Generic Lists.
o = oo;
Very expressive, self-explanatory na$$anonymous$$g :) - If you keep it like that it won't be long till you get your self and your $$anonymous$$m lost and confused working on a big project. Just a side tip: Use self-explanatory names, saves you a lot of headache :)
the best thing to do if you want another one-letter variable is to type that letter twice :D btw, o as object, v as vertex, e as explosion. Its not hat confusing :)
"the best thing to do if you want another one-letter variable is to type that letter twice" lol, good one.
It's better to be crystal clear than trying to guess what something is. It might be O$$anonymous$$ here (a little bit O$$anonymous$$) when your scripts are not very big, but when they do get big, when you deal with your $$anonymous$$m, and even when you get back to your code after a while it will look Babylon text :D
It will be very hard for you to remember what you meant with your letters :) - Not to mention, good na$$anonymous$$g make documentation short, if the name is clear, then the purpose is clear.
A good programmer writes well documented code, but yet a better one lets his code speak for itself, without any documentation! Wouldn't that be nice? :)
aren't you a poet? but I suppose yeah, I should add comments after variable to know what they are (if thats what you meant)
Never use the Array class under any circumstances. Ins$$anonymous$$d of this array conversion stuff, just use a generic List.
Answer by ArkaneX · Sep 14, 2013 at 12:50 PM
Converting to builtin array does not mean the array elements are cloned - they are still the same objects. So first element in iTanks.i javascript Array is exactly the same element as first element of iTanks2 .NET array.
You can't then call v.Clear() on each element of both javascript arrays, and expect that it won't affect elements of newly created builtin arrays.
EDIT: you can check that by examining v.length after calling ToBuiltin but before Clear calls.
Actually, that sucks. One would think that converting an array will solve the problem, but no, it's a shallow copy again. Well, thanks anyway :). I already fixed it, more complicated way that ToBuiltin (), I'll maybe post it later.
It's not a shallow copy, it's just the same reference. Shallow copies are new objects.
Your answer
