Game Development Community

Scripting problem

by Drethon · in Torque Game Engine · 03/17/2009 (4:05 pm) · 6 replies

Could someone help a scripting newb out? I'm trying to add some basic actions through scripts as shown at the bottom, I've gotten everything to work right up to the call to Plant (the farming text is properly echoed) but then I get the following error in the log:

DarkAge.RPG/server/scripts/player.cs (1005): Unable to find object: '' attempting to call function 'Plant'


function Player::Plant(%this)
{
echo("Planting!");
}

function Player::AreaAction(%ZoneType)
{
echo("Actioning"+%MyZone.ZoneType);
if ( %ZoneType != 0 )
{
echo("Zoning");
if ( %MyZone.ZoneType == "Farm" )
{
echo("Farming");
%this.Plant(%this) ;
}
}
}

function serverCmdAreaAction(%client)
{
echo("Area Action Executed");
%client.getControlObject().AreaAction(%client.MyZone);
}

#1
03/17/2009 (4:39 pm)
function Player::AreaAction(%ZoneType)

should be

function Player::AreaAction(%this, %ZoneType)
#2
03/17/2009 (5:24 pm)
Perfect, I thought %this was a self defined variable but I take it I have to pass in what the character object is?
#3
03/17/2009 (6:24 pm)
%this is self defined if you aren't going to use it in the function..but since u are using it it is best to define it just in case
#4
03/18/2009 (10:40 am)
%this is a little confusing.

when you call a method function of an object, torquescript automatically inserts the object itself as %this as the first argument. you include %this in the method definition, but not when you call it. so in your code example, the second %this in "%this.Plant(%this);" is un-needed.

so eg,
function player::foo(%this, %param)
{
   echo("i am" SPC %this SPC "and i got param" SPC %param);
}

%somePlayerObject.foo(123);

would display something like "i am 8383 and i got param 123".


interestingly, if you want (although i don't know why you would)
you can change the calling syntax so that you're calling a function in a namespace rather than an object method, and in that case you would pass in %this. that is, calling "%somePlayerObject.foo(123)" is equivelant to calling "player::foo(%somePlayerObject, 123)".

welcome to torquescript!
#5
03/18/2009 (12:35 pm)
Thanks, that helps with the "I kinda understand what is going on" issue. Now to plow on!
#6
03/18/2009 (5:09 pm)
like orion said, it's a bit confusing, but it's safe to always add the "%this" as first parameter especially if you use one or more extra parameters.