Nifty GUI won't display text at right time

Hi all,

I have a game system where I first create a nifty GUI object. The user enters the parameters of the game and after the start button is clicked the game is loaded. However, I want the GUI screen to display the text “Loading…” before the game is loaded. I used the below code, but it seems that the text is only displayed after the finishloading method (the parent variable refers to the Main class).

[java]
public void startGame(){
Label loadMessage = screen.findNiftyControl(“errorMessage”, Label.class);
loadMessage.setText(“Loading…”);
parent.finishLoading();
}
[/java]

I assume it has something to do with rendering loops but I’m not sure how to solve this problem. Any help is appreciated.

@deev said: Hi all,

I have a game system where I first create a nifty GUI object. The user enters the parameters of the game and after the start button is clicked the game is loaded. However, I want the GUI screen to display the text “Loading…” before the game is loaded. I used the below code, but it seems that the text is only displayed after the finishloading method (the parent variable refers to the Main class).

[java]
public void startGame(){
Label loadMessage = screen.findNiftyControl(“errorMessage”, Label.class);
loadMessage.setText(“Loading…”);
parent.finishLoading();
}
[/java]

I assume it has something to do with rendering loops but I’m not sure how to solve this problem. Any help is appreciated.

If you are not loading your game on a background thread… then your locking the render thread and there is nothing that can be done. Push the whole process to a seperate thread (the loading process that is) and use callables to send any updates need. This way you can still make visual changes while your app is busy doing other things.

EDIT: Just to clarify… this is not a Nifty issue. This will happen with any onscreen changes you attempt to make.

1 Like

Thanks, that was my next guess but I was hoping there was a hack around it.

@deev said: Thanks, that was my next guess but I was hoping there was a hack around it.

A single thread executes things sequentially and “render thread” is not a misnomer here.

So in the sequence of:
-update things
-render them
-update things
-render them
…done by the render thread

If you insert…
-update things to show Loading…
-take a really long time doing something
-render them

…then they won’t be rendered until “take a really long time doing something” is finished.

1 Like
@deev said: Thanks, that was my next guess but I was hoping there was a hack around it.

Essentially, this is probably faster than any hack around it would be anyways… in the run method of the thread, you can (in it’s simplest form):

[java]
@Override
public void run() {
final Node n = //load some asset.
Callable call = new Callable() {
public Object call() throws Exception {
app.getRootNode().attachChild(n);
return null;
}
};
app.enqueue(call);

// Do this some more...

}
[/java]

This also gives you a chance to send % complete updates for any status bar/indicator you are displaying.