Jump to content

Memory allocation question


Recommended Posts

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 ?

Link to post
Share on other sites

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites

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 ?

};

Link to post
Share on other sites

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/blob/development/Engine/source/T3D/staticShape.cpp#L127


https://github.com/GarageGames/Torque3D/blob/development/Engine/source/T3D/staticShape.cpp#L134


note order matters so pack it as you'll be unpacking it.

Datas for the datablocks, while 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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...