Game Development Community

(TorqueScript) Set an object field virtually?

by Robert Lee · in Torque Game Engine · 01/20/2005 (1:16 pm) · 8 replies

Hi, I'm looking for a way to be able to achieve the following with torque script code:

%obj = GetObjectName();
%field = GetFieldName();

%obj.%field = GetValue(); // this causes an advanced script error

Any suggestions? I also tried shoving the command to be evaluated into a call() function but that doesn't work either.

#1
01/20/2005 (1:29 pm)
I guess I dont understand what you are asking, but here are some functions off the top of my head:
// gets the name of the object
%name = %obj.getName();

// gets the class of the object
%class = %obj.getClassName();

// gets the datablock for an object
%data = %obj.getDataBlock();

// gets some field value from that objects datablock
%someField = %data.someField;
#2
01/20/2005 (1:50 pm)
Quote:// gets some field value from that objects datablock
%someField = %data.someField;

I'd like the "%data.someField" part of that code to be chosen virtually/dynamically.

For example:
In some cases 'data' will "point" to an Emitter, in another case, a Particle.
In some cases 'someField' will "point" to the ejectionPeriodMS field, in another case, ejectionOffset.

Put another way, I was hoping to write a function similar to...

function SetFieldValue(%objName, %fieldName, %value)
{
    %objName.%fieldName = %value;
}

An then I use it like so ...
SetFieldValue("CrossbowBoltEmitter", "thetaMax", 10.0);


I hope that makes more sense.
#3
01/20/2005 (2:24 pm)
I just discovered the eval() function, and it allows me to do what I need. I still wonder if there's a better way to do this, but it's no biggie.

Strangely enough, eval() is not listed in the script reference section of Ken Finney's book.
#4
01/20/2005 (11:16 pm)
That's odd. Anyway, yes, eval works good. You can also do things like

("Obj" @ %var).field

And I think (tho am not 100% sure) that

("Obj" @ %var).(%anotherVar)

works. Naturally, if it doesn't you can fall back to eval very easily.
#5
01/21/2005 (9:14 am)
Thanks. It doesn't look like this works ...

(%obj).(%field) = %val;

But it's nice to know I can at least do this ...

(%obj).fleldName

And eval handles the rest.
#6
01/22/2005 (4:17 pm)
Thanks for checking what I said. :)
#7
01/22/2005 (5:05 pm)
Robert, do this:
eval( %obj @ "." @ %field @ " = %val ;" );
#8
01/24/2005 (8:26 am)
Thanks. It doesn't look like this works ...

(%obj).(%field) = %val;

But it's nice to know I can at least do this ...

(%obj).fleldName

And eval handles the rest.