From 0251fe87bbd807d5348aaabc0ec12cba2585d69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=8D=9A=7ENex?= <87421482+NexIsDumb@users.noreply.github.com> Date: Sun, 21 Sep 2025 21:43:57 +0200 Subject: [PATCH] making stages easier to be properly destroyed --- source/funkin/game/PlayState.hx | 12 ++++++++++-- source/funkin/game/Stage.hx | 32 ++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/source/funkin/game/PlayState.hx b/source/funkin/game/PlayState.hx index 5e6fdaf5..d1953984 100644 --- a/source/funkin/game/PlayState.hx +++ b/source/funkin/game/PlayState.hx @@ -1060,14 +1060,22 @@ class PlayState extends MusicBeatState } public override function destroy() { + var notNull = stage != null; + if (notNull) PlayState.instance.gameAndCharsCall("onStageDestroy", [stage]); scripts.call("destroy"); - for(g in __cachedGraphics) - g.useCount--; + + for (g in __cachedGraphics) g.useCount--; @:privateAccess { for (strumLine in strumLines.members) FlxG.sound.destroySound(strumLine.vocals); if (FlxG.sound.music != inst) FlxG.sound.destroySound(inst); FlxG.sound.destroySound(vocals); } + + if (notNull) { + stage.destroySilently(); + remove(stage, true); + } + scripts = FlxDestroyUtil.destroy(scripts); super.destroy(); diff --git a/source/funkin/game/Stage.hx b/source/funkin/game/Stage.hx index 5ef140b6..16c26a58 100644 --- a/source/funkin/game/Stage.hx +++ b/source/funkin/game/Stage.hx @@ -206,7 +206,7 @@ class Stage extends FlxBasic implements IBeatReceiver { } // idk lemme check anyways just in case scripts did smth - Nex - if (event != null) PlayState.instance.scripts.event("onPostStageCreation", event); + if (event != null) PlayState.instance.gameAndCharsEvent("onPostStageCreation", event); // shortlived scripts destroy when the stage finishes setting up - Nex for (info in xmlImportedScripts) if (info.shortLived) { @@ -349,17 +349,37 @@ class Stage extends FlxBasic implements IBeatReceiver { } } + /** + * Same of destroy, but doesn't call the various script events. + * @param destroySprites Whether the stage sprites should be destroyed + * @param destroyScript Whether the stage script should be destroyed + **/ + public function destroySilently(destroySprites:Bool = true, destroyScript:Bool = true) { + if (destroyScript && stageScript != null) { + if (PlayState.instance == state && PlayState.instance.scripts != null) PlayState.instance.scripts.remove(stageScript); + stageScript.destroy(); + } + + if (destroySprites) + for (e in stageSprites) + e?.destroy(); + + startCam.put(); + super.destroy(); + } + + public override function destroy() { + if (PlayState.instance == state && PlayState.instance.scripts != null) PlayState.instance.gameAndCharsCall("onStageDestroy", [this]); + stageScript?.call("destroy"); + destroySilently(); + } + public function beatHit(curBeat:Int) {} public function stepHit(curStep:Int) {} public function measureHit(curMeasure:Int) {} - public override function destroy() { - startCam.put(); - super.destroy(); - } - /** * Gets a list of stages that are available to be used. * @param mods Whenever only the mods folder should be checked