Casting App States

Hi Guys,

I hope someone can help me with this. By all means tell if my approach is incorrect.

I have 3 apps states which all have the same method names and the same parameters.

In my main class, depending on user selection I only want to attach 1 of the 3 app states to the statemanager.

From my main class I want to call the methods from the attached app state using the same code.

For example, app state 1 has a method called RUNGAME
app state 2 has a method called RUNGAME
app state 3 has a method called RUNGAME

The user selects game 2 so I want to attach app state 2. That’s straightforward.

Now I want to write code that will call methods from which ever state I have attached.

I think what I need to do is something like, create an generic appstate object and cast it at runtime to the correct appstate type?

does that make any sense at all???

you can extend AbstractAppState to your custom base or abstract class, that way you only cast to your class to this base class. and call your methods without caring if it’s app state 1/2/3 since they overrode them their own way. You can do that.

Also, I recommend going through some basic Java tutorials, specifically about “interfaces”.

Thanks for that. I’m glad you understood what I meant.

I sort of get what you mean, but would you be able to create a code stub or point me to one that shows your suggestion in practice. I appreciate your help.

Thanks, I’ll look at that.

Yeah, what you want to do isn’t hard. It’s just that for anyone with the requisite skills to actually use JME effectively then it’s not even something they’d have to ask. So this can kind of serve as a litmus test to beefing up your Java skills to the point where you can use the engine effectively. JME is not really for Java beginners.

Thanks. I appreciate your comments. I’m not new to programming. I have over 20 years experience in many other languages. Java is relatively new to me and admittedly I’m learning what I need to know about Java as I go and there are some gaps.

I’ve spent over 6 months on JME intensive development (my time is expensive) and I’m due to release a fully cross platform app in the next few weeks even with the severe limitations of IOS support.

Just so you know, you can come across as condescending and if you want real developers to get involved it’s best to be encouraging and helpful rather than patronising new contributors.

Yeah, probably better to just let them go unanswered or poorly answered. :-/

…I was trying to be nice. The people who actually ARE inexperienced often thank me. We had no idea of knowing from your question… only that the AppStateManager.getState(Class) method is somehow a stumbling block. So I guess incorrectly.

Perhaps it only seemed condescending to you because you have more experience than it seemed. Perhaps it was.

Either way, what you want to do is pretty simple:

public class MyAppState extends AbstractAppState implements SomeCommonInterface {
 ..............
    @Override
    public void myCommonMethod() {
    }
}

SomeCommonInterface myThing = appStateManager.getState(SomeCommonInterface.class);
myThing.myCommonMethod();

And since you have lots of programming experience (which we didn’t know before) then that answer will make sense to you and you will tuck it into your Java-idioms belt for later. If you were new to Java/programming, then I just did you a HUGE disservice by bypassing a bunch of essential learning.

More than half the questions here are basically of the variety: “I just got my first saw and I want to make a baby grand piano… can someone tell me how to make the hammers?” We aren’t being mean to suggest they make a bird house or two first.

2 Likes

Sure the stateManager works this way?
Afaik it has to be

SomeCommonInterface myThing = (SomeCommonInterface)appStateManager.getState(MyAppState.class);

But you still have to keep track of the actual attached appstate class and not the common interface.

Yes, I’m sure… for a few reasons:

  1. I wrote this code.
  2. I use it all the time this way.

OP stated that he will only ever have one of these attached at a time so there is no reason to disambiguate to a specific concrete class. The interface works fine and allows for better polymorphism.

1 Like

Ah, missed the isAssignableFrom. So it returns the first attached AppState that implements the interface.
Good to know.

I take your point and I appreciate your help. Thanks.

Excellent. That worked perfectly. I had already implemented 2 interfaces for communicating with the IOS and Android native methods but didn’t realise you could use it like this so thanks again for the information. A big help.