HaxeFlixel / flixel

Free, cross-platform 2D game engine powered by Haxe and OpenFL
https://haxeflixel.com/
MIT License
1.93k stars 431 forks source link

Consider dropping FlxState.create #1083

Open Gama11 opened 10 years ago

Gama11 commented 10 years ago

Constructor params are discouraged, there's no real reason to let the user create an instance of the state himself. switchState() would take a Class.

JoeCreates commented 10 years ago

Why not have a system which lets the user pass a state around and witch to a state which is already instantiated?

Ideally, I'd say it should be possible to use the state constructor to initialise a state, and an onEnter for things that are reset only when the state is entered, thus allowing for persistent states.

Gama11 commented 10 years ago

I don't like the idea of persistent states tbh. The whole point is that there's only ever one state, except for substates.

JoeCreates commented 10 years ago

The point is not that there exists only one state. The point is that you are only ever in one state. Whether or not inactive states. Really, it's slightly more complex than this, even, as to allow for decent state transitions you need to be able to render/update two states at once.

impaler commented 10 years ago

I always create a wrapper to switch by class type and type.createinstance anyway.

@joecreates flixel doesn't support two states at a time that's a different issue. Id rather serialize in some form than manage persistent states but it depends on the use case.

JoeCreates commented 10 years ago

@impaler Perhaps it is a different issue, but I'd say it's pretty relevant to how states actually end up being created.

gamedevsam commented 10 years ago

I think we should either keep the current implementation (by passing around classes with blank constructors), or allow users to instantiate their own states and change what state is active using a state machine (perhaps using FlxFSM currently being proposed for flixel-addons).

Gama11 commented 10 years ago

@gamedevsam The current situation is a middle-ground solution. You can instantiate states yourself in switchState() and thus use constructor params there - resetState() however assumes that there are no constructor params and calls Type.createInstance() with an empty array as constructor params. It's inconsistent / confusing.

Tiago-Ling commented 10 years ago

@Sam,

Currently implementation already makes it possible to use switchState(new MyState(a, b)); and talking with other people it seems that many of them already makes use of this feature.

Tiago Ling Alexandre Tel: +55 41 8819-3191

2014-05-13 14:03 GMT-03:00 Samuel Batista notifications@github.com:

I think we should either keep the current implementation (by passing around classes with blank constructors), or allow users to instantiate their own states and change what state is active using a state machine (perhaps using FlxFSMhttps://github.com/HaxeFlixel/flixel-addons/pull/72currently being proposed for flixel-addons). A middle of the ground solution doesn't seem that much better than what we currently have.

— Reply to this email directly or view it on GitHubhttps://github.com/HaxeFlixel/flixel/issues/1083#issuecomment-42982798 .

gamedevsam commented 10 years ago

In that case, I suggest we leave this alone, or adopt a robust state machine that would handle state stacking. That would eliminate the need for FlxSubStates, allowing users to push any state on top of another.

Tiago-Ling commented 10 years ago

@gamedevsam, Agreed. About a more robust approach, pushdown automata appears to be a good place to look:

http://en.wikipedia.org/wiki/Pushdown_automaton

Tiago Ling Alexandre Tel: +55 41 8819-3191

gamedevsam commented 10 years ago

Here are some more resources regarding state stacks: http://gamedev.stackexchange.com/questions/1783/game-state-stack http://gamedevelopment.tutsplus.com/tutorials/finite-state-machines-theory-and-implementation--gamedev-11867 (especially the part that mentions Stack-Based FSM)

Here's a sample implementation of a game state FSM on XNA: http://xbox.create.msdn.com/en-US/education/catalog/sample/game_state_management

JoshCheek commented 9 years ago

It would be really nice to be able to instantiate my own state. I'm just starting, so maybe there's a solution I don't know about, but FlxGame instantiating _initialState without args means I can't pass it initialization arguments, so I have to create global state (Presumably I could use a macro to create a new class dynamically with a binding into the context that created it, but I wasn't able to get that to work).

Also, it's just generally confusing that FlxState says to use create instead of new, b/c then I don't know how to initialize the object. I looked at the tutorial to see how they do it, and they define new, so mixed messages here. Personally, I'm going to use new instead of create, because I can actually call that one.

Gama11 commented 9 years ago

I hadn't even noticed the tutorial does that, that's kind of an issue considering that breaks FlxG.resetState()..