Game countdown timer that works with BJS


Im trying to find a simple timer that I can implement into my PG so that players can feel that they have a patch of time to finish the game I looked into this page:
Handle Events with Observables - Babylon.js Documentation and saw a few but wanted to get feedback. Its the last piece of the puzzle(as far as the code part). Thank ya

:heartpulse: :heartpulse:,

You could just use setInterval if that solves your issue?

I must admit I have no experience with Babylon GUI so don’t judge me for that part :stuck_out_tongue:

1 Like

I just realised that maybe you did want to use observables specifically. Here’s how I’d go about doing that:

Specifically here I’m using the scene.beforeRenderObservable to update the count variable every tick, subtracting the delta time between frames. Then just round the number and set it as the text in the GUI element.


This is neat and THANK YA SO VERY MUCH :cupid:(dont worry about the GUI Im still figuring it out as well :laughing:

Question: How do I get it to stop at 0? I noticed that it runs from positive numbers to negative…how do I get it to stop at 0(text saying times up or completed). Tried changing it but nothing happened.

BTW apologies for late response…work got in the way

To get it to stop at 0 you’d just put a check on it before changing the GUI.

if (count <= 0) {
    count = 0;

See here:

To say “Time’s Up” or something similar just change the text inside that if block and wrap the other part in an else. You might have to change the width of the label to accommodate it.

1 Like

Thank ya Bunches​:heart: :blush:

1 Like