Game Development Community

Preload and Con::executef

by James Steele · in Torque Game Engine · 08/04/2008 (11:31 am) · 6 replies

In order to setup some aspects of a datablock, I call a script function from the preload method in the datablock for some custom vehicle setup code. After the script function is called, I then proceed to do some additional general setup code inside of preload().

The problem is that when preload is called from the server, Con::executef doesn't appear to call the script function.

If my update code relies on data being setup from this script function, will this cause a problem on the server side of things? Am I correct in thinking that the server and client copies of the datablock values should match each other?

#1
08/04/2008 (12:18 pm)
@The problem is that when preload is called from the server, Con::executef doesn't appear to call the script function.

Did you define your script side "onPreload" in a server side script file or a client one? If you want it to exist for both you should put it in a script file in common that gets executed for both. But normally datablock methods only exist and are only used by the server.

@Am I correct in thinking that the server and client copies of the datablock values should match each other?

Is this a new datablock object that you created or one that already exists, like shapebasedata, etc? Most datablocks have their pack/unpackUpdate setup so that their static fields are networked from server to client. But the same is not true of dynamic fields. If you aren't sure, look at the datablocks pack/unpack update methods and you can see exactly what data is networked.
#2
08/04/2008 (12:44 pm)
Thanks for that James (my, that feels strange addressing somebody with the same first name!)

I defined my script function (it's actually called "setup") in the server side scripts. I traced into the executef method, and it just returns because the script namespace entry seems to be null when the server calls preload. It seems to be fine when the client calls it, even though the script is in "server".

I've packed all of the non-dynamic data ok, it's just stuff that points to other datablocks that I need to setup, plus a couple of other values.
#3
08/04/2008 (12:50 pm)
Hmm, I believe preload get called before onAdd, therefore your datablock has not yet been registered with torque and does not have a namespace linkage. Although datablocks do not generally override/use onAdd they can as objects derived from simObject, and that might be a better place to put your initialization code.
#4
08/04/2008 (1:04 pm)
OnAdd() is called before preload, so this doesn't work either I'm afraid!

I think that I will just have to do what I want in a different way. As it stands, I have the info for setting up one object that uses two or more datablocks as a structure, and the values in this structure are set via a console function - very similar in the way that the wheels are setup when the car is spawned in car.cs.

Instead, I may have to creat a console datablock that points to these objects, which in turn is referenced in the vehicle data block. It's a bit messy and more convoluted than I had in mind, but I guess I can live with that. I just hope the poor soul setting up vehicles can live with it too :)
#5
08/04/2008 (2:42 pm)
Hmm sounds like you might already have a solution in mind, but just to throw out a few other ideas...

What happens if you call executef on setup at the end of your datablocks onadd, after parent::onadd? Is the namespace entry still null?

Also, could the script file in which you define your setup method possibly be getting executed after your datablock is created?
#6
08/05/2008 (1:16 am)
@What happens if you call executef on setup at the end of your datablocks onadd, after parent::onadd? Is the namespace entry still null?

This is what I did, and no; the namespace is not yet setup at this point. Keep in mind that onAdd() is called before preload(), so the result is the same no matter where it put my executef in the onAdd() method.

@Also, could the script file in which you define your setup method possibly be getting executed after your datablock is created?

I want the script file to be called after the datablock is created, and this is the expected behaviour of calling the function from preload(). At this point, it is perfectly valid to assume that the member variables that are setup in script have already been initialized from the script initialisation or unpacked from the server.

The mechanism for calling preload() seems to work differently for the server, compared to the client. On the server this happens before the object is linked properly, and on the client this happens after. Or atleast this is how I understand it after tracing through the source to see what the differences were.

My solution seems to work ok, I just hadn't tried setting up/transmitting object references with Torque before, but I nailed it first time after looking through the wheeledVehicle.cpp file. It's maybe not as neat as being able to call some console functions to set a wheel tire or spring which gives me some additional error checking and reporting, but it works and it's not SO much of a pain to setup additional datablocks to encapsulate some of the more complex structures.

Thanks for all of your help though James.