Hodo33 Posted December 10, 2019 Share Posted December 10, 2019 If I have an object say simple static shapedatablock 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 worldnew StaticShape(someBox) { Data left out for brevity...}Later in the game I need more data about the object so I do something like thissomeBox.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 ? Quote Link to comment Share on other sites More sharing options...
Happenstance Posted December 10, 2019 Share Posted December 10, 2019 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. Quote Link to comment Share on other sites More sharing options...
Azaezel Posted December 10, 2019 Share Posted December 10, 2019 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. Quote Link to comment Share on other sites More sharing options...
Hodo33 Posted December 11, 2019 Author Share Posted December 11, 2019 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 ?}; Quote Link to comment Share on other sites More sharing options...
Azaezel Posted December 11, 2019 Share Posted December 11, 2019 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 tohttps://github.com/GarageGames/Torque3D/blob/development/Engine/source/T3D/staticShape.cpp#L127https://github.com/GarageGames/Torque3D/blob/development/Engine/source/T3D/staticShape.cpp#L134note 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.