Main menu for a game

you doing ${CALL.getPlayerLuck()} in the HUD screen? if so you may need to rethink this anyways, as this is only going to be called once when binding the screen, and that method sounds like it’s dynamic and going to need to update the gui again at some point? So you may be able to just keep polling the value (and if changed) then update nifty, and then forget about the getPlayerLuck initial call, if you can’t get it to work.

My idea was to update nifty when the player get more luck points, I think it’s possible, but I’m into nifty only for like 2 or 3 days now.

yes its possible, something like [java]nifty.getScreen(“screenId”).getElementById (“textId”).getRenderer (TextRenderer.class).setText (“new Text”)[/java]

Anyways for the initialization problem, perhaps post your new updated code, and we can see if we can spot anything again

I got around the problem by using the guiNode in JME, so practically I’m drawing the hud by mixing the 2 GUIs. But still it would be good to know where was I mistaking, so here is my code from that version:

MyStartScreen:
[java]
package mygame;

import com.jme3.app.Application;
import com.jme3.app.FlyCamAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class MyStartScreen extends AbstractAppState implements ScreenController {

private Nifty nifty;
private Screen screen;
private Main app;

public MyStartScreen(Main app) {
    this.app = app;
}

public void bind(Nifty nifty, Screen screen) {
    this.nifty = nifty;
    this.screen = screen;
}

public void onStartScreen() {
    //Unused
}

public void onEndScreen() {
    //Unused
}

@Override
public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
    this.app = (Main) app;
}

@Override
public void update(float tpf) {
}

public void startGame(String nextScreen) {
    nifty.gotoScreen(nextScreen);
    app.started = true;
}

public void quitGame() {
    app.stop();
}

public String getPlayerName(){
return System.getProperty("user.name");

}

public String getPlayerLuck(){

return Integer.toString(app.player.luck);
}
}
[/java]

screen.xml:
[java]
<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“nifty-default-styles.xml” />
<useControls filename=“nifty-default-controls.xml” />
<screen id=“start” controller=“mygame.MyStartScreen”>
<layer id=“background” childLayout=“center”>
<image filename=“Interface/Images/Tropic_Night.jpg”></image>
</layer>
<layer id=“foreground” backgroundColor="#0000" childLayout=“vertical”>
<panel id=“panel_top” height=“25%” width=“75%” align=“center” childLayout=“center”>
<text text=“The Provinces Of Dintena” font=“Interface/Fonts/Default.fnt” width=“100%” height=“100%” />
</panel>
<panel id=“panel_mid” height=“50%” width=“75%” align=“center” childLayout=“center”>
<text text="${CALL.getPlayerName()}'s Cool Game" font=“Interface/Fonts/Default.fnt” width=“100%” height=“100%” />
</panel>
<panel id=“panel_bottom” height=“25%” width=“75%” align=“center” childLayout=“horizontal”>
<panel id=“panel_bottom_left” height=“50%” width=“50%” valign=“center” childLayout=“center”>
<control name=“button” label=“Start” id=“StartButton” align=“center” valign=“center” visibleToMouse=“true”>
<interact onClick=“startGame(hud)”/>
</control>
</panel>
<panel id=“panel_bottom_right” height=“50%” width=“50%” valign=“center” childLayout=“center” >
<control name=“button” label=“Quit” id=“QuitButton” align=“center” valign=“center” visibleToMouse=“true”>
<interact onClick=“quitGame()”/>
</control>
</panel>
</panel>
</layer>
</screen>
<screen id=“hud” controller=“mygame.MyStartScreen”>
<layer id=“background” childLayout=“center”>

    &lt;/layer&gt;
    &lt;layer id="foreground" backgroundColor="#0000" childLayout="horizontal"&gt;
        &lt;panel id="panel_left" width="22%" height="100%" childLayout="vertical"&gt;
            &lt;panel id="panel_top_right1" width="100%" height="65%" childLayout="center"&gt;
                
            &lt;/panel&gt;
            &lt;panel id="panel_bot_right" width="100%" height="35%" valign="center" backgroundColor="#00f8" align="center" childLayout="center"&gt;
                &lt;text text="${CALL.getPlayerLuck()}" font="Interface/Fonts/Default.fnt" width="100%" height="100%" /&gt;
            &lt;/panel&gt;
        &lt;/panel&gt;
        &lt;panel id="panel_right" width="78%" height="100%" childLayout="vertical"&gt;  
            
        &lt;/panel&gt;
    &lt;/layer&gt;
&lt;/screen&gt;

</nifty>
[/java]

The expression Integer.toString(app.player.luck); is working, I tested it. I printed the player’s luck to the console by calling getPlayerLuck() method from Main class.

I would expect that to work. Does it just output ”${CALL.getPlayerLuck()} ? Whats the code for simpleInitApp, has that changed?

I made the MyStartScreen startScreen, NiftyJmeDisplay display, Nifty nifty, a field, but nothing else.

I think I know what’s the problem. Now I got it working. Seems like I had to convert the data to string in the Main class, and then I could pass it to the screen controller.

nifty seems tricky. Is there another way to show a Menu or Inventary on the screen?

You mean in nifty or in another library? There are three other GUI libraries of various ease-of-use, flexibility, dev activity levels

Nifty, plus other three? :wink:

  1. lemur
  2. t0neg0d
  3. ???

The JavaFX UI support that is on other threads here.

Thank you pspeed. I will try Java FX, seems better. I saw a friend using it for other purposes. It is like Java + CSS.

i did the same but didn’t show any changes??? hlp me out frm here.

main class//

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import de.lessvoid.nifty.Nifty;

public class Main extends SimpleApplication {

public static void main(String[] args) {
    Main app = new Main();
    app.start();
}

@Override
public void simpleInitApp() {
    Box b = new Box(1, 1, 1);
    Geometry geom = new Geometry("Box", b);

    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Blue);
    geom.setMaterial(mat);

    rootNode.attachChild(geom);
    
    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(
assetManager, inputManager, audioRenderer, guiViewPort);

Nifty nifty = niftyDisplay.getNifty();
nifty.fromXml(“Interface/gui.xml”, “start”);
guiViewPort.addProcessor(niftyDisplay);
flyCam.setDragToRotate(true);

}

@Override
public void simpleUpdate(float tpf) {
    //TODO: add update code
}

@Override
public void simpleRender(RenderManager rm) {
    //TODO: add render code
}

}

//

MyStartScreen.java

package mygame;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class MyStartScreen extends AbstractAppState implements ScreenController {
private Nifty nifty;
private Screen screen;
private SimpleApplication app;
private String nextScreen;

public MyStartScreen(Main app){
this.app = app;
}

@Override
public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
     this.app =(Main)app;
}

@Override
public void update(float tpf) {
    //TODO: implement behavior during runtime
}

@Override
public void cleanup() {
    super.cleanup();
}

public void bind(Nifty nifty, Screen screen) {
       this.nifty = nifty;
this.screen = screen;
}

public void onStartScreen() {
      nifty.gotoScreen(nextScreen);
 
}

public void onEndScreen() {
      app.stop();
}

}

gui.xml

<?xml version="1.0" encoding="UTF-8"?>
  <panel id="panel_bottom" height="25%" width="75%" align="center" childLayout="horizontal" >
    <panel id="panel_bottom_left" height="50%" width="50%" valign="center" childLayout="center">
      <control name="button" label="Start" id="StartButton" align="center" valign="center"
         visibleToMouse="true" >
           <interact onClick="startGame(hud)"/>
      </control>
    </panel>
    <panel id="panel_bottom_right" height="50%" width="50%" valign="center" childLayout="center">
      <control name="button" label="Quit" id="QuitButton" align="center" valign="center"visibleToMouse="true" > 
       <interact onClick="quitGame()"/>
      </control>
    </panel>
  </panel>
</layer>
<layer id="foreground" childLayout="horizontal">
  <panel id="panel_left" width="80%" height="100%" childLayout="vertical" ></panel>
  <panel id="panel_right" width="20%" height="100%" childLayout="vertical">
    <panel id="panel_top_right1" width="100%" height="15%" childLayout="center">
      <control name="label" color="#000" text="123" width="100%" height="100%" />
    </panel>
      </panel>
</layer></screen></nifty>

anyone have a vid tutorial about nifty or GUI, please

i dont use Nifty, or Lemur. About simple GUI elements you have them on wiki.

here you have general GUI wiki pages:
https://wiki.jmonkeyengine.org/jme3.html#tutorials-for-beginners
(search for GUI keyword)

generally people use one of: Nifty / Lemur / JavaFX / own solutions

not sure if i searched it properly but i found some links with description how to use:

nifty github: GitHub - nifty-gui/nifty-gui: Your Open Source Java OpenGL GUI. Interactive user interfaces for games or similar applications. LWJGL, JOGL, JME or Java2d rendering.
wiki: https://wiki.jmonkeyengine.org/jme3/advanced/nifty_gui.html

lemur github: Getting Started · jMonkeyEngine-Contributions/Lemur Wiki · GitHub

dont seen video, but you might search youtube searching jme lemur or jme nifty keywords

Thank u for replay, that’s very helpful for me as beginner. :pray: :slightly_smiling_face:

2 Likes

np :slight_smile:

based on what i know, a lot of people like Lemur. so i suggest you try it first.

more advanced users also use JavaFX, but it might be hard for new users.

1 Like

Lemur is a great GUI library and works on both 2D and 3D scenes.

My javaFX library only works on 2D GUIs, but if you’ve used javaFX before it’s just the same workflow.

https://github.com/jayfella/jme-jfx-11

2 Likes

Thanks to everyone ^^

1 Like