0
\$\begingroup\$

I have an application with several Screen and Actor. On my main screen, the actors added to the stage during creation are rendering fine.

However, I have a Netty server running, and when it receive certain events, it adds, in the network thread, new actors to the stage. These are never drawn, despite the main thread continuing to call the render function of the corresponding screen.

In the code below, if I call recieveInbound from the Screen's show by mocking the exact same network payload, the actors are drawn as expected.

What am I doing wrong?

MainScreen.java

// ...

public MainScreen(Game game) {
    batch = game.getBatch();
    viewport = game.getMainViewport();
    stage = new Stage(viewport, batch);

    try {
        capabilities = GL.getCapabilities();
    }
    catch (IllegalStateException e) {
        capabilities = GL.createCapabilities();
    }
}

/**
 * Receives and manages signals from the remotes.
 *
 * @param code The received code
 * @param object The received payload
 */
public void receiveInbound(int code, Object object) {

    // Restore OpenGL context if needed before processing.
    try {
        GL.getCapabilities();
    }
    catch (IllegalStateException e) {
        GL.setCapabilities(capabilities);
    }
    // LOGGER.debug(stage.getActors().size); <-- 1 added in show, as expected
    if (code == 10) {
        LevelMap map = mapManager.serve(code, object);
        stage.addActor(map.getBackground());
        Arrays.stream(map.contentMatrix)
            .forEach(row -> Arrays.stream(row)
                .forEach(stage::addActor)
            );
    }
    // LOGGER.debug(stage.getActors().size); <-- 854, as expected
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL33.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(viewport.getCamera().combined);
    // Expected position, actors are around there.
    viewport.getCamera().position.set(0,0,0); 
    stage.act();
    stage.draw();
}
\$\endgroup\$
1
  • \$\begingroup\$ Join libgdx community on discord. They will help you resolve it much faster. \$\endgroup\$
    – Sneh
    Aug 23, 2021 at 23:36

1 Answer 1

0
\$\begingroup\$

If you are communicating with game using another thread, then bugs can happen. In such cases you can execute code on main thread using this code:

Gdx.app.postRunnable(new Runnable() {
    @Override
    public void run () {
        // put your code here
    }
});
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .