Using Control from other Thread

Hello!

Im new here, reading forums for some time, but now registered.



Im writing some basic AI system, but that system is different Thread - just Runnable object with endles loop.



In this loop i try to control Custom Controls - like change animation, start moving in some direction.



Custom controls have flags - is busy or not. If is busy then AI thread dont alter that control.



All is working fine. To lower CPU usage in AI loop have sleep :stuck_out_tongue:



And have a question: it is this solution good?

My target is to control about 200 Controls.



Tom.

It’s probably not needed to run it all in another loop. But what you can do, and probably should do, is dispatch CPU-intensive calculations to a separate thread, and then have it come back and submit its results back to the game thread https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:multithreading

pathfinding, goal planning, etc.



If your AI is a control, it gets updated every frame anyways. You can have a little sleep counter in there if you want it to not run itself every frame.

In my case ai is not a Control:

[java]public class AIControl implements Runnable{



MobFactory factory;

Random r;



public AIControl(MobFactory factory) {

this.factory = factory;

r =new Random(90);

}





public void run() {

while (true){

try {

Thread.sleep(10);

} catch (InterruptedException ex) {

Logger.getLogger(AIControl.class.getName()).log(Level.SEVERE, null, ex);

}

for (Spatial s : factory.mobList) {

if (!s.getControl(MobControl.class).isLocked()) {

if (r.nextBoolean()) {

s.getControl(MobControl.class).setMobState(MobState.IDLE);

} else {

s.getControl(MobControl.class).setMobState(MobState.WALK);

}

}

}

}

}



}[/java]

I’m always wary of while(true) loops.

Making your AI a control (that is attached to an empty node or something) is the same as running your stuff in a separate thread. Except you get the benefit of being on the game loop and not always having to read/write to the game update loop.

With your separate thread, you will have to sync the factory.mobList (which you would get for free by running it in the game loop). This might end up making it slower. And you will have to be careful if your mobList changes (new mobs, removed mobs, etc).



Realistically, going through each actor and checking its MobState is not expensive. Pathfinding is expensive, and action planning can be too. That’s where you want it to run on another thread.

1 Like

I see.

So better is to attach AI Control for each mob spatial and JME take care of iterating on spatials?

Every AI Control will run logic for one spatial.

There will be no while(true) loop.

Checking control can be in update(), ok.

guessing i know what You talking about.

Thank You very much, let me try this :stuck_out_tongue:

It’s similar to Swing in that all interactions with the world state must be done from the app thread (use app.enqueue to pass messages back otherwise).



However that thread is doing all the rendering so you really really don’t want to slow it down - so any expensive computations (like as mentioned above pathfinding) you can farm off to a separate thread and then enqueue the results back once it is done. The following of the path can be done in the game thread as that is much faster.



https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:multithreading