Game countdown timer that works with BJS

Hey!!

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

https://www.babylonjs-playground.com/#0XEE49#27

:heartpulse: :heartpulse:,
Mekaboo

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:

https://www.babylonjs-playground.com/#0XEE49#29

1 Like

I just realised that maybe you did want to use observables specifically. Here’s how I’d go about doing that:
https://www.babylonjs-playground.com/#0XEE49#30

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.

2 Likes

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.

https://www.babylonjs-playground.com/#0XEE49#31

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: https://www.babylonjs-playground.com/#0XEE49#32

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