I want to know the best way to call a function every specific time.
If I base myself to the tutorial https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_main_event_loop you can rotate a item base on the delay of the call so it always spin the same “speed”.
But how to handle IA call that should be trigger every second ?
Thanks
p.s. : I’m brand new to jmonkeyengine and my java is a bit rusty so detail answer is appreciate
What is the best way to call something every X (relative timer like the hello_main_event_loop tutori
Is your AI a central system or lots of little components?
Just keep a counter that increments each frame, then when it is over 1.0f (seconds), run your AI code and reset the counter.
Do this in a control.
[java]
MyAIControl extends AbstractControl{
private float counter = 0;
private final float duration = 1.0f;
protected void controlUpdate(float tpf) {
counter += tpf;
if (counter >= duration) {
// do your AI stuff
counter = 0;
}
}
[/java]
@Sploreg said:
Just keep a counter that increments each frame, then when it is over 1.0f (seconds), run your AI code and reset the counter.
Do this in a control.
Well that would depend on how "busy" the AI is. You may well want to run the AI in a separate thread and then only act on them in the rendering thread...
@zarch said:
Well that would depend on how "busy" the AI is. You may well want to run the AI in a separate thread and then only act on them in the rendering thread...
I completely agree with you but the original poster did ask how to trigger the AI once a second.
In truth, basing the AI on the render thread timing may be a bad idea in the long run.
Yes I meant it just as a launch of the AI code, which then often runs on a separate thread.
But really, AI usually doesn’t need to be absolutely precise when it runs, give or take half a second would be the absolute worse case and not a big deal when it comes to AI (in the most general AI scenario).
Don’t worry about the exact timing too much; it won’t be exactly 1 second, but close enough.