Better way to solve script issue than eval?
by Lee Latham · in Torque Game Engine · 04/28/2007 (7:20 pm) · 2 replies
Hello,
I'm a novice scripter, and I've come up with something that works, but it sure is ugly, and I'm wondering if there's a better way.
What I'm wanting to do is spawn a vehicle. What I would like to do is this:
But this generates compilation errors. So what I did was this:
Surely I've made a mountain out of a molehill and there is a better way? I ask because I can see myself doing this sort of thing a lot in the future.
I'm a novice scripter, and I've come up with something that works, but it sure is ugly, and I'm wondering if there's a better way.
What I'm wanting to do is spawn a vehicle. What I would like to do is this:
%newcar = new %type (%name) { //basically I'm wanting to spawn vehicles dynamically
canSaveDynamicFields = "1";
position = %spawnpoint;
rotation = %rotation;
scale = "1 1 1";
dataBlock = %dbname;
disableMove = "0";
mountable = "1";
};But this generates compilation errors. So what I did was this:
%line1 = "%newcar = new " @ %type @ "(" @ %name @ ") {";
%line2 = "canSaveDynamicFields = \"1\";";
%line3 = "position = %spawnpoint;";
%line4 = "rotation = %rotation;";
%line5 = "scale = \"1 1 1\";";
%line6 = "dataBlock = %dbname;";
%line7 = "disableMove = \"0\";";
%line8 = "mountable = \"1\";";
%line9 = "};";
eval(%line1 NL %line2 NL %line3 NL %line4 NL %line5 NL %line6 NL %line7 NL %line8 NL %line9);Surely I've made a mountain out of a molehill and there is a better way? I ask because I can see myself doing this sort of thing a lot in the future.
Associate Orion Elenzil
Real Life Plus
i've bumped into the same thing and had to just do an eval.
however you can simplify your life a little by writing a convenience function like this:
function newScriptObject(%className, %objectName) { eval ("%ret = new " @ %className @ "(" @ %objectName @ ");"); // the following is optional, but it's a good idea if (isObject(MissionCleanup)) MissionCleanup.add(%ret); return %ret; }and then your code above becomes