Memory allocation question

Scripting questions, discussions, etc
5 posts Page 1 of 1
Hodo33
Posts: 104
Joined: Fri Dec 29, 2017 4:34 pm
by Hodo33 » Tue Dec 10, 2019 3:29 pm
If I have an object say simple static shape
datablock StaticShapeData(BoxA)
{
shapeFile = "art/shapes/boxes/boxa.dts";
};

Then in the onAdd

function BoxA::onAdd(%this,%obj)
{
%obj.type = 22; << memory for the object plus this extra data gets allocated at the time of creation ?
}

so I create one in the game world
new StaticShape(someBox) {
Data left out for brevity...
}

Later in the game I need more data about the object so I do something like this

someBox.color = blue;

Is the memory for this object freed then reallocated with a new size?

I am getting a crash in game and I think it has something to do with poor coding techniques on my part.
If I know before hand that I will need a color data should I add it into the datablock description or would it be ok to add in with the ::onAdd(%this,%obj) at the time of creation ?
Happenstance
Posts: 104
Joined: Sat Apr 11, 2015 9:08 pm
by Happenstance » Tue Dec 10, 2019 9:31 pm
In your examples '.type' & '.color' are known as 'dynamic fields' in the world of Torque.

Internally each object has a SimFieldDictionary (basically a hashmap) that stores a key:value pair for each 'dynamic field' the object has. When you add a new dynamic field (e.g. %obj.myNewVar = 15) the object itself isn't reallocated, a new entry is simply added to its field dictionary.

Adding new dynamic fields can happen in onAdd() or any other time. If you define a dynamic field as part of a datablock definition I believe you'll be adding it to the actual datablock, not the object itself so to access it you'd need to do something like: %obj.getDataBlock().myNewVar = ... This method also has some networking implications. Datablocks are downloaded to clients once so dynamic fields in datablocks is a bit of an odd choice.
Azaezel
DEVGRU
Posts: 495
Joined: Tue Feb 03, 2015 9:50 pm
 
by Azaezel » Tue Dec 10, 2019 11:50 pm
This method also has some networking implications. Datablocks are downloaded to clients once so dynamic fields in datablocks is a bit of an odd choice.
It should be noted that new dynamic fields aren't networked (see ::pack/::unpack in the sourcecode for how to actively network a value if it needs to be known on the client), but other than that, yeah, engine'll slap a var on any time you choose to set one, and evaluate to 0/"" if you look one up you haven't set yet.
Hodo33
Posts: 104
Joined: Fri Dec 29, 2017 4:34 pm
by Hodo33 » Wed Dec 11, 2019 3:17 pm
If I made the var part of the datablock declaration rather than creating it in the onAdd() would it then cross network? and the object allocation will be one contiguous block rather than having to append a pointer?

datablock StaticShapeData(BoxA)
{
shapeFile = "art/shapes/boxes/boxa.dts";
someNewVar = "22"; <<< putting this here more network friendly ?
};
Azaezel
DEVGRU
Posts: 495
Joined: Tue Feb 03, 2015 9:50 pm
 
by Azaezel » Wed Dec 11, 2019 5:32 pm
If I made the var part of the datablock declaration rather than creating it in the onAdd() would it then cross network? and the object allocation will be one contiguous block rather than having to append a pointer?

datablock StaticShapeData(BoxA)
{
shapeFile = "art/shapes/boxes/boxa.dts";
someNewVar = "22"; <<< putting this here more network friendly ?
};
afraid not. there you do need to turn to the source and insert lines comparable to

https://github.com/GarageGames/Torque3D ... e.cpp#L127

https://github.com/GarageGames/Torque3D ... e.cpp#L134

note order matters so pack it as you'll be unpacking it.
<pack/unpack>Datas for the datablocks, while <pack/unpack>Updates for a given object instance.

You could of course run a commandtoclient/clientCMD combo to pass along any arbitrary values you want to anything in particular, though do be aware that object IDs aren't the same on client and server side so that'll fail without interpretation methods.
5 posts Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests