Hi,
What do I want to achieve?I want to create a Textfield T that listens to a certain variable X. Whenever X's value changes, the T should update its text to the value of X.
ExampleFrame 1:
player.strength = 10 //had the same value in the last frame
// display.tfplayerstrength does nothing
Frame 2:
player.strength = 11
// display.tfplayerstrength detects there is a change in player.strength
display.tfplayerstrength:setText(player.strength)
I hope you get the idea.
What's the problem?In some engines you have a game loop and a graphics loop - both are executed each frame. In the game loop X gets updated, and in the graphics loop T just displays X's value anyway. Sounds bad, because this implies both loops update everything all the time. Contrary to expectation this can be really fast.
Now when you try to do it this way in Gideros, you will have a bad time. Have about 30 or 50 textfields, all updating their text every frame, you get extreme performance problems. So you have to detect when a value has to be changed.
What do I want to avoid?I do
not want to write a controller class that does all the checks like:
did player.strength change? if so, update display.tfplayerstrength.
did player.vitality change? if so, update display.tfplayervitality.
did player.exp change? if so, update display.tfplayerxp.
...
I want a Textfield that's able to check for itself if a variable has changed.
Basically, what I need, is to pass a
variable reference into an object.
I'm thinking along the lines of this:
BoundTextfield = Core.class(TextField)
function BoundTextfield:init(font, text, var)
TextField.init(self, font, text)
self.var = var
end |
But the problem is, when I pass "var", I'm sure it passes the value, and not a reference to the variable itself.
Does anyone know how to achieve what I want?
Comments
Likes: Holonist
Messy code for examination:
in onEnterFrame(), the Textfield checks if the value refObj[refAttr] (for example player["vit"]) has changed, and then updates the text accordingly. Of course "oldVal" also has to be adjusted, or we end up updating the text every frame.
But you may want to only display rounded numbers, while still keeping the original floating point number.
Edit: Then again, this would mean the textfield would get updated when decimals change, but the display value would stay the same (kinda dumb). There's definitely optimization potential, but anyway, getting to this to work already solved a very big issue for me.
just realized what you mean with local objects. Well what would that change?
From my experience it's almost always better to use object-bound variables so you can access them later.
About the local things:
http://lua-users.org/wiki/OptimisingUsingLocalVariables
You won't notice performance improvements until you have "many" objects, but I can tell you that this works for real
of course, the downside is that you lose access on local objects, so you can't always do everything local.
Likes: Holonist