HaxeFlixel / flixel

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

Investigate replacing Reflection with macros where possible (FlxTween, FlxBar, FlxSlider...) #1059

Open Gama11 opened 10 years ago

Gama11 commented 10 years ago

This would be a lot faster than the current VarTweens that use Reflection and more convenient than NumTweens with callbacks (probably also faster since callback functions are not typed statically), basically the perfect solution if we can get it to work. Tweens in tinkerbell seem to do this.

Also, tweening properties does not work on js. Using a macro would potentially allow us to work around that problem and generate tweens that call the setter function.

kevinresol commented 10 years ago

Cool idea!

JoeCreates commented 10 years ago

The same can go for FlxBar and FlxSlider. It gets a bit trickier when it comes to watching values with these, though. The current technique for watching can cause a single frame delay in many cases.

mastef commented 8 years ago

@Gama11 did you get anywhere with this?

I wonder how a VarTween with e.g. 3 properties ( ~ x, y, alpha ) performs vs 3 separate num Tweens.

Also used the setter functions on NumTweens - would be nice if this would be detected on compile time so otherwise useless Float setters wouldn't have to be added in the code, and the Tween Function Callback could be simply the Float itself.

mastef commented 8 years ago

Also Vartweens could detect on new() through Reflection what types it's bound to, so it could either switch over to Numtweens, or call the proper methods instead of using Reflection api. That could be interesting, too

JoeCreates commented 7 years ago

Just an update. I've made working tweens for Lycan with macros and no reflection or dynamics. It would change the Flixel api a lot to do something similar, but would ultimately make it more convenient as well as more performant.

// There is no need for tweens to have a target, so you can target any number of objects
// with a single tween. The tween function simply uses any field access expression. Even locals work.

var myLocal:Float = 0;
FlxTween.tween([player.x => 10, player.scale.x => 2, ob.x => 50, myLocal=> 1], 2)

// Or even compounding fields for the same objects...

FlxTween.tween([player => [x => 100, y => 100, scale => [x => 2, y => 2]]], 2)

// "a...b" means "tweens from a to b", allowing you to easily specify start values

FlxTween.tween([player.x => 100...200]);

// Last but not least, we are not restricted to only tweening fields. We can also tween methods...
FlxTween.tween([player.scale.set(1...2, 1...2)]);
Gama11 commented 7 years ago

That looks really nice! Wouldn't even have to be a breaking change if tween() is kept and the macro version has a different name.