HaxeFlixel / flixel

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

FlxState.destroy() causes FlxG.resetState() to throw an error #3146

Closed BendyGaming0 closed 1 month ago

BendyGaming0 commented 1 month ago

Because the state.destroy() function is always called on the current state by FlxGame when switching or resetting states, it always throws an error when using a maker function for NextState as it calls the overwritten state._constructor field in NextState.createInstance()


Code snippet reproducing the issue:

package;

import flixel.FlxState;
import flixel.ui.FlxButton;

class PlayState extends FlxState
{
    override public function create():Void
    {
        var resetButton = new FlxButton(0, 0, "Reset", () -> FlxG.resetState());
        resetButton.screenCenter();
        add(resetButton);
    }
}

Observed behavior: FlxState throws an error saying that the state has been destroyed while attempting to use resetState

Expected behavior: The current state (PlayState) would be restarted using the previously passed in NextState maker function by FlxG.switchState() somewhere else

Geokureli commented 1 month ago

The provided code snippet does not reproduce the issue, when tested.

Perhaps your actual failing program is calling resetState directly in create rather on an event, which I do not recommend doing

BendyGaming0 commented 1 month ago

Found the issue. resetState was being passed into Void->Void variable that was called twice due to an oversight by the original developer, it wasn't noticed before as calling resetState twice did not throw an error prior to state switching changes. Sorry!