SimObject function
by Christian M Weber · in Torque Game Engine · 03/27/2004 (7:56 am) · 4 replies
Console/simBase.cc
Trying to get this to work but failing, I've printed messages in the console.
ex:
new ScriptObject(Temp);
function Temp::echo(%a, %b) {
echo("called "@%a SPC %b);
}
Temp.Call(echo);
echos: "3 [0 call] [1 Temp] [2 echo] [3 null] [4 null] ..."
Temp.call(echo, "A", "B");
echos "5 [0 call] [1 Temp] [2 echo] [3 A] [4 B] ..."
echos "called 1573 B"
1537 being Temp object id.
Any help is much appreciated :)
ConsoleMethod(SimObject, call, const char *, 3, 0, "call(funcName [,args ...])")
{
Con::printf("%i [0 %s] [1 %s] [2 %s] [3 %s] [4 %s] ...", argc, argv[0], argv[1], argv[2], (argc > 3 ? argv[3] : "null"), (argc > 4 ? argv[4] : "null"));
return Con::execute(object, argc - 2, argv + 2);
}Trying to get this to work but failing, I've printed messages in the console.
ex:
new ScriptObject(Temp);
function Temp::echo(%a, %b) {
echo("called "@%a SPC %b);
}
Temp.Call(echo);
echos: "3 [0 call] [1 Temp] [2 echo] [3 null] [4 null] ..."
Temp.call(echo, "A", "B");
echos "5 [0 call] [1 Temp] [2 echo] [3 A] [4 B] ..."
echos "called 1573 B"
1537 being Temp object id.
Any help is much appreciated :)
About the author
#2
If so, the reason it's spitting back "1537" (or whatever Temp's id will be) instead of "A" is that the first parameter passed to a script object member function is always the objectid. So, in the code above, %a = Temp's objectid. (Typically, you should name the first parameter "%this", to make the objectid passing more explicit.)
To achieve the behavior described above, make the following changes:
--------------------
function Temp::echo(%this, %a, %b)
{...}
Then, change your final Temp.Call() function call to the following:
Temp.Call(echo, Temp.getID(), "A", "B");
This will echo "called A B"
-------------------
If this is not what you're looking for, or you already knew about the implicit id passing, my apologies for being long-winded. It is difficult to determine with certainty what you're after, given the information provided. Tell us more about what you're trying to do and what may be wrong, and hopefully we can figure it out.
edited to provide more explanation
03/28/2004 (1:11 am)
Do you just want your new Call method to call the designated script function? Explicitly, do you just want the last line to echo "called A B"?If so, the reason it's spitting back "1537" (or whatever Temp's id will be) instead of "A" is that the first parameter passed to a script object member function is always the objectid. So, in the code above, %a = Temp's objectid. (Typically, you should name the first parameter "%this", to make the objectid passing more explicit.)
To achieve the behavior described above, make the following changes:
--------------------
function Temp::echo(%this, %a, %b)
{...}
Then, change your final Temp.Call() function call to the following:
Temp.Call(echo, Temp.getID(), "A", "B");
This will echo "called A B"
-------------------
If this is not what you're looking for, or you already knew about the implicit id passing, my apologies for being long-winded. It is difficult to determine with certainty what you're after, given the information provided. Tell us more about what you're trying to do and what may be wrong, and hopefully we can figure it out.
edited to provide more explanation
#3
Now I just need to find a way to not have to pass the object id like that.
edit:
Heres the fixed version
Now I don't have to
eval(%object@"."@%funcName@"("@%blah@", "@%blahAgain@");");
Anymore, which just looks ugly in the scripts and hard to read.
03/28/2004 (4:16 am)
Thanks Josh :)Now I just need to find a way to not have to pass the object id like that.
edit:
Heres the fixed version
ConsoleMethod(SimObject, call, const char *, 3, 0, "call(funcName [,args ...])")
{
argv[1] = argv[2];
argv[2] = object->getIdString();
return Con::execute(object, argc - 1, argv + 1);
}Now I don't have to
eval(%object@"."@%funcName@"("@%blah@", "@%blahAgain@");");
Anymore, which just looks ugly in the scripts and hard to read.
#4
03/28/2004 (8:15 am)
Good job :)
Associate Kyle Carter