rootNode question

You need to make sure your doing everything right from the get go as well, otherwise your code gets all messy, and trying to find bugs makes you wanna kill yourself.

normen said:
Multithreading is basically just making it harder to maintain your software and when you have no proper handle on java and programming in general you will quickly run into design and coding problems with your application. I outlined a safe way to do multithreading for tasks that would block the update loop unnecessary here: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:multithreading If that all doesn't mean anything to you its probably not the time to do multithreading yet :)
This looks great, thanx.

May I ask again about it? Sorry for my stupidnessā€¦ Will such a way suits multithreading?



MyGame ā€“ is a main class.

NewClass ā€“ is a new class.

[java]

class NewClass {



MyGame mg;

Spatial sp;



NewClass (MyGame rootGame, Spatial rootSpatial){

sp = rootSpatial;

mg = rootGame;

mg.getRootNode().attachChild(sp);

}



}[/java]



In MyGame class:

[java]

ā€¦

public static void mainā€¦

ā€¦



NewClass nc;

Spatial Spaa = new Spatial();



simpleInitApp(){



nc = new NewClass(Spaa, this);



}

ā€¦

[/java]

Okay, this became messy and I think you are completely confused by now. :stuck_out_tongue:


  1. We were talking about multiple classes at first. You want to divide the code up in many functional parts (classes and objects). That is good BUT this has nothing to do with multithreading. What you wrote now is just two classes that work with each other.


  2. The second point was multithreading and that you cannot access OpenGL-related stuff if you are not in the right thread. That was the thing I talked about.


  3. The last but not least thing is that you cannot change the scenegraph outside the update loop. There are many things going on that need to be calculated like bounds, cullingā€¦ That is what madjack was talking about. Only when the engine is not completely up and running (like when you are in your simpleInit() method) you can alter things.



    What you wrote in your last post would be working only because the scenegraph is not being processed (3). If you want to do the same code during the game itself (for example spawning a car based on a timer or sth.) then this code would fail if you donā€™t do this in the update loop. The UpdateControl takes a task (like the attachChild() instruction from another method or another class) and does exactly that. It waits with the task until the next update loop and processes it only then. This way it does not ā€œcorruptā€ the scenegraph. The same applies if you have multiple threads (2) that want to spawn cars or objects into the scene. If you are not in the right thread AND the update loop you wonā€™t be able to change thingsā€¦



    So to be short: Yes, what you wrote will work. But this has nothing to do with multithreading. And no, it will not work when the game is running already (I mean when the main class is running its update() loop) and you call this method from another class and outside the update() method.

THANK YOU VERY MUCH FOR DETAILED EXPLANATION!!!

solution 1:

You can use a singleton scene class that holds all variables like the simpleApplication, the jframe, nifty, etc.



You can then do Scene.get().getApplication() and then get rootNode or whatever else you need afterwords.



It is the easiest and fastest solution.

However its against object oriented design standards :

a) other people have to use your ā€œsceneā€ class in order to use your system.

b) scene has many features (allows to access everything in the system) = god class.

c) everything depends on scene.

d) you cannot have 2 scenes running at the same time.



Any alternatives to singleton scene ?



solution 2:

Every object that is created will contain a reference to scene.

  1. speed / memory overhead ( 4 bytes x number of objects ).
  2. suffers from a), b) c).



    solution 3:

    Every objects uses only variables it needs.
  3. speed, memory overhead.
  4. will lead to huge constructors with 10 arguments, assetManager, camera, etc.
  5. solves a,b,c,d problems.
  6. new problem : scripts need to have access to everything = because everything in a game should be modable.



    So which solution would be best for a video game ?

@Dodikles , If a game is runningā€¦ I cannot use I posted above within update() loopā€¦



But can Abstarct AppState and AppStateManger help with overriding the initialization? To use such classes with AppState?



I hope I wrote clearlyā€¦ sorry my mother language is not English.

Yes, you can use the code in your update loop. Look what I wrote :wink:



If you want to do the same code during the game itself (for example spawning a car based on a timer or sth.) then this code would fail if you donā€™t do this in the update loop.


If you do this in the update loop then you are fine :)