Skip to content

Commit 0251fe8

Browse files
committed
making stages easier to be properly destroyed
1 parent b7c932f commit 0251fe8

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

source/funkin/game/PlayState.hx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,14 +1060,22 @@ class PlayState extends MusicBeatState
10601060
}
10611061

10621062
public override function destroy() {
1063+
var notNull = stage != null;
1064+
if (notNull) PlayState.instance.gameAndCharsCall("onStageDestroy", [stage]);
10631065
scripts.call("destroy");
1064-
for(g in __cachedGraphics)
1065-
g.useCount--;
1066+
1067+
for (g in __cachedGraphics) g.useCount--;
10661068
@:privateAccess {
10671069
for (strumLine in strumLines.members) FlxG.sound.destroySound(strumLine.vocals);
10681070
if (FlxG.sound.music != inst) FlxG.sound.destroySound(inst);
10691071
FlxG.sound.destroySound(vocals);
10701072
}
1073+
1074+
if (notNull) {
1075+
stage.destroySilently();
1076+
remove(stage, true);
1077+
}
1078+
10711079
scripts = FlxDestroyUtil.destroy(scripts);
10721080

10731081
super.destroy();

source/funkin/game/Stage.hx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class Stage extends FlxBasic implements IBeatReceiver {
206206
}
207207

208208
// idk lemme check anyways just in case scripts did smth - Nex
209-
if (event != null) PlayState.instance.scripts.event("onPostStageCreation", event);
209+
if (event != null) PlayState.instance.gameAndCharsEvent("onPostStageCreation", event);
210210

211211
// shortlived scripts destroy when the stage finishes setting up - Nex
212212
for (info in xmlImportedScripts) if (info.shortLived) {
@@ -349,17 +349,37 @@ class Stage extends FlxBasic implements IBeatReceiver {
349349
}
350350
}
351351

352-
public function beatHit(curBeat:Int) {}
353-
354-
public function stepHit(curStep:Int) {}
352+
/**
353+
* Same of destroy, but doesn't call the various script events.
354+
* @param destroySprites Whether the stage sprites should be destroyed
355+
* @param destroyScript Whether the stage script should be destroyed
356+
**/
357+
public function destroySilently(destroySprites:Bool = true, destroyScript:Bool = true) {
358+
if (destroyScript && stageScript != null) {
359+
if (PlayState.instance == state && PlayState.instance.scripts != null) PlayState.instance.scripts.remove(stageScript);
360+
stageScript.destroy();
361+
}
355362

356-
public function measureHit(curMeasure:Int) {}
363+
if (destroySprites)
364+
for (e in stageSprites)
365+
e?.destroy();
357366

358-
public override function destroy() {
359367
startCam.put();
360368
super.destroy();
361369
}
362370

371+
public override function destroy() {
372+
if (PlayState.instance == state && PlayState.instance.scripts != null) PlayState.instance.gameAndCharsCall("onStageDestroy", [this]);
373+
stageScript?.call("destroy");
374+
destroySilently();
375+
}
376+
377+
public function beatHit(curBeat:Int) {}
378+
379+
public function stepHit(curStep:Int) {}
380+
381+
public function measureHit(curMeasure:Int) {}
382+
363383
/**
364384
* Gets a list of stages that are available to be used.
365385
* @param mods Whenever only the mods folder should be checked

0 commit comments

Comments
 (0)