Game Development Community

Timers optimization

by Bojan Vukojevic · in Torque Game Builder · 03/11/2011 (11:24 am) · 6 replies

Hello guys,

I have a question about best usage of timers. I have an object that has health points and gets damaged from time to time. There is a repair button that is disabled if the object has 100% health and enabled otherwise. How to check when to disable/enable the button? The easiest answer is to make a timer and check health every second for example. If lower than 100% enable button else disable button. This sounds easy enough and should work good, maybe the check interval should be 500ms or something but i doubt the player will notice if it is one second.

The thing is i'm afraid that this maybe is not the best solution. I try to use timers as rarely as possible i do not want to stress the engine too much. Can someone suggest a proper way to do stuff like this? Is there a way to create an event that will trigger itself when some value changes? What is the best practice performance vise?

While I'm here how to disable gui button? I can change the image and disable the on click command but is there anything built into the engine for this?

Thanks for your help :)

#1
03/11/2011 (2:14 pm)
setActive(false) should disable a GUI control. Check TDN for all the methods supported by GUI controls.

Why not make the functions which deduct/increase health update the GUI? No need to have scheduled timers when you can update only as necessary :)

Also look into behaviours. They're always useful to help you give wildly different objects some similar functionality. without implementing it within each class.
#2
03/12/2011 (11:53 am)
Hey Ronny,

Thanks for the answer this is probably the way i will do it at the end. I wanted to do it with timers because I had a few places where i did damage, now i will just do something like DoDamage(object, amount) and put everything there. Also setActive is the way to go for button disabling thanks for that also.

One more thing. I was thinking about behaviors but couldn't find a way to use them in this particular problem. Can you please give me an example? No code just logic in few words.
#3
03/12/2011 (1:29 pm)
I think some of the example behaviours on TDN would be best to look at. There are some for taking and dealing damage, for instance. A behaviour can also add collision behaviour to different objects, so you could have a player ship class, an enemy ship class and attach the damage/collision behaviours to all, with setup inside those configuring all the collision settings on objects being added to the scene.

Behaviours also let you extend the editor, so when you add a behaviour to a scene object you can also get dropdowns, text fields and checkboxes to add more options. The player controls could even be a behaviour, so you basically only have a ship class with basic collision/damage code (reversing the other example :).

When I use sprites for buttons they're also useful. A ClickableButton behaviour with a text field in the editor to configure the function to call does the trick. A non-configurable behaviour with pre-determined settings can also be useful for tedious operations, like spawning enemies. Just clone the objects and set the behaviour, add to scene, done.
#4
03/12/2011 (1:56 pm)
Heh this is funny :) I love talking with someone about a problem because there is a chance that something will come to mind that didn't before, and that is exactly what happened.

I know about behaviors i use them from time to time, i kinda prefer to do everything that i can in torsion and i create a lot of objects in code. I skipped behaviors this time for damage dealing because i do not use collision always to do damage and behaviors are triggered by events that are programed into engine, collisions and similar. But while i was typing the answer to explain my code i realized that whenever i deal damage i use some event, collision, mouse click or triggers. I think i will do some restructuring :)

Can you please answer me just one more question. When i create new objects i always create them brand new ie i do not clone them. What are the benefits of clone? Any speed increase or less memory?

Thanks
#5
03/12/2011 (3:46 pm)
Cloning an object copies any custom variables etc. you've attached to them. They also behave the right way if you have colliding objects clones, from what I've seen, so there's very little house-keeping to manage new ones.

You might want to create one object the normal way, clone it up to the max number of simultaneously expect objects and stick them in a SimSet for speed and efficiency. But cloning shouldn't make any difference to memory/speed. It just saves typing :)
#6
03/12/2011 (5:02 pm)
Cool i was afraid i was losing some performance by creating only new objects. Thanks for all the help m8 :)