rootNode question

When I create another class which does not have the main method and does not extend SimpleApplication, so I cannot make operations for rootNode. For example:

[java]rootNode.attachChild(spatial);[/java]



Should I do something for a common class to have an access for rootNode?

Do you mean that you cannot change things if you are not in the render thread?



You can always do something like this:



[java]



public void attachChildFromAnotherClass(Spatial s)

{

main.rootNode.addControl(new UpdateControl());

main.rootNode.getControl(UpdateControl.class).enqueue(new Callable() {



@Override

public Object call() throws Exception {

main.rootNode.attachChild(s)

return null;

}

});

}

[/java]

1 Like

Thanks!

Omg, read the wiki manuals please xD This is like the most basic java question ever ^^

well, I did not find it on the wiki, but I figured out by myself another approach:



MyGame - is a main class.

NewClass - is a new class.



[java]class NewClass {



MyGame mg;

Spatial sp;



NewClass (){

sp = new Spatial();

mg.getRootNode().attachChild(sp);

}



}[/java]



Actually, I’m not a programmer… I’m a 3d artist… I’ve been trying studying java by myself.

Except mg will be null unless you set it to something… like maybe in the constructor.



Using JME without knowing Java is officially super-duper hard.

Yes, that is basic Java and not only is your mg reference possibly null, but you can also have the problem of not being able to change the scene graph because of the aforementioned problem (render thread).

Actually, I tested it in my scene and it worked ok… But if you say that it’s wrong, so I will be use the tip of @Dodikles .



Another way is using a “Node nd” instaed of “rootNode”. And then attach “nd” to the rootNode in the main class:



[java]class NewClass {



Node nd;

Spatial sp;



NewClass (){

sp = new Spatial();

nd.attachChild(sp);

}



}[/java]

Found this post fitting for a request like this. I’m in a boat like mifts, but my experience is mostly playing around with javascript.



If anyone knows of good introductions to concepts like threading, synchronization, and other things unique for java compared to js, I’ld epreciate it greatly.



I’m quite lazy, so links to oracle or sun is not what i’m looking for :slight_smile:

mifth said:
Actually, I tested it in my scene and it worked ok... But if you say that it's wrong, so I will be use the tip of @Dodikles .

Another way is using a "Node nd" instaed of "rootNode". And then attach "nd" to the rootNode in the main class:

[java]class NewClass {

Node nd;
Spatial sp;

NewClass (){
sp = new Spatial();
nd.attachChild(sp);
}

}[/java]

This can't work either.

When jME will try to attach the child you'll get a NPE because the node isn't initialized.

[java]Node nd; <-- this means nothing. It's null.[/java]

You have to do something like this:
[java]
nd = new Node("node name");

// or

NewClass (Node nodeFromSomewhereElse) {
sp = new Spatial();
nodeFromSomewhereElse.attachChild(sp);
}

// The above would be called elsewhere in a different class using something like:

NewClass someNewClass = new NewClass(rootNode);
[/java]

BTW, this is basic programming 001, not even 101. :wink:

Oh, sorry, now I get wanted to say. :slight_smile:

I do remember about initializing of an object.



I was not at home when I wrote it. I have just forgotten about initializing. So, will it be ok if I use the main class in another class?



[java]class NewClass {



MyGame mg = new MyGame();

Spatial sp;



NewClass (){

sp = new Spatial();

mg.getRootNode().attachChild(sp);

}



}[/java]

No, this way you would have two instances. And this is definitely NOT what you want!



Try this



[java]

class NewClass

{

MyGame mg;

Spatial sp;



public NewClass(MyGame ref)

{

sp = new Spatial()

mg = ref;

mg.getRootNode().attachChild(sp);

}



}

[/java]



But keep in mind that the “mg.getRootNode().attachChild(sp)” can fail if you are in the wrong thread and then you have to do what I wrote in the beginning.

That won’t really work either although you might have the impression it does work. Why do you ask?



Because your "MyGame mg = new MyGame(); is basically making a NEW MyGame and -not- using the one with the app.start(); you know, the one with the simpleInit(); method?

If you do not know Java well then do not attempt multithreading at all. Seriously.



And if you start wanting to explore down that path, use java.util.concurrent classes whenever possible. If you are looking for things to read beyond basic threading tutorials, anything by Doug Leah… and then keep rereading it until it makes sense. Or just use java.util.concurrent classes since he basically wrote them. :slight_smile:

1 Like
Dodikles said:
But keep in mind that the "mg.getRootNode().attachChild(sp)" can fail if you are in the wrong thread and then you have to do what I wrote in the beginning.

And unless this is called from the simpleUpdate(); this will also fail with a scene graph error (if it's not in a different thread).
madjack said:


You have to do something like this:
[java]
nd = new Node(&quot;node name&quot;);

// or

NewClass (Node nodeFromSomewhereElse) {
sp = new Spatial();
nodeFromSomewhereElse.attachChild(sp);
}

// The above would be called elsewhere in a different class using something like:

NewClass someNewClass = new NewClass(rootNode);
[/java]


As I understand "NewClass (Node nodeFromSomewhereElse)" constructor for another class will be the best way.
Thanks to @madjack .

I would highly suggest you read up on programming (preferable Java) though before you delve deeper into this. Read a lot. I don’t mean this in a demeaning way, as I’m sure the others didn’t either, but if you aspire to be a good programmer some day you have to have good habits and only experience and good learning material will help. Just picking up bits and pieces of code here and there won’t do much for you.



Learn, read and do tutorials. MANY tutorials. It’s the only way. :slight_smile:



Good luck.

1 Like

@pspeed : I’ve got to much things in my list of things to learn. From what you’re saying, I get that multithreading is far down on the java learning road.

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 :slight_smile:

2 Likes