calling a function saved in a varible
by Joseph Bosch · in Torque Game Builder · 02/26/2013 (8:56 am) · 5 replies
I have a class which saves a function in a variable IE.
ability[0] = "grenade();";
I followed the structure as the command variable in most GUI objects but can not figure out how to actually call the function saved in this variable;
ability[0] = "grenade();";
I followed the structure as the command variable in most GUI objects but can not figure out how to actually call the function saved in this variable;
#2
If you have a string that does not evaluate to valid code you will see the message "parse error" appear in the console with no context at all, so it helps to debug these carefully.
Um, unless you are working engine-side. Then you would probably want to use
02/26/2013 (9:50 am)
If you have a field defined as you have shown...%object.ability[0] = "grenade();";then this
eval(%object.ability[0]);should call that function.
If you have a string that does not evaluate to valid code you will see the message "parse error" appear in the console with no context at all, so it helps to debug these carefully.
Um, unless you are working engine-side. Then you would probably want to use
evaluate(ability[0]);but I haven't really played with that much and it probably requires even more careful debugging.
#3
02/26/2013 (10:03 am)
In TGB you can assign a GUI element a Command inside it's GuiCtrl, either through the Gui Builder, or through direct script. And then call upon that function, just like any other://Example 1
new GuiBitmapButtonCtrl() {
Command = "doThis();";};
function doThis()
{ echo("I did it!");}
//Example 2
new GuiBitmapButtonCtrl(myGui) {
Command = "myGui.doIt();";};
function myGui::doIt()
{ echo("I did it again!");}
#4
02/26/2013 (12:01 pm)
My apologies, after re-reading your question, I believe Richard was on the right track with eval, so you'd have to set up something like this:myClass.ability[0] = "grenade();"
myClass.setAbility();
function myClass::setAbility(%this)
{
%count = myClass.getAbiliyCount();
for(%i=0;%i<%count;%i++)
{
eval(%this.ability[%i]);
}
}
function myClass.getAbilityCount(%this)
{
%count = 0;
while(true)
{
if(%this.ability[%count] $= "")
return false;
else
%count++;
}
return %count;
}
function grenade()
{
echo("Boom!");
}
#5
03/01/2013 (12:23 pm)
thank you that works
Employee Michael Perry
ZombieShortbus