Updating nested game objects - update child before parent or parent before child?
#1 Members - Reputation: 348
Posted 03 July 2012 - 07:57 PM
#3 Members - Reputation: 786
Posted 03 July 2012 - 10:15 PM
#4 Members - Reputation: 4604
Posted 03 July 2012 - 11:13 PM
I think that this is quite common, you often have an object attached to its parent (member->party, item->npc,guild->town) which acts depending on the state of the parent.where children have their position and velocities relative to and dependant on the parent
Best to encapsulate the control of the update in the object itself, a standard update method would look like this(pseudo code):
[source lang="java"]object::update(){ // call internal update this->internalUpdate(); for each child do child.update(); end}[/source]
When you wish to update an object class in an other way, just overwrite the update method like this:
[source lang="java"]SpecialOjbect::update(){ // call super update Object::update(); // do some post processing ...} [/source]
My game: Gnoblins
Developer journal about Gnoblins
Small goodies: Simple alpha transparency in deferred shader
#5 Members - Reputation: 1138
Posted 04 July 2012 - 02:56 AM
#6 Members - Reputation: 487
Posted 04 July 2012 - 02:36 PM
#7 GDNet+ - Reputation: 1719
Posted 05 July 2012 - 01:12 AM
I suggest to consider the idea of dropping extensive update process. I once read an article suggesting to not update at all.I have a system where game objects can contain other game objects as their children, and have an update(float DeltaTime) function in my game objects which does the per-frame updates (ticks, or whatever your favorite engine calls it). I'm wondering whether it makes more sense to update child objects before their parents or the other way around
I don't think that's feasible but I can sympathize with the concept and I worked out a system which is somewhat compliant with this concept. What I have done in my system is to have a single place when update(deltaTime) will be issued. There will only be one update call per tick. The user (script) is in charge of ensuring the information is propagated to the objects it needs, in any order it needs.
Exception are what I call live objects, similar in concept to the volatile C keyword. So far those objects have the following properties:
- They are architecturally simple. They don't have subcomponents and most cannot even be used as subcomponents albeit all can be instanced as objects in the script.
- They are guaranteed to change atomically before each script is run and guaranteed to be consistent across different script invocations in the same tick.
- They require (mostly) quite some work to be implemented in script.
- They are typically backed by native data structures (some even take 0 bytes in script heap memory).
Now, in the case of "natively nested" components, such as the limbs of a ragdoll, you don't update them. They physics API updates them for you and pours out the transforms to your system. Albeit it appears reasonable the parent will be synced first, I specified no order is guaranteed.
So in short I suggest to consider not enforcing an order. But I'm interested in this discussion so please make specific examples. As I learned the hard way, nobody will get anywhere as long as generic objects are considered against arbitrary relationships.
#8 Members - Reputation: 1138
Posted 05 July 2012 - 07:12 AM
Ideally, the physics should work with 2 states: current that is read only and future that is write only; this helps to make use of multiple CPUs (by making the physics independent of the order of updating). It may be useful to separate e.g. AI updating from physics updating. Ahh, and also, if physics gets different values depending to the order (e.g. two masses connected by a spring, and result depends to which is updated first), that is rather bad and makes it difficult/impossible to ensure conservation of momentum (and energy).Dmytry, that's usually what I do in a clear owner->child type of a scenario, but it may not work in the aforementioned physics attachment system, since whatever function goes on updating all your objects would probably update the child already. Hence why i said, it depends on the nature of the relationship.
Edited by Dmytry, 05 July 2012 - 07:15 AM.






