Game Development Community

Script Execute calls OnAdd/OnData not working

by William Harding · in Torque Game Engine Advanced · 04/10/2006 (2:36 am) · 2 replies

First off I have the latest head version...

I've been working with starter.fps for a couple days now and discovered something really weird I can't figure out. It looks to me like an engine bug, frankly.

So I added a health kit to the mission (HealthKit).

And wanted to make a function so it would do something on init...

function HealthKit::onNewDataBlock(%this,%user)
{
echo("Health kit data block added.");
if (%user.client)
{
%user.client.player.setExternalRenderFocus(%this);
}
}

All very well and good, but it's never called. My echo never comes up...

But here's the rub. It -works- if I populate the C code with echo messages!

They're commented out here, but you can see two of them that I added in... (GameBase is a parent of Item.)

void GameBase::scriptOnNewDataBlock()
{
//Con::printf("Script New Data Block");
// Script onNewDataBlock() must be called by the leaf class
// after everything is loaded.
if (!isGhost())
{
//Con::printf("Script New Data Block 2");
Con::executef(mDataBlock,2,"onNewDataBlock",scriptThis());
}
}

I have added comments to the execute function this is tied to...

const char *execute(SimObject *object, S32 argc, const char *argv[])
{
Con::printf("Execute");
static char idBuf[16];
if(argc < 2)
return "";
if(object->getNamespace())
{
dSprintf(idBuf, sizeof(idBuf), "%d", object->getId());
argv[1] = idBuf;

StringTableEntry funcName = StringTable->insert(argv[0]);
Namespace::Entry *ent = object->getNamespace()->lookup(funcName);

Con::printf("Script function call %s:%s",object->getName(),funcName);

----

As I discovered, it attempts to call script functions through this gateway above.

If I put in enough echo messages then it will call the script properly! Argh! I'm ready to throw in the towel, call the console messaging an unreliable, broken system, and find some other way of doing what I want.

So what gives? Does populating the console give it enough 'lag time' to be able to run the function? Am I going insane? Is there a better way to call back script functions or know when something is being initialized and call some init code?

Please note the object was placed in the mission in at the base level. (Not in an object subgroup.) So it spawns automatically and I can see it spinning in the level right next to my spawn point. Mostly to taunt me.

#1
04/10/2006 (2:49 am)
(Of course I'll debug this more heavily tomorrow, as I need those functions working. Late night. :)

Edit: Couldn't resist... Think I got it!

The object is still considered to be a ghost at this point, I'm guessing because it was just instantiated from the mission and there hasn't been time for it to populate properly. Guess I don't understand the 'ghost' thing well enough, but I'm betting it's multithreaded code operating in the background. I'm just passing right through the code unless I add enough of a delay beforehand to allow it to execute, I think. Which is done by populating it with console print functions.

So... that begs the question why have OnAdd if I can't rely on it.

Must be a better way of doing this.
#2
04/10/2006 (1:52 pm)
Edit: Closing out this thread as I found it was -my- mistake! Anyway, not sure it will do what I want it to, anyway, so have to find another method.

I read the log incorrectly and debugging proved there was more than one item being substantiated. I was looking at the wrong one, not realizing that items are spawned on the server way before the map is fully loaded and started.

Since the player executes way after the items are created, my initial plan won't work.