Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the way FlxStates are called #2565

Closed
wants to merge 9 commits into from
8 changes: 4 additions & 4 deletions flixel/FlxG.hx
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,10 @@ class FlxG
* Attempts to switch from the current game state to `nextState`.
* The state switch is successful if `switchTo()` of the current `state` returns `true`.
*/
public static inline function switchState(nextState:FlxState):Void
public static inline function switchState(nextState:() -> FlxState):Void
{
if (state.switchTo(nextState))
game._requestedState = nextState;
if (state.switchTo(nextState()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state.switchTo() should take a ()->FlxState rather than a FlxState instance, it should store the function in _requestedState and wait until the new stuff is all set up to call it, creating the state

game._requestedState = nextState();
}

/**
Expand All @@ -379,7 +379,7 @@ class FlxG
*/
public static inline function resetState():Void
{
switchState(Type.createInstance(Type.getClass(state), []));
switchState(() -> Type.createInstance(Type.getClass(state), []));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once we start storing the state creator function we can just call that to create a new one, rather than needing to use Type.createInstance.

with Type.createInstance FlxStates can't have constructor args, by passing in functions we can do things like

FlxG.switchState(()->{ return new MyState(arg1, arg2); });

and we'll know how to switch to that state AND reset it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, so like a function? I like it, I like it

}

/**
Expand Down
2 changes: 2 additions & 0 deletions flixel/FlxState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class FlxState extends FlxGroup
@:noCompletion
var _subStateClosed:FlxTypedSignal<FlxSubState->Void>;

public function new() {super();}
Geokureli marked this conversation as resolved.
Show resolved Hide resolved

/**
* This function is called after the game engine successfully switches states.
* Override this function, NOT the constructor, to initialize or set up your game state.
Expand Down
2 changes: 1 addition & 1 deletion flixel/system/FlxSplash.hx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class FlxSplash extends FlxState
#end
FlxG.stage.removeChild(_sprite);
FlxG.stage.removeChild(_text);
FlxG.switchState(Type.createInstance(nextState, []));
FlxG.switchState(() -> Type.createInstance(nextState, []));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nextState should also be a ()->FlxState, and the FlxGame constructor should take a function instead of a Class Ref

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm So like making the PlayState.new also in FlxGame?
I liked that you didn't need to add anything, just the name but ok

Copy link
Member

@Geokureli Geokureli May 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where before you had

addChild(new FlxGame(400, 300, MenuState);

now you can just do

addChild(new FlxGame(400, 300, MenuState.new);

the reason for this is to allow constuctor args, like

addChild(new FlxGame(400, 300, MenuState.new.bind(arg));

FlxG.game._gameJustStarted = true;
}
}