Game Development Community

Extending PlayerData

by Andrei Nadin · in Torque Game Engine · 10/22/2005 (3:18 pm) · 5 replies

Hi,

I am trying to add a function to a playerdata datablock, so that I can make calls like player.takeDamage();, however I am getting problems

I have ...

datablock PlayerData(MyPlayer)
{
blah blah blah (removed to save posting space)
}

then i have added my own after this ...

function MyPlayer::takeDamage(%this, %obj)
{
echo ("Ouch!);
}

Unfortunately when I load the mission I get:

>>>> Advanced script error report. Line 1294
>>>> Some error context, with ## on sides of error halt:
function MyPlayer::takeDamage(%this,%obj)
{
^echo ("Ouch!);
##
##}
>>>> Error report complete

I've searched the forums, but can't figure out what I am doing wrong.

Please help!

#1
10/22/2005 (3:38 pm)
If what you typed was exactly what you have in your script...

datablock PlayerData(MyPlayer)
{
blah blah blah
}

needs a ';' after the closing brace. ie...

datablock PlayerData(MyPlayer)
{
blah blah blah
};
#2
10/22/2005 (3:43 pm)
>> echo ("Ouch!);

Closing quote is missing:

echo ("Ouch!");
#3
10/22/2005 (5:13 pm)
Also, the implication of your code suggests that you are calling it from a Player classed object, not a PlayerData classed object..."myPlayer" suggests that the object indexed by %myPlayer isn't a datablock, but a Player instead.

Once you fix the typo mentioned above, be on the lookout for what I mention: it may mean that you want to call it via %myPlayer.getDataBlock().takeDamage();, and change "MyPlayer" to MyPlayerData, or whatever is the name of your datablock class.

As well, there is actually a pre-defined console method on ShapeBase (of which things like Player are derived) called "applyDamage()", which may be what you are looking for afterall! Calling the ConsoleMethod applyDamage() from script both performs the call to the ::applyDamage() C++ method, as well as executing a callback to the script based ::onDamage() method, which should handle most, if not all of your needs.
#4
10/23/2005 (4:13 am)
Hi again,

Thanks muchly for the pointer on the " - can't beleive I couldn't see that!

Stephen - the takeDamage function is just an example I was using as my first attempt to extend the PlayerData. What I am trying to do is to understand how to write these things!

Here is my goal:

%player = new Player() {
dataBlock = MyPlayer;
};

%player.SomeFunctionCall();

With the small " problem corrected, the script now compiles but attempts to call

%player.takeDamage();

return that the function could not be found (or words to that effect). I assume you are going to say that I will need to call %player.getDataBlock().takeDamage(); ... but how do I make it work like above?
#5
10/23/2005 (2:10 pm)
Hurrah, I got it working - realised what I was doing wrong ... i needed to do player::functionname ... now im on my way! Thanks for the pointers though ... and that getDataBlock() function looks very useful!