Hi all, I wanna make a timer. For example, I have attached a spatial to the rootNode. After 3 seconds, I want to detach that spatial at the rootNode.
So, is there any efficient way to make a timer? thanks in advance…
Make a control and attach it to that spatial. Set 2 local vars for that control: float time, float timeCount.
In the update method increase timeCount with ‘tpf’ (time per frame). Then test if timeCount >= time. If so, then perform your action (detaching the spatial). It should give you a good enough timer implementation for what you need.
Or for longer term stuff just use a ScheduledExecutorService and in the callback for that enqueue something into the rendering thread (or set a flag the rendering thread reads, or whatever) to do the work.
@zarch said:
Or for longer term stuff
.. "longer term" in terms of processing, spawning a thread just to wait for something isn't really ideal.
@normen said:
.. "longer term" in terms of processing, spawning a thread just to wait for something isn't really ideal.
Unless you do it a lot. The scheduled executor will be much better/more efficient at sorting and running hundreds of timed events than hundreds of idle counting controls would.
But it doesn't sound like the case here.
@pspeed said:
The scheduled executor will be much better/more efficient at sorting and running hundreds of timed events than hundreds of idle counting controls would.
Yeah, I was more talking about the various examples we had on the forum here with people spawning threads just to not block the update loop. And the scheduled executor also wouldn't spawn lots of threads if most of its tasks are waiting.
Yep, it’s situational.
Longer term I meant more like stuff that has to happen every hour (where you wouldn’t want to keep a counter running in a control for an hour) - or as mentioned above if you have a lot of timer events all happened at different times.