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

add SignalFrontEnd.preGameStart #2188

Merged
merged 11 commits into from
Nov 21, 2018
Merged

add SignalFrontEnd.preGameStart #2188

merged 11 commits into from
Nov 21, 2018

Conversation

Birdwards
Copy link
Contributor

I believe there should be a way to call a function after a FlxGame is initialized, but immediately before the game begins (i.e. immediately before the first state is created).

To give an example use case: I recently finished a game with a lot of FlxBitmapText objects that used the same font. In order to avoid calling FlxBitmapFont.fromAngelCode(), FlxBitmapFont.fromMonospace(), or FlxBitmapFont.fromXNA() every single time I created a new text object (which would've negatively affected performance, among other things), I created a single FlxBitmapFont object and passed it to FlxBitmapText.new() whenever I needed a new text object. But when could I initialize this single FlxBitmapFont object?

I couldn't initialize the FlxBitmapFont before initializing the FlxGame in Main.hx, because that throws an error; I believe this is because FlxBitmapFont requires some functionality that isn't set up until you initialize FlxGame. I couldn't add the font initialization to FlxG.signals.gameStarted either, because that would have been too late; gameStarted only dispatches after the initial state is created, and I needed to use the FlxBitmapFont in my initial state. I could only initialize my FlxBitmapFont after the creation of FlxGame, but before the creation of my initial state. I ended up writing a static function named init() to be called from the beginning of the create() function of the initial state, but this is a flawed approach- if the initial state is visited multiple times, init() is called multiple times, and if the initial state is changed, the init() call needs to be moved.

For a better solution, I added a preGameStart signal to SignalFrontEnd, which works just like gameStarted, except that it dispatches before the creation of the initial state, rather than after. Any function added to this signal is called once and only once, before the creation of the initial state, whatever the initial state may be. I also renamed gameStarted to postGameStart, to match naming conventions for other signals in SignalFrontEnd.

/**
* Gets dispatched when the game is started (first state after the splash screen).
*/
public var gameStarted(default, null):FlxSignal = new FlxSignal();
Copy link
Member

Choose a reason for hiding this comment

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

This breaks backwards compatibility. Perhaps we could keep a dummy gameStarted property with (get, never) access, marked as @:deprecated. The getter would simply "redirect" to postGameStart.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. I've added your suggestion to the branch.

@Gama11 Gama11 merged commit 25198ae into HaxeFlixel:dev Nov 21, 2018
@Gama11
Copy link
Member

Gama11 commented Nov 21, 2018

Thanks!

@MSGhero
Copy link
Member

MSGhero commented Nov 21, 2018

Oh man I do so many static init functions for the same reason

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.

3 participants