SimObjects and Threading
by Demolishun · in Torque 3D Professional · 08/22/2012 (11:24 pm) · 2 replies
I have a TerrainBlock object (based upon SceneObject which is based upon SimObject). This object is used to manipulate terrain data. Now, this object may exist inside or outside the Sim at any time. I know this is true because I have tested this. This is also how I intend to use the object. I want to be able to manipulate the object in either place.
The hard part. Inside or outside a Sim I want to be able to make the object "hands off" to the rest of the codebase. I don't want it to know it even exists if possible. The reason is I want to make functions that run in worker threads to manipulate the data. While the object is being tweaked by the worker thread I don't want anything messing with it for obvious reasons.
Now I know I had to tell the engine this object exists for it to show up in the Sim. What I am hoping is that I can switch this on and off as needed in script. So any experience with making the object not accessible from the script would probably work.
Does anyone have experience with this?
Here are the functions I think might do what I want:
The hard part. Inside or outside a Sim I want to be able to make the object "hands off" to the rest of the codebase. I don't want it to know it even exists if possible. The reason is I want to make functions that run in worker threads to manipulate the data. While the object is being tweaked by the worker thread I don't want anything messing with it for obvious reasons.
Now I know I had to tell the engine this object exists for it to show up in the Sim. What I am hoping is that I can switch this on and off as needed in script. So any experience with making the object not accessible from the script would probably work.
Does anyone have experience with this?
Here are the functions I think might do what I want:
/// Removes the object from the client/server container void removeFromScene(); /// Adds object to the client or server container depending on the object void addToScene();
About the author
I love programming, I love programming things that go click, whirr, boom. For organized T3D Links visit: http://demolishun.com/?page_id=67
#2
Hmmm, I will start with removing from the sim. I wonder how many awesome glorious game engine crashes I will cause...
08/22/2012 (11:39 pm)
If the SimObject disappears and a function is called against the object then it can schedule the action by placing the action in a stack. When the scheduled function runs it checks for the object, if it exists it applies the first function from the stack, if not it reschedules. There could be some timeout values to prevent this from occurring forever, but it could allow objects to be manipulated in other threads in the background. There might be a general use case here for background processing of game objects in threads. It might make sense to include this functionality in the SimObject class so that objects don't have to removed from the Sim.Hmmm, I will start with removing from the sim. I wonder how many awesome glorious game engine crashes I will cause...
Torque Owner Demolishun
DemolishunConsulting Rocks!
/// Register an object with the object system. /// /// This must be called if you want to keep the object around. /// In the rare case that you will delete the object immediately, or /// don't want to be able to use Sim::findObject to locate it, then /// you don't need to register it. /// /// registerObject adds the object to the global ID and name dictionaries, /// after first assigning it a new ID number. It calls onAdd(). If onAdd fails, /// it unregisters the object and returns false. /// /// If a subclass's onAdd doesn't eventually call SimObject::onAdd(), it will /// cause an assertion. bool registerObject(); /// Register the object, forcing the id. /// /// @see registerObject() /// @param id ID to assign to the object. bool registerObject(U32 id); /// Register the object, assigning the name. /// /// @see registerObject() /// @param name Name to assign to the object. bool registerObject(const char *name); /// Register the object, assigning a name and ID. /// /// @see registerObject() /// @param name Name to assign to the object. /// @param id ID to assign to the object. bool registerObject(const char *name, U32 id); /// Unregister the object from Sim. /// /// This performs several operations: /// - Sets the removed flag. /// - Call onRemove() /// - Clear out notifications. /// - Remove the object from... /// - its group, if any. (via getGroup) /// - Sim::gNameDictionary /// - Sim::gIDDictionary /// - Finally, cancel any pending events for this object (as it can't receive them now). void unregisterObject();