Headless mode problem

hi,
Pleas someone help, I was making game client server architecture. it was working, player can move in all direction but on server i was using :

getGameWorldById(worldId).start();

And i decided use:

getGameWorldById(worldId).start(JmeContext.Type.Headless);

And it’s gone terrible, nothing was working, simpleUpdate looping each 5-15 seconds, just disaster. Phisics is not calculating anymore.
sometimes simpleUpdate loop runs fast and after while stops and go slow.

[java]
@Override
public void simpleUpdate(float ftp) {

    for (Player pl : allRoomPlayers) {
        System.out.println(pl.getName() + " zaidejas " + pl.getPlayerController().B_FOWARD);
        Vector3f modelFowardDir = pl.getPlayerController().getPlayerNode().getWorldRotation().mult(Vector3f.UNIT_Z);

        //go foward or backward

        walkDirection.set(0, 0, 0);
        if (pl.getPlayerController().B_FOWARD) {
            System.out.println("foward pressed ");
            walkDirection.addLocal(modelFowardDir.mult(14f));                 
            pl.getPlayerController().getPlayerControl().setWalkDirection(walkDirection);
        } else if (pl.getPlayerController().B_BACKWARD) {
            walkDirection.addLocal(modelFowardDir.mult(pl.getSpeed()).negate());              
        } 
     
        pl.getPlayerController().getPlayerControl().setWalkDirection(walkDirection);


         positionMessage.viewDirection = pl.getPlayerController().getPlayerControl().getViewDirection();
        positionMessage.location = pl.getPlayerController().getPlayerNode().getLocalTranslation();
        positionMessage.name = pl.getName();
        this.sendToAllPlayers(positionMessage);

    }
}

[/java]
this is part of my code, it was working, but after changed mode in headless everything have stop. Can someone give advice how i can fix it ?

After couple hours:

I have figured out one thing, I can create new world by creating new state AbstractAppState.
Is it ok to create new state for each new world in my game ?
in one world will be max 6 people 3x3

Its very simple

You need to create main in headless
[java]

public static void main(String[] args) {
    ServerMain app = new ServerMain();        
    app.start(JmeContext.Type.Headless);         

}

[/java]

After it you need create new game world

[java]

private void startGame(final int worldId) {
GameWorld gw = new GameWorld(worldId);
stateManager.attach(gw);
}

[/java]

GameWorld class should be laike:

public class GameWorld extends AbstractAppState {

private int id;

private SimpleApplication app;
private Node rootNode;
private AssetManager assetManager;
private AppStateManager stateManager;

// separated physics world
private BulletAppState bulletAppState;

public GameWorld(int id) {
this.id = id;
}

@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication) app; // can cast Application to something more specific
this.rootNode = this.app.getRootNode();
this.assetManager = this.app.getAssetManager();
this.stateManager = this.app.getStateManager();

bulletAppState = new BulletAppState(); //<-- question about this
}
}

[/java]

sorry if i have made some mistakes, I just was taking parts of my code

I just have question is it okay create new worlds like this ? And for each world new BulletAppState ?

I don’t know how edit firs post ;/
it’s little bit mess. I had problem and i solved this way. I’m posting just my solution. maybe someone will find it useful.

[java]
public class GameWorld extends AbstractAppState {

private int id;
private SimpleApplication app;
private Node rootNode;
private AssetManager assetManager;
private AppStateManager stateManager; // separated physics world
private BulletAppState bulletAppState;

public GameWorld(int id) {
this.id = id;
}
@Override public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication) app; // can cast Application to something more specific
this.rootNode = this.app.getRootNode();
this.assetManager = this.app.getAssetManager();
this.stateManager = this.app.getStateManager();
bulletAppState = new BulletAppState(); //<– question about this
}
} [/java]

First post can’t currentl be edited unless via URL manipulation, but it’s okay.

AppStates aren’t for world creation. Their purpose is to make it possible to switch stuff on and off without having to worry about threading (the AppState will do the actual state change inside the render thread).
So I think it’s an accident that using an AppState solved our problem.

1 Like

I’m not sure is it good solution, if project will scale what problems i will get ?
Can you give me any suggestions how i can create new world most efficient way ?

If multiple worlds are unconnected, you can simply start each in its own program. If they are connected, you can update them from the same update loop, essentially unifying them all into a single universe.

I don’t know what you’re trying to achieve, so it’s hard to give any advice.

this game is like Multiplayer online battle arena (MOBA) type, words are not connected, I was trying create own programs. But problem was when my server in headless mode was creating new SimpleApplication in headless mode, i got a lot of problems. Everything was working while each my world was in display mode, but not in headless.

@EdmundasSan said: this game is like Multiplayer online battle arena (MOBA) type, words are not connected, I was trying create own programs. But problem was when my server in headless mode was creating new SimpleApplication in headless mode, i got a lot of problems. Everything was working while each my world was in display mode, but not in headless.

Wait… each world was a full SimpleApplication? Yeah, that’s not going to fly. One app per app.

Yes I know now. I have learned, bad example ;D so what you are thinking about new AbstractAppState per world? In this state will be done all physics calculation.

Usually, you’d put the physics in another app state (there is already one for this purpose I think) and just clear it out and reset it as needed.

1 Like