Script - argument passing problem
by John Vanderbeck · in Torque Game Engine · 03/22/2004 (5:16 pm) · 5 replies
Given this script code:
In function setVehicle() the passed in argument %vehicle is always empty. You will note in function nextVehicle() the variable passed in is a global array item.
Now obviously I don't even need to pass this since its global, but that's outside of the point here. Is this a problem with passing a global as an argument? A problem with the fact that its an array item? A problem with me being too tired for my own good?
function setVehicle(%vehicle)
{
echo("setVehicle(" @ %vehicle @ ")");
echo("Current Vehicle: " @ $vehicles[$currentVehicle]);
if (%vehicle $= "")
{
view.setEmpty();
}
else
{
// set the displayed object
view.setObject("vehicle", "r1/data/shapes/vehicles/" @ %vehicle @ ".dts", "", 0);
// load and display the text info file for the vehicle
}
}
function nextVehicle()
{
$currentVehicle++;
if ($currentVehicle > $maxVechicles)
$currentVehicle = 0;
echo("Setting current vehicle to: " @ $vehicles[$currentVehicle]);
setVehicle($vehicles[$currentVechicle]);
}In function setVehicle() the passed in argument %vehicle is always empty. You will note in function nextVehicle() the variable passed in is a global array item.
Now obviously I don't even need to pass this since its global, but that's outside of the point here. Is this a problem with passing a global as an argument? A problem with the fact that its an array item? A problem with me being too tired for my own good?
#2
Before I posted this I checked all the spellings but I still managed to miss that :(
Funny part is, i'm not sure that matters right there. Since torque creates variables on first use, that would evaluate to "$vehicles[0]" I think, which means I should have gotten the first vehicle in the array.
But in any case, its an obvious error. My apologies for such a stupid post.
03/22/2004 (7:24 pm)
GAH! Defintly sleep. I was typing vehicle so many times that after a while it got to that weird point where the word looks like its spelled wrong no matter what :pBefore I posted this I checked all the spellings but I still managed to miss that :(
Funny part is, i'm not sure that matters right there. Since torque creates variables on first use, that would evaluate to "$vehicles[0]" I think, which means I should have gotten the first vehicle in the array.
But in any case, its an obvious error. My apologies for such a stupid post.
#3
Just so ya know, the erroneous code does not evaluate to $vehicles[0]. TorqueScript doesn't auto-assign new variables to 0. It shouldn't either, because that would assume that new variables are to be used as numbers. :)
Just thought I'd chime in with a smart-ass, know-it-all comment and make you feel even sillier. ;p Good times.
03/23/2004 (1:21 am)
Hehe woops. No worries, that crap happens to everybody.Just so ya know, the erroneous code does not evaluate to $vehicles[0]. TorqueScript doesn't auto-assign new variables to 0. It shouldn't either, because that would assume that new variables are to be used as numbers. :)
Just thought I'd chime in with a smart-ass, know-it-all comment and make you feel even sillier. ;p Good times.
#4
03/23/2004 (5:00 am)
Interesting. So what are new variables intialized to? Are they at all or just left to random memory?
#5
What happens when we attempt to read the value of a variable that is unset? TorqueScript returns "", an empty string. Doing some simple tests will confirm this.
More accurately, if some TorqueScript code attempts to read the value of some variable like "$misspelled_var_name", where $misspelled_var_name is a new variable we accidentally just created, TorqueScript will attempt to look up $misspelled_var_name in it's table of objects and tokens. When it finds out that $misspelled_var_name does not exist table, TorqueScript will return "", empty.
Given the typeless, declaration-free nature of the language, this is the only sensical thing to return when nonsensical variables are read. :)
03/23/2004 (11:21 am)
Ok, here's the nitty-gritty answer. So, as you know, TorqueScript does not require explicit variable declarations. This has all the usual strengths and weaknesses. One weakness is that it requires you to be extremely careful with variable names. If a variable name is misspelled, TorqueScript will actually create a separate variable with that (misspelled) name. What happens when we attempt to read the value of a variable that is unset? TorqueScript returns "", an empty string. Doing some simple tests will confirm this.
More accurately, if some TorqueScript code attempts to read the value of some variable like "$misspelled_var_name", where $misspelled_var_name is a new variable we accidentally just created, TorqueScript will attempt to look up $misspelled_var_name in it's table of objects and tokens. When it finds out that $misspelled_var_name does not exist table, TorqueScript will return "", empty.
Given the typeless, declaration-free nature of the language, this is the only sensical thing to return when nonsensical variables are read. :)
Torque 3D Owner Martin "Founder" Hoover
->currentVechicle<-
should be:
->currentVehicle<-