Load a model in new scenne

how load the model when i the login is made with sucess
this is my code
ControleTela.class

package meujogo.nifty;
import com.jme3.app.state.AbstractAppState;
import mygame.Main;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

 

/**
 *
 * @author Pedro
 */
public class ControleTela extends AbstractAppState implements ScreenController {
    Main jogo; //Usado para acessar os dados do jogo
    Nifty nifty;
     private Screen screen;//Usado para interagir com a tela
      private Element formLayer; //Esse objeto conterá a referência a layer da tela referente ao formulário
      private FormularioController formController; 
      private servercontrol Servercontrol;

    public ControleTela (Main jogo){
        this.jogo=jogo;
    }
 
  public void startGame(String nextScreen) {
    nifty.gotoScreen(nextScreen);  // switch to another screen
  }
     public void bind(Nifty nifty, Screen screen) {
        System.out.println("bind( " + screen.getScreenId() + ")");
        this.nifty=nifty;
         
        //Aqui eu busco na tela a camada de Formulário
        formLayer= screen.findElementByName("formLayer");
        if (formLayer!=null){ 
            formController=formLayer.getControl(FormularioController.class);
           Servercontrol=formLayer.getControl(servercontrol.class);
          
            formController.setControleTela(this);
       //     Servercontrol.setControleTela(this);
   
        }
  
    }    
 
 public void login(){
if(formController.getlogin().equals("Administrador")){
nifty.fromXml("Interface/Server.xml", "Server");

//nifty.gotoScreen("server");
}
else if(formController.getlogin().equals("Jogador")){
System.out.println("ENTROU COMO JOGADOR");
}else if(formController.getlogin().equals("ERRO"))return; 
   
//System.out.println("");
 }
 public void options(){

     nifty.fromXml("Interface/options.xml","Options");
 
 }
public void cancel(){
nifty.fromXml("Interface/Login.xml","start");
 }
    public void onStartScreen() {
        System.out.println("onStartScreen");
    }
 
    public void onEndScreen() {
        System.out.println("onEndScreen");
    }
 
    public void quit(){
        nifty.gotoScreen("end");
    }
public void Logout(){
nifty.fromXml("Interface/Login.xml","start");
}
 public void entrar(){
nifty.fromXml("Interface/charSelection.xml","charSelection");
}
}

main class

import com.jme3.app.SimpleApplication;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.RenderManager;
import com.jme3.system.AppSettings;
import de.lessvoid.nifty.Nifty;
import meujogo.nifty.ControleTela;

public class Main extends SimpleApplication {

    private Nifty nifty;
private ControleTela startScreen;
    public static void main(String[] args) {
        Main app = new Main();
      
        AppSettings gameSettings = null;
       
        gameSettings = new AppSettings(false);
       // gameSettings.setIcons(args);
        gameSettings.setResolution(800,600);
        gameSettings.setFullscreen(false);
        gameSettings.setVSync(false);
        gameSettings.setTitle("First Game");
        gameSettings.setUseInput(true);
        gameSettings.setFrameRate(500);
        gameSettings.setSamples(0);
        
        gameSettings.setRenderer("LWJGL-OpenGL2");
        app.settings = gameSettings;
        app.setShowSettings(false);
        app.setDisplayFps(true);

app.setDisplayStatView(false);

        


        app.start();
   
    }

    public void simpleInitApp() {
       ControleTela  startScreen = new ControleTela(this);
    stateManager.attach(startScreen);
        flyCam.setEnabled(false);
        NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,inputManager,audioRenderer,guiViewPort);
        nifty = niftyDisplay.getNifty();
        nifty.fromXml("Interface/Login.xml", "start",startScreen);
     // nifty.addXml("Interface/server.xml");
        // attach the nifty display to the gui view port as a processor
        guiViewPort.addProcessor(niftyDisplay);
        inputManager.setCursorVisible(true);
       
 
    }
     @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

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

i know how load the models but only in main class
but i want load de model when i change the screen

Just pass the assetManager and rootNode to your class…

this is the class i want load the model
i can´t make it work
package meujogo.nifty;

import com.jme3.app.state.AbstractAppState;
import com.jme3.asset.AssetManager;
import com.jme3.light.DirectionalLight;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Controller;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import de.lessvoid.xml.xpp3.Attributes;
import java.util.Properties;

/**
 *
 * @author Pedro
 */
public class charselection extends AbstractAppState implements ScreenController {
    Screen screen;
    Element element;
   private AssetManager assetManager;
  // rootnode ola;
    //Referencia a objetos da tela
   
    ControleTela controleTela;
  
    // private Nifty nifty;//Caso seja necessário chamar algum método do controle 
                              //principal já temos a referência
   

  /**
   * When the selection of the ListBox changes this method is called.
   */
 
     
        public void bind(Nifty nifty, Screen screen, Element element, Properties arg3,
            Attributes arg4) {
        this.screen=screen;
        this.element=element;
     /*    Spatial ninja = assetManager.loadModel("Models/characters/characters.j3o");
        ninja.scale(0.05f, 0.05f, 0.05f);
        ninja.rotate(0.0f, -3.0f, 0.0f);
        ninja.setLocalTranslation(0.0f, -5.0f, -2.0f);
        rootNode.attachChild(ninja);
        // You must add a light to make the model visible
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
        rootNode.addLight(sun);
   */
//element.show();
//element.disable();

 //scrollPanel.setUp(10.f, 10.f, 100.f, 100.f, ScrollPanel.AutoScroll.OFF);

       //elResultado= element.findControl("resultado",String.);
 
    }

   
    public void exit() {
    System.exit(0);
     }
  /*  */ 
    /*
     * Método chamado quando o usuário clica no botão "Calcula" 
     */
   
     
   
   



     
        public void init(Properties arg0, Attributes arg1) {
        // TODO Auto-generated method stub
         
    }
 
 
 
     
        public boolean inputEvent(NiftyInputEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }
 
        public void onFocus(boolean arg0) {
        // TODO Auto-generated method stub
         
    }
 
    @Override
    public void onStartScreen() {
       

        // TODO Auto-generated method stub
         
    }
 
 
    public ControleTela getControleTela() {
        return controleTela;
    }
 
 
    public void setControleTela(ControleTela controleTela) {
        this.controleTela = controleTela;
    }

    public void bind(Nifty nifty, Screen screen) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void onEndScreen() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

Sorry, I think you’d better follow some basic java tutorials because what you are asking is really a trivial question, not only for java developers but for anyone that works with oop, but replying to it would require to teach you java from scratch.

In a sentence:
You never set assetManager to anything so it’s null.


is possibel load models outside the main class
i only can load models
i want load the models when i enter in charecters Selection

Yes.

Then you should setup your app state properly.

This is covered over and over in several hundred threads here.

ok give a simple example how to setup my app to state properly

Caelum Escola de Tecnologia Cursos Online <<<
I hope i guessed the language right.

ok thanks
i put this code this link AppState Example
have a question i have to create new node to have time i create a new gui

I don’t really do tech support for nifty. I stopped using it 5+ years ago because I found it too difficult.

But for regular app state setup, it’s not really an app state if you don’t attach it. It’s just some random code hanging out in space. But I can’t actually see how you’ve set it up so it would be nearly impossible for me to tell if you did it right or whatever… nor can anyone else help with that.

If you search for nifty and appstate on the forum I suspect you will get about 116235783468345682468234 hits because this question used to come up every other day.

i put my code in the GitHub this is the link
https://github.com/Kammikazy/FirstGame
with the code and nifty to see where i my error

i have a error Jul 24, 2016 3:23:31 AM de.lessvoid.xml.tools.ClassHelper getInstance
WARNING: class [mygame.ServerScreenState] could not be instantiated (java.lang.InstantiationException: mygame.ServerScreenState)
this is my code
ServerScreenState

    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 com.jme3.asset.AssetManager;
    import com.jme3.font.BitmapFont;
    import com.jme3.font.BitmapText;
    import com.jme3.light.DirectionalLight;
    import com.jme3.material.Material;
    import com.jme3.math.ColorRGBA;
    import com.jme3.math.Vector3f;
    import com.jme3.renderer.ViewPort;
    import com.jme3.scene.Geometry;
    import com.jme3.scene.Node;
    import com.jme3.scene.Spatial;
    import com.jme3.scene.shape.Box;
    import de.lessvoid.nifty.Nifty;
    import de.lessvoid.nifty.NiftyEventSubscriber;
    import de.lessvoid.nifty.controls.Controller;
    import de.lessvoid.nifty.controls.ListBox;
    import de.lessvoid.nifty.controls.ListBoxSelectionChangedEvent;
    import de.lessvoid.nifty.elements.Element;
    import de.lessvoid.nifty.input.NiftyInputEvent;
    import de.lessvoid.nifty.screen.Screen;
    import de.lessvoid.xml.xpp3.Attributes;
    import java.util.List;
    import java.util.Properties;

    /**
     * A template how to create an Application State. This example state simply
     * changes the background color depending on the camera position.
     */
    public class ServerScreenState extends AbstractAppState implements Controller {

      private ViewPort viewPort;
      private Node rootNode;
      private Node guiNode;
      private AssetManager assetManager;
      private Node localRootNode = new Node("Settings Screen RootNode");
      private Node localGuiNode = new Node("Settings Screen GuiNode");
      private final ColorRGBA backgroundColor = ColorRGBA.DarkGray;
    private ControleTela controleTela;
      public ServerScreenState(SimpleApplication app) {
        this.rootNode     = app.getRootNode();
        this.viewPort      = app.getViewPort();
        this.guiNode       = app.getGuiNode();
        this.assetManager  = app.getAssetManager();
        this.controleTela = controleTela;
      }
     Screen screen;
        Element element;
         ListBox listBox;

        //Referencia a objetos da tela
        @NiftyEventSubscriber(id="myListBox")
      public void onMyListBoxSelectionChanged(final String id, final ListBoxSelectionChangedEvent<String> event) {
        List<String> selection = event.getSelection();
        for (String selectedItem : selection) {
          System.out.println("listbox selection [" + selectedItem + "]");
          String[] parts =selectedItem.split(":");
    String part1 = parts[0]; // 004
    String part2 = parts[1];
    String part3 = parts[2];
    String part4 = parts[3];
    String part5 = parts[4];

    System.out.println(part1);
    System.out.println(part2);
    System.out.println(part3);
    System.out.println(part4);
    System.out.println(part5);
        }
        
      }
        
        
      @Override
      public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);

        /** Load this scene */
        viewPort.setBackgroundColor(backgroundColor);

       /* Spatial ninja = assetManager.loadModel("Models/characters/characters.j3o");
            ninja.scale(5.00f, 5.00f, 5.00f);
            ninja.rotate(0.0f, 0.0f, 0.0f);
            ninja.setLocalTranslation(0.0f, 0.0f, 0.0f);
            rootNode.attachChild(ninja);
            // You must add a light to make the model visible
            DirectionalLight sun = new DirectionalLight();
            sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
            rootNode.addLight(sun);
    */

        /** Load the HUD */
        BitmapFont guiFont = assetManager.loadFont(
                "Interface/Fonts/Default.fnt");
        BitmapText displaytext = new BitmapText(guiFont);
        displaytext.setSize(guiFont.getCharSet().getRenderedSize());
        displaytext.move(10, displaytext.getLineHeight() + 20, 0);
        displaytext.setText("Settings screen. Press RETURN to save "
                + "and return to start screen.");
        localGuiNode.attachChild(displaytext);
      }

      @Override
      public void update(float tpf) {
         /** the action happens here */
      }

      @Override
      public void stateAttached(AppStateManager stateManager) {
        rootNode.attachChild(localRootNode);
        guiNode.attachChild(localGuiNode);
        viewPort.setBackgroundColor(backgroundColor);
      }

      @Override
      public void stateDetached(AppStateManager stateManager) {
        rootNode.detachChild(localRootNode);
        guiNode.detachChild(localGuiNode);
      }

        @Override
        public void bind(Nifty nifty, Screen screen, Element element, Properties parameter, Attributes controlDefinitionAttributes) {
          this.screen=screen;
            this.element=element;
             
             listBox = element.findNiftyControl("myListBox", ListBox.class);
            
      listBox.addItem("PT:                                                             "+"Maria:                                             "+"Lisbon:                                                 " +"Maria:                                         " +"Full:");
       // listBox.addItem("b");
        //listBox.addItem("c");
        
     //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void init(Properties parameter, Attributes controlDefinitionAttributes) {
            //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void onStartScreen() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void onFocus(boolean getFocus) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean inputEvent(NiftyInputEvent inputEvent) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
      public ControleTela getControleTela() {
            return controleTela;
        }
     
     
        public void setControleTela(ControleTela controleTela) {
            this.controleTela = controleTela;
        }
    }
class  controlatela
package mygame;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

 

/**
 *
 * @author Pedro
 */
public class ControleTela extends AbstractAppState implements ScreenController {
  Application jogo; //Usado para acessar os dados do jogo
    Nifty nifty;
    //GameState ola ;
 //    private Screen screen;//Usado para interagir com a tela
      private Element formLayer; //Esse objeto conterá a referência a layer da tela referente ao formulário
      private FormularioController formController; 
     public ServerScreenState Servercontrol;
    // private charselection Charselection;

    

   public ControleTela (Application jogo){
        this.jogo=jogo;
   }
 
  public void startGame(String nextScreen) {
    nifty.gotoScreen(nextScreen);  // switch to another screen
  }
     public void bind(Nifty nifty, Screen screen) {
        System.out.println("bind( " + screen.getScreenId() + ")");
        this.nifty=nifty;
         
        //Aqui eu busco na tela a camada de Formulário
        formLayer= screen.findElementByName("formLayer");
        if (formLayer!=null){ 
            formController=formLayer.getControl(FormularioController.class);
          Servercontrol=formLayer.getControl(ServerScreenState.class);
           //Charselection=formLayer.getControl(charselection.class);
        //    formController.setControleTela(this);
       //     Servercontrol.setControleTela(this);
   
        }
  
    }    
 
 public void login(){
     
if(formController.getlogin().equals("Administrador")){
// return "Administrador"; 
 nifty.registerScreenController(this);
     nifty.addControls();
    nifty.fromXml("Interface/Server.xml", "Server",this);


//nifty.gotoScreen("server");
}
else if(formController.getlogin().equals("Jogador")){
System.out.println("ENTROU COMO JOGADOR");
}else{
 System.out.println("ERRO");  
//System.out.println("");
// return null;
 }
 }
 public void options(){
   nifty.removeScreen("start");
     nifty.fromXml("Interface/options.xml","Options");
 
 }
public void cancel(){
    
nifty.fromXml("Interface/Login.xml","start");
 }
    public void onStartScreen() {
        System.out.println("onStartScreen");
    }
 
    public void onEndScreen() {
        System.out.println("onEndScreen");
    }
 
    public void quit(){
        nifty.gotoScreen("end");
    }
public void Logout(){
nifty.fromXml("Interface/Login.xml","start");
}
 public void entrar(){

 //.getStateManager().getState(GameState.class).chargeTower();
nifty.registerScreenController(this);

nifty.fromXml("Interface/charSelection.xml","charSelection");

nifty.addControls();

}
 
}

That error tells me two things:

  1. you didn’t tell nifty what your controller was so it’s trying to create an instance for you… which will never work.
  2. you still don’t understand the lifecycle of app states. There is no need to pass the SimpleApplication because you will be given the application on initialize() when you attach the app state.

If you extend BaseAppState then you will get a lot more helper methods… but it still won’t help with your more fundamental issues, I guess.

Can you make a simple example how to change se gamestus whem i click in controler i only change the game status when i click in key
If possibel

Maybe someone else can. I don’t use nifty and haven’t for 5 years or more.

You should search the forum for “nifty appstate” and I’ bet you’ll find all kinds of stuff.

Else, I can quote my contracting rates… but for nifty support they will be quite high.

thanks i fanily find the solution
for my problem