Best way to use custom controls

Ive grasped appstates very easily but i havent found a use for spatials . I mean i can use getters to access the rootNode from an appstates with getters like
this.rootNode =app.getRootNode()
but all i have access to in a custom control is the spatial object and am struggling to get access to rootNode state using the idea i heard from the tutorials which uses a seperate class that safely access the rootnode using a seperate thread .Can anyone explain to me how it is done please?

You get it via the class type:
spatial.getControl(MyControl.class)

Oh ,is the MyControl class the main class or an appstate that has access to the rootNode stead?if it is eiither of the above then i definately have something

MyControl is a custom class written by you. I think you are kind of confused.

AppStates are a way of extending the application through composition instead of subclassing. (read: much cleaner)

Controls are a way of extending Spatials through composition instead of subclassing.

You can add behaviors without having to subclass them.

<cite>@pspeed said:</cite> Controls are a way of extending Spatials through composition instead of subclassing.

Yes. This is key here. Controls have the ability to influence the spatial they are tied to in pretty much all the ways you can think of. That is what makes them powerful.

@Pspeed ,i hear you there coz wat i have is A GameState appstate extended by a PlayerControl appstate ,i need the rootNode to get its children and read values from them so that i can control this spatial ,i also need the inputManager for navigation
if you are saying this is subclassing ,then i need help on how you really use a custom control

@Rhymez said: @Pspeed ,i hear you there coz wat i have is A GameState appstate extended by a PlayerControl appstate ,i need the rootNode to get its children and read values from them so that i can control this spatial ,i also need the inputManager for navigation if you are saying this is subclassing ,then i need help on how you really use a custom control

I can’t really parse all of that. You seem to be equating app states and controls and stuff. I’m not sure what you are actually trying to do so I can’t help.

AppStates are for global things.

Controls are for spatial-specific things.

If a control needs a reference to the application then just give it to it.

And with all due respect, If that’s too hard then maybe you need more Java skills before writing a Java game.

Come on im a quick learner whose not really specialising in software engineering but in electronics ,i do have some programming skills and am gonna finalise java in my third year, the thing is i never got to see a full code sample of a custom control like there is for other topics ,guess il keep googling for a code that works then…

What, like the ones on this page?

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_controls

Ive gone thru those ideas for a code a number of times but was looking for a certain code sample that implements the functions i was talking about

What functions? You haven’t mentioned any functions?

To have access to the application from your control…pass the application to your control when you construct it…it really is that simple. (Or use a static AppContext style pattern and store it there so everything that needs to can access it without passing).

It’s not that we don’t want to help you…it’s that we don’t understand where/how you are stuck.

Exactly , was saying i want to stop using appstates to control individual spatials and use custom controls ,but normen here had given me an idea of accessing the The simple aplication objects like the rootnode ,just as ive explained then pspeed comes over and mocks me coz this is the reason i wanted to stop using appstates as custom controls .

@Rhymez said: Exactly , was saying i want to stop using appstates to control individual spatials and use custom controls ,but normen here had given me an idea of accessing the The simple aplication objects like the rootnode ,just as ive explained then pspeed comes over and mocks me coz this is the reason i wanted to stop using appstates as custom controls .

I’m not mocking you. Just suggesting that you are doing things the hardest way ever trying to learn a bunch of things at once.

For example, is this really the source of your issue?

[java]
public class MyCustomControl extends DefaultControl {
private MyApp myApp;

public MyCustomControl( MyApp a ) {
    this.myApp = a;
}

public void controlUpdate( float tpf ) {
    // Do stuff with spatial
    spatial.move( 1 * tpf, 0, 0 );

    // Do stuff with app
    myApp.getRootNode()....
}

}
[/java]

Because if that’s the issue then this is the very most basic object oriented programming stuff. When you try to do two really hard things at once you will never know where your confusion actually comes from. I give this advice a lot: write some simple Java programs until you are familiar with the language and object oriented programming in general… and then attempt something harder.

If you get offended because someone suggests you still have learning to do then it will be really hard to get help.

Also, the source code is available for all of the controls in JME. There are several and they are decent enough examples if you need a “full code example”. You can even just browse the source on the web if you want… though it might also be in the SDK by default, I don’t remember.

At pspeed ,thanks the top code works ,i knw im still learning coz iv been using jme3 for a year now and have just started looking at advanced logic which includes appstates then i will look at multithread. i mean we all have our passions and mine now is to program a 3d game and am sure i will pick up some of the advanced java basics along the way

Well, the source is available for JME. And there are quite a few examples of AppStates and Controls that JME uses even internally.

http://code.google.com/p/jmonkeyengine/source/browse/#svn%2Ftrunk%2Fengine%2Fsrc%2Fcore

Finally i got a custom control to by not trying to subclass the GameState appstate to get access to the rootNode,in an EnemyControl class where the enemy’s task is to follow the player.In the update method i had
Node root =spatial.getParent();
rootNode.depthFirstTransversal(new SceneGraphVisitor(){
public void visit(Spatial spa){
if(spa.getName().equals(“player”)){Vector3f=spa.getLocalTranslation();
}}});
then i can use the players location to move the enemy spatial.

Got a custom control to work that is, from the jme3 tutorials on custom ,i hear of a PlayerNav control,if it reads input from the user to move the player spatial ,how can i get the inputManager into this custom control?

@Rhymez said: Got a custom control to work that is, from the jme3 tutorials on custom ,i hear of a PlayerNav control,if it reads input from the user to move the player spatial ,how can i get the inputManager into this custom control?

You pass it during construction. See above.