Game Development Community

Dynamic Field Problems

by Zach Broderick · in Technical Issues · 12/17/2007 (9:16 pm) · 3 replies

After a bunch of reading I cannot find any information on this.

Now I'm still a complete noob so bear with me.

In my game I need to create a series of scripted objects, all exactly the same, except for one variable that will be different for each object.

Currently the only way I know how to create scripted objects is to create a datablock from StaticShapeData.
(Is this the right thing to do? Are there other(better) ways of creating a scripted object?)
datablock StaticShapeData(Object)
{
   category = "misc";
   shapefile = "~/data/shapes/object/object.dts";
   canSaveDynamicFields = "1";
   variable = "value";
};

Now I know that Dynamic Fields will allow me to have a variable unique to each object. The problem is, setting these Dynamic Fields for each object.
Another hitch would be that I would like to have a default value for the Dynamic Fields to be set to, in case it is not manually set.

How would I do this?

Currently I'm using onAdd() to create these Dynamic Fields for every object created from the datablock
function Object::onAdd(%this, %obj)
{
   %obj.variable = %this.variable;
}

But as you might have guessed this means that when I close my game and reopen it the dynamic variables are set back to what I have set as the default in the datablock.

#1
12/18/2007 (3:42 am)
If you don't want the objects to be a part of the scene such as player, static shape... Then have a look at ScriptObject.

As for "defaults", there is a copy option for data blocks which you can use as follows (Note this is just copying datavalues, do not confuse it's purpose with inheritance)

datablock StaticShapeData(BaseObject)
{
   category = "misc";
   shapefile = "~/data/shapes/object/object.dts";
   canSaveDynamicFields = "1";
   variable = "value";
   SomeValue = "test";
   SomeOtherValue = "other test";
   defaultVar = "20";
};

Then you can make an object that will have ALL the fields from the above datablock and override the defaultVar with a new value, lets say 30.

datablock StaticShapeData(MyObject : BaseObject)
{
   defaultVar = "30";
};

So echo MyObject.defaultVar would give 30, and echo( MyObject.someOtherValue ); will give "other test".

As you mention, these are dynamic variables and will only be accessible from script. Look at initPersitFields if you also want to use these in the engine.

You can do the same copy option for ScriptObjects, which also removes the need for using datablocks. You can also use "class" and "superClass" to add additional namespaces to hang methods off of.

There should be plenty of examples of using ScriptObjects with class and superclass if you have a search through the forums.
#2
12/18/2007 (7:21 am)
Object that are initialized from datablocks can have additional dynamic parameters of their own.
You set the datablock (tells what "kind" of object it is, and can provide script methods), then can add any additional object-specializing info to just the object itself ("shoot me and get 3 points!").

So if you need some small amount of miscellaneous settings per object, you can often use only one datablock but with these additional per object fields. You can use these object fields inside datablock methods as long as you pass in the object as well as the usual %this (the datablock ID) as parameters to the datablock method. (See the Server CheckPoint.cs for the racing demo as an example.)

A minor but possibly important point: dynamic variables are only available from Server script. They aren't sent over the network. So you normally shouldn't be using this type of information in the GUI, for example.
#3
12/29/2007 (9:38 am)
I solved this issue by using a database. All of my objects are generated from the database and upon exit are saved back to the database. Of course to get database support into Torque you have to use C++ to do it. But there are some excellent resources for this.

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5531
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5531