Very basic jME question :S

Guys this might be a very basic question so I apologize in advance.



I have a main class that extends simpleApplication and another one that accesses it through functions like myApp.getAssetManager();



Now since everything including the assetManager gets initialized in SimpleInit() that is triggered by app.start() I am having an issue accessing the assetManager from the other class as my code is something like that:





[java]

Visual app = new Visual(); // main class that extends simpleApplication

AnotherClass DoStuff = new AnotherClass(app);

app.start();

[/java]



so obviously DoStuff is called before app.Start() which is why I am getting assetManager always null. The thing is doStuff is really important as I initialize things in it, and I don’t want to put them/initialize them inside SimpleInit() so any way around that? can I initialize or add things to the initialization without calling app.start()?





again sorry if this is a really stupid question :S

AssetManager doesn’t exist until after app.start().



I don’t understand why you don’t want to call new AnotherClass() from simpleInit().

@pspeed said:
AssetManager doesn't exist until after app.start().


Was about to say it, but got Ninja'd.

Let your AnotherClass wait until app is in simpleInit then trigger whatever to AnotherClass... ?
1 Like

the reason is bc i got 2 things hapening, the main is the actual game that I want to be able to run independently. and AnotherClass that runs animation events (like cinematics) that i was hoping to call and trigger the main class through it. In fact I used to have AnotherClass extend SimpleApplication as almost a totally different application but as it’s a bad design I am trying to combine them by having the AnotherClass use the assetManager of the main class.



Im assuming theres no other way other than rethinking my design rite?

to be more clear, I want the main to be only a window of jME and I want to send events to it in order to animate them (my whole game is really an animation tool) via anotherClass

You can start the simple application without it actually doing anything (i.e. don’t add any nodes or whatever)…then your other class has access to the asset manager.



It sounds like you need to rethink your design though and either refactor the animation side so it has no dependency on SimpleApp state or accept that it has to be a child of the simple app rather than the other way around.



You’re trying to fight the framework rather than go along with the way it wants to work so it’s going to be awkward and not particularly clean whatever you do.

1 Like

I concur with @zarch There’s no other way to correctly do it.

1 Like

this might be more like a Java question but how about if I had two adapters and one helper class where each adapter is considered as a separate application (that extends Simpleapplicaiton) but the helper class is used by both. I worked it all out except at the beginning of the helper class I got something like this:





[java]

private Adapter1 myApp = Adapter1.getApp();

//private Adapter2 myApp = Adapter2.getApp();

[/java]



where I call one adapter at a time. Adapter1 and Adapter2 are different but they both extend SimpleApplication(). Inside the helper class, I use myApp to use helper functions (eg. myApp.getRootNode().attachChild(…) and it works fine except I don’t like the fact that I have to comment the code of the other adapter every time I need to use the other one.



any ideas on how to make this generic?



I am expecting some smartass to put java beginner link or something but it’s really different with jME :slight_smile:

Are you running separate SimpleApplications in the same application or as separate applications?

no of course not. they r separate/



I just dont want to cresate the helper class twice just becoz of one line

There is too much I don’t understand about your setup.



I don’t know why your app isn’t passing itself to this other class and implementing a more generic interface that the other class will use.



A static getApp() method used by a beginner is a sure sign of a poor design elsewhere.

@homsi If I want another class to manipulate the main application, then I make it extend AbtractAppState (create and add it to the statemanager in simpleInitApp()) and I access app.getAssetManager() and app.getRootNode() etc… On the other hand, if that were the right answer, pspeed would have already proposed it… :?

@zathras said:
@homsi If I want another class to manipulate the main application, then I make it extend AbtractAppState (create and add it to the statemanager in simpleInitApp()) and I access app.getAssetManager() and app.getRootNode() etc... On the other hand, if that were the right answer, pspeed would have already proposed it... :?


She wants to be able to use the AssetManager before simpleInit()... Or something silly like that. :P
1 Like

the point was made that the design was not right and since these apps that need to modify that main application were realy separate apps I have now 2 SEPARATE appplications that extend simpleApplication and one class let’s call it AnotherClass.java that has a lot of methods relating to animation.



so the question now is in AnotherClass.java my code is like this



[java]



public class AnotherClass{



/** to be replaced by appropriate adapter */

private Adapter1 myApp = Adapter1.getApp();

//private Adapter2 myApp = Adapter2 .getApp();



…



myApp.getInputManager()…

myApp.getRootNode().attachChild(…)



…

[/java]



so everything works fine except that I have to comment and un-comment the code at the beginning to the appropriate adapter I am trying ot make it generic.

You are kind of repeating yourself without providing any new information.



Why is it that Adapter1 isn’t instantiating AnotherClass and passing it the application?



Why is it that AnotherClass isn’t just an AppState that your apps instantiate and attach to the state manager so that AnotherClass gets a nice life cycle and everything it needs handed to it.



If you want help with your designs then we are going to need more information. Otherwise I really do recommend that you spend some time learning Java because other than the simplification that AppState provides, these aren’t really JME specific issues.

1 Like

Yeah, I can’t really help here. There are way too many static methods which makes proper design kind of impossible. Sorry… I think people will have a lot of trouble helping you.



Static-method based designs will find lots of cases where OOP fails for them and they have to resort to commenting out sections of code to change basic behavior where a strategy pattern or polymorphism would have helped avoid it.

@zarch, I am going with your approach and I thought of doing something like this in anotherClass in order to start the Game (contains the main) without anything in it and add things to it as I go:



[java]

private static void initializeGame() {

game = new Game();

game.start();





Box b = new Box(Vector3f.ZERO, 1, 1, 1); // create cube shape at the origin

Geometry geom = new Geometry(“Box”, b); // create cube geometry from the shape

Material mat = new Material(game.getAssetManager(),

“Common/MatDefs/Misc/Unshaded.j3md”); // create a simple material

mat.setColor(“Color”, ColorRGBA.Blue); // set color of material to blue

geom.setMaterial(mat); // set the cube’s material

game.getRootNode().attachChild(geom);





}

[/java]



however I get a nullpointer error because box is being called before game has completely initiated. In fact, if I debug and go step by step and stop before Box is initiated and wait for the app to have started, the rest runs fine and Box attaches to the rootNode just fine. I think my Java level doesn’t allow me to figure it out. How can I fix that? Again sorry if the design looks awkward but its a part of a bigger design and am not just wasting time.

Have you tried?



[java]Thread.sleep(4000);[/java]



there must be a cleaner approach though

1 Like
@homsi said:
How can I fix that? Again sorry if the design looks awkward but its a part of a bigger design and am not just wasting time.


Then the design is flawed. There isn't any other way to put it. You may think you're not just wasting time, but you'll end up working 100x more than you should.

It's your project. *shrug*
1 Like
Then the design is flawed.


I am not sure what she's trying to accomplish but for me I have a Visualizer class that extends SimpleApplication and an Animation class that takes care of the animation and adapters that use these two classes. I dont have a game though, I am animating a trace file for my simulator so for me I need one visualizer that is contained in an adapter (I have MANY adapters). I don't think her design is necessarily flawed, I know mine is not. But in my case I don't want to have the main class that extends simpleApplication (Visualizer) call the other class from it but vice versa.