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

New signal (preStateCreate), dispatched with FlxState as parameter #1557

Merged
merged 1 commit into from
Jul 8, 2015
Merged

New signal (preStateCreate), dispatched with FlxState as parameter #1557

merged 1 commit into from
Jul 8, 2015

Conversation

j1mmie
Copy link

@j1mmie j1mmie commented Jul 7, 2015

I found this useful while working on a game with dependency injection and would like to propose it for the main line. I wanted a single listener to detect every time the state has changed, and automatically inject dependencies into that state.

The solution was to create a new signal, preStateCreate, that includes the newly-change-to FlxState as the payload of the signal. This new signal is dispatched after the stateChange signal, and before the .create() method is called on the new state (hence, "pre state create").

This new signal is dispatched after the stateChange signal, and before the `.create()`
method is called on the newly-changed-to state.
@Gama11
Copy link
Member

Gama11 commented Jul 8, 2015

Thanks!

That's actually a very elegant solution to this problem (without any breaking API changes!). Some people simply add arguments to the FlxState child class constructors, but that breaks any usage of FlxG.resetState since that attempts to create a new instance using Reflection without any arguments.

There's also the option to use static variables, but that's kind of a hack as well.

Gama11 added a commit that referenced this pull request Jul 8, 2015
New signal (preStateCreate), dispatched with FlxState as parameter
@Gama11 Gama11 merged commit b34973c into HaxeFlixel:dev Jul 8, 2015
@j1mmie
Copy link
Author

j1mmie commented Jul 8, 2015

Yeah I was originally adding an accessor to FlxGame for _newState, and using the old stateChanged signal. That seemed a bit hacky.

Thanks for the review. I'll check to see if there are some docs I can modify as a result of this addition.

@Gama11
Copy link
Member

Gama11 commented Jul 8, 2015

This also makes FlxSignal#addOnce() seem a lot more useful. :)

@j1mmie j1mmie deleted the pre-state-change-signal branch March 4, 2016 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants