Return a Value from a SubState



  • I am passing in a value as "trigger" to a substate, that works fine. My problem is I am setting a Bool flag in the substate that I want to pass back to the parent, but it dont know how to return it.
    I tried setting up a Global state that I import, but that didn't seem to work. Help!

    Thanks Lee

    	var tempState:StoryState = new StoryState();
    	tempState.isPersistant = true;
    	tempState.trigger = trigger;  // pass trigger number into substate.
    	openSubState(tempState);
    

    I just close it when I return

    close();
    return ;



  • Quite a few different ways you could add a public FlxSignal to StoryState and have a function in your parent state subscribe to it, then dispatch it before you call close().

    So for example StoryState would have in the declarations
    public var triggerSignal:FlxTypedSignal<Bool->Void>
    in it's constructor
    triggerSignal = new FlxTypedSignal<Bool->Void>()
    and when its closes itself it dispatches it

    triggerSignal.dispatch(trigger);
    close();
    return ;
    

    Your main state would have a function to receive the dispatch

    function substateTriggerHandler(trigger:Bool):Void
    {
    }
    

    Which it would add to the signal when it creates the state

    var tempState:StoryState = new StoryState();
    tempState.triggerSignal.add(substateTriggerHandler);
    

    If you care about encapulation you could pass substateTriggerHandler to as a parameter to StoryState, I guess.



  • Thanks I will try to get this working. Seems overly complex for returning a value. Shame there is not easy way of creating a variable that is truly global. I tried have a gloabal state but when you import it into antoher state then the value goes back to null. Lee



  • @Ferrari177 If you just want a global value you could use a static variable in StoryState, eg
    public static var trigger:Bool;
    A static value is one that is a property of the class rather than an instance of the class, so you can access it anywhere as
    StoryState.trigger



  • And of course you can simply run any state method from substate on substate's closing:

    (cast _parentState).AnyMethod(); 
    close();
    return ;
    

    Note: Cast disables the check for the existence of such a method in a class at compile time and at runtime, it means that the correctness of the method name and parameters - at your own risk.



  • AHH yes! thats what I needed. Thanks. Got the data going back, Thanks again.


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.