State callbacks
by John Coyne · in Technical Issues · 02/14/2006 (3:38 am) · 3 replies
Hi
I'd like to store the state of a game unit in a variable and respond to changes in the variable. I'm not sure of the coding method to do this. The most obvious method seems to be to schedule a function every second or so, and check the state of the variable at that point. This method seems a little on the messy side and i'm a bit concerned the game might slow down if i have a reasonably large number of units. Is there a more efficient way?
I'd like to store the state of a game unit in a variable and respond to changes in the variable. I'm not sure of the coding method to do this. The most obvious method seems to be to schedule a function every second or so, and check the state of the variable at that point. This method seems a little on the messy side and i'm a bit concerned the game might slow down if i have a reasonably large number of units. Is there a more efficient way?
About the author
#2
In the Observer Pattern, each observable object (called a subject) registers the observer objects that are interested in "watching" it and subsequently maintains a list of observers Each observer object implements an update method with the same prototype (usually implemented such that the subject can pass the new state information into the update call to the observer directly). Once the subject's state changes, it can be directed to force an update to all of its observers (and this update can happen at any time -- immediately or delayed depending on the need). When the update method is called in the observer, its local understanding of the state information is updated.
This is similar to the Event Pattern without the extra overhead of a bunch of event objects floating around the environment.
Either method is more efficient than the 'check every frame' method as the observer pattern only notifies those units that are observing the data and only when the data changes, and the event model also only notifies the right number of objects, just with the added expense of temporary objects.
02/14/2006 (3:50 pm)
I think what you might be looking for is an observer pattern. It is a one-way, one-to-many relationship.In the Observer Pattern, each observable object (called a subject) registers the observer objects that are interested in "watching" it and subsequently maintains a list of observers Each observer object implements an update method with the same prototype (usually implemented such that the subject can pass the new state information into the update call to the observer directly). Once the subject's state changes, it can be directed to force an update to all of its observers (and this update can happen at any time -- immediately or delayed depending on the need). When the update method is called in the observer, its local understanding of the state information is updated.
This is similar to the Event Pattern without the extra overhead of a bunch of event objects floating around the environment.
Either method is more efficient than the 'check every frame' method as the observer pattern only notifies those units that are observing the data and only when the data changes, and the event model also only notifies the right number of objects, just with the added expense of temporary objects.
#3
Is you variable stored in a datablock ?
if yes :
In your game what is the event to do that ?
what is the trigger ?
collision
damage
zone
...
otherwise : i don't know ( It does not mean that is not feasible )
:)
02/14/2006 (11:36 pm)
"state of a game unit in a variable"Is you variable stored in a datablock ?
if yes :
In your game what is the event to do that ?
what is the trigger ?
collision
damage
zone
...
otherwise : i don't know ( It does not mean that is not feasible )
:)
Torque Owner Jason Farmer
So say for example you want to increment the value of a variable called score on the client object.
You'd have a %score variable in the client but you wouldn't modify it directly.
You'd use functions to do it. Set_Score(NewValue, SomeObject ) and Get_Score() . Of course you could get away without using Get_Score for performance..
Set score would do some additional processing on the client or maybe send messages to the server.. whatever you need.
it's what I'd do, doesn't make it the perfect answer though, there may be a much better way of doing it.
hope this helps.