J3o scene objects

I made a terrain with objects in the terraineditor and scenecomposer. How can I access the objects in the programming window? I tried to do it with the SceneGraphVisitor class like it says in the documentation but it doesn’t work. I used the following piece of code from the documentation:

SceneGraphVisitor visitor = new SceneGraphVisitor() {

  public void visit(Spatial spatial) {
    // search criterion can be control class:
    MyControl control = spatial.getControl(MyControl.class);
    if (control != null) {
      // you have access to any method, e.g. name.
      System.out.println("Instance of " + control.getClass().getName()
                       + " found for " + spatial.getName());


and then in the simpleInitApp I used rootNode.depthFirstTraversal(visitor);

Also in the above code I tried to use MyControl which is not part of the API.
What code or class should I use instead of MyControl?
I would like to know the name of the objects from the scene j3o file so I can program with it.

SceneGraphVisitor is the way to visit every spatial in the scene graph. You haven’t really said why you want to search the whole scene graph.

MyControl is an example. It’s showing how someone would “search the whole scene graph” for a particular Spatial that had a control of type MyControl… presumably that they wrote and were trying to find.

What is it that you are actually trying to do?

Did you load your objects?

Probably you want to take a step back and explain to us what you are trying to do as if you are talking to your grandmother. “I made some terrain and now I just want to load it my game.” or…?

I made a terrain in the terraineditor and added cannon turrets in the scenecomposer. I saved everything as a j3o file in the scenes asset directory. Now I am trying to make the turrets shoot at me if I enter a range.

So the framework/outline for what you want to do is:

  1. load the scene (j3o)
  2. search the scene for your turrets
  3. “do things” with the turrets.

…and I guess you are stuck on number 2?

In the scene composer, you can give your turrets a name… maybe you already have. In that case, it is straight-forward to find them.

If you provide the code you have already then it will be easier to offer suggestions for the next step.

I also should ask if you have done the JME tutorials yet. That will help answer some of these questions and probably your next 50 questions also.

yes I am stuck on number 2. I renamed 1 of my turrets to turret1 and tried to use it. for example:
turret1.move(0,1,1); but it cannot find turret1.
I read alot of the tutorials but there is nothing like this in the documentation.

It is up to you to find and assign turret1 to a Spatial variable.

I don’t use scene graph visitor, I was unaware of its existence early on in my JME career and I ended up doing the same thing in a slightly different way. But I believe you would want to do something like this:

         Spatial turretSpatial1 = visitedSpatial;

You could also get extra data about the turret with user data fields that you can set to Spatials in the scene composer. The name only holds one string of data about the Spatial, but user data lets you map as much data to a spatial as you need, like if you wanted to add a level to each turret for example:

int turretLevel = turretSpatial1.getUserData("turretLevel");

“turret1” is the name of that Spatial as in Spatial.getName().

In the case of turet1.move()… the turret1 there is just a variable that you define. You need to point it to something.

These are kind of fundamental things in programming that a tutorial may not cover. Some existing programming knowledge is expected.

You can’t just pull turret1 out of the air and start using it… you need to declare it. You haven’t shown the code for how you load your scene or anything so I’m going to have to make some guesses.

Presumably you’ve loaded the scene/j3o file covered in tutorials like Hello Asset:

Spatial myScene = assetManager.loadAsset("Models/myScene.j3o");

Assuming that the spatial is also a Node then you can cast it to Node:

Node mySceneNode = (Spatial)myScene;

If you’ve given the turret a unique name in the scene editor then you can find it like:

Spatial myTurret = mySceneNode.getChild("turret1");

The advice no one takes:
it’s clear from your questions that you are still in the early stages of learning to write code. You would probably benefit from trying to do some basic Java tutorials to learn what classes, fields, variables, etc. are and how/when to use them. Anything that covers general object oriented programming stuff in a way that you will understand would be beneficial.

Then come back to JME by doing the tutorials, not just reading them. Have the javadoc open while you do to read about the other things the objects you play with will do. Try changing the tutorial code in small ways to see what it does… break it then fix it, break it then fix it, etc… When the documentation and the javadocs fall short then you can come back here and ask questions… and probably get really quick responses.

…but otherwise this forum is very poor at teaching Java programming itself.

I would put all the turrets under a Empty Node called “turrets” off the main. So you can easily find all the turrets and then once you have that node call getChild with a name for the turret you are looking for.

Put groups of things inside a NODE (place holder) make life easy to gain access to things. I do things like playfield (terrain), players, enemies, items that can be picked up and etc… There is a tutorial on using Nodes to group things to quickly and easily find items.

I love the whole node (as a place holder), it make life so easy.

My two thoughts.

Thanks pspeed. I used your code, changed it a little and it works now. Thank you all for helping me.