//Good dynamic global
function OnCollisionExit(collision : Collision) {
var thisname = new Object;
var thisname = this.rigidbody.name;
eval(“Globals.” + thisname + “og = 0”);
}
For some reason this freezes for a second the first time it hits the ground, but thats not so bad and I’ll figure that out later.
Why are you declaring thisname twice anyway? I’m just not clear on what it is you’re trying to do here, create an object then eval that object as a string? Couldn’t this be done some other way that to use eval?
eval() causes the freeze because it has to compile code on the fly. Generally you want to avoid using it, except in specific circumstances. I’m not sure what you’re doing, but there’s probably a better way, like using arrays or hashtables. Also you don’t want to use “OnCollisionExit(collision : Collision)” unless you’re actually using the collision info; “OnCollisionExit();” is faster.
The reason I use eval is because I want to call a variable with a variable. In php you would do something like:
$name = “box”;
$$name = “Value”;
change the variable box to Value.
So the explanation to why I was doing this was I have 1 script that I made to determine if the object is on the ground. in my globals I have 3 variables 1 for each object. box1og, box2og and box3og. My intentions are to change that variable to true if it’s on the ground, but the variable I am updating depends on what object I am on. I decided to change this and will just use a couple of if statements instead of eval.
I recently just ran into another problem that I can use eval for and I am having a hard time finding another solution.
I have a php script that I load with this output:
&username=BLAH&score=BLAH&tournament=BLAH
so I am breaking it up like this:
var locLines = download.data.Split(“”[0]);
var locItems = locLines[1].Split(“=”[0]);
now here is where I could use eval to create all my variables in unity or do something like this:
var username = locItems[1];
var locItems = locLines[2].Split(“=”[0]);
var score = locItems[1];
var locItems = locLines[3].Split(“=”[0]);
var tournament = locItems[1];
I have a bunch of data coming from some php scripts and it would take lines and lines of code to do this for each call. Any ideas? I thought of using an string array, but I’m just starting to learn javascript and failed when I attempted it. I thought it would be something like this:
var stringarray : new ArrayList();
stringarray[$locitems[0]] = $locitems[1];
That’s probably all wrong, but you get the idea. I’m still searching google and these forums for a good way to do that without using eval. In PHP I would do something like this: varname = "NAME";
{$varname} = “VALUE”;
Creating a variable called NAME with a value of VALUE.
I think you’d just use an array for that. Have “boxOnGround = new boolean[numberOfBoxes];” and then each object has a corresponding number. This should be a lot easier to use and more flexible, since you can then have any number of boxes without having to change the code, and it enables you to do things like loop through them all at once if you wanted to.
One possibility is to use a hashtable:
var locLines = download.data.Split(""[0]);
var myData = new Hashtable();
for (i = 1; i <= 3; i++) {
var locItems = locLines[i].Split("="[0]);
myData[locItems[0]] = locItems[1];
}
print (myData["username"] + " " + myData["score"] + " " + myData["tournament"]);
That would be a hashtable like I did above; an actual string array would be:
var stringarray = new String[sizeOfArray];
stringarray[0] = $locitems[1];
You’d refer to each entry by a number in this case. You can use enums to make it more readable: