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();
}