Best practices for accessing a substate's parent state's extended public variables



  • I have a state, BlahState extends FlxState.

    It opens a subState.

    From the subState's create() I want to access BlahState.blahVariable, a public variable that is specific to BlahState, i.e. not found in FlxState.

    Currently I do this:

    var blahState:BlahState = cast _parentState;

    I thought that worked well enough, but are there downsides to this approach?



  • The main downside I can see from this route is that it's bad for reusability - you now can't use this substate in a state that's not a Blahstate instance.

    This is probably not an issue and there's a strong case for just going the most straightforward route, which you've done. The important thing is to be aware.



  • @HappinessSam said in Best practices for accessing a substate's parent state's extended public variables:

    The main downside I can see from this route is that it's bad for reusability - you now can't use this substate in a state that's not a Blahstate instance.

    This is probably not an issue and there's a strong case for just going the most straightforward route, which you've done. The important thing is to be aware.

    Ah, OK. That makes good sense. In my case it's actually quite reusable, because BlahSubstate is used for almost every screen in my game. :)

    So I wonder if that's what @Claudio-Ficara was referring to in the other thread about there being better ways to do this...



  • Oh, my bad. When I said this I was reffering to my example that can be troublesome, not the way you do it, which is perfectly fine! Sorry for the confusion mate :heart:



  • @Claudio-Ficara said in Best practices for accessing a substate's parent state's extended public variables:

    Oh, my bad. When I said this I was reffering to my example that can be troublesome, not the way you do it, which is perfectly fine! Sorry for the confusion mate :heart:

    Ohhhhhh, LOL! I totally missed that. Thanks for the clarification :) :heart:


Log in to reply