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

Pass FlxG.elapsed through update methods #1188

Closed
JoeCreates opened this issue Jun 22, 2014 · 8 comments
Closed

Pass FlxG.elapsed through update methods #1188

JoeCreates opened this issue Jun 22, 2014 · 8 comments

Comments

@JoeCreates
Copy link
Member

Most game engines tend to pass a time delta down through update() methods, whereas HaxeFlixel uses a single static variable FlxG.elapsed. The problem with this is that it gives no control to updating classes over how quickly they should update.

HaxeFlixel has FlxG.timeScale for if you wish to slow down or speed up the game, but when is it ever useful to slow down or speed up everything? Generally if you want to have such an effect, you'll have a HUD updating at a constant rate while the world updates at a different speed.

For this reason, I would suggest update() methods are replaced with update(elapsed:Int). It would this be trivial for any updating object to have it's own elapsed coefficient for changing the speed at which its members update.

It might be possible to stop this change from being a breaking change by making elapsed optional?

@Gama11
Copy link
Member

Gama11 commented Jun 22, 2014

It doesn't matter if it's optional or not; when overriding functions signatures must match. And considering super.update() would require this new additional param, it being optional doesn't make too much sense either (not to mention you would have to check for null everwhere). You also can't really "ignore" the param (update(_)) because it's necessary for the super call.

This would be a huge breaking change, and I'm not sure it's worth it. It could be handled via find + replace though.

May be worth noting: Flambe has this already, HaxePunk does not.

@JoeCreates
Copy link
Member Author

The slowing/speeding effect pretty fundamental capability when it comes to polish, and certain game mechanics aren't really possible without it. You'd be hard pushed to find a AAA game that doesn't use it somewhere, even if there's not really any gameplay effect.

Other libs that do this (with render too in some cases) include LWJGL, Slick, LibGDX, and XNA/Monogame. Unity, on the other hand, does not as far as I can tell.

I'm also trying to think of other ways of implementing the effect. One way might be to give FlxObject or FlxBasic their own timescale property, and replace their uses of FlxG.elapsed with a local property elapsed that takes into account it's own timescale. This might be achievable without breaking anything?

Another option, but this one rather hacky and could potentially break in some circumstances, would require FlxG.elapsed to be writeable (whereas at the moment it is read-only). Then this would be possible:

override public function update():Void {
    var realElapsed:Float = FlxG.elapsed;
    FlxG.elapsed *= timeScale;
    super.update();
    FlxG.elapsed = realElapsed;
}

@gamedevsam
Copy link
Contributor

I've done that hack before, it's pretty nasty, but it works. I don't think it's worth making this change, it would be a huge headache for all existing users.

@JoeCreates
Copy link
Member Author

@gamedevsam That hack isn't even possible right now. FlxG.elapsed cannot be written to.

@gamedevsam
Copy link
Contributor

Maybe it should be then....

@Gama11
Copy link
Member

Gama11 commented Jun 23, 2014

Not sure we should encourage that kind of hack... there might be better solutions.

@gamedevsam
Copy link
Contributor

The only other possible solution I see atm is what @JoeCreates mentioned, adding a timeScale property to FlxSprites and FlxGroups to apply selective slow-mo or speedup.

@MSGhero
Copy link
Member

MSGhero commented Jul 9, 2014

How would this work for FlxGroups? Children's timeScale gets multiplied/divided by the Group's when added/removed? Does that mean that values of 0 shouldn't be allowed to avoid a divide-by-zero?

How would the new timeScale affect FlxAnimation (maybe others), since that uses FlxG.timeScale directly?

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

No branches or pull requests

4 participants