Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.

Commit e34e435

Browse files
committed
CHG | Enhance CustomStage script execution with threading support and update ModsMenuState to use Game.restartGame
1 parent e1d34e3 commit e34e435

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

source/Game.hx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package;
2+
3+
import states.TitleState;
4+
import states.FreeplayState;
5+
6+
class Game extends MusicBeatState {
7+
public static function restartGame() {
8+
//MusicBeatState.switchCustomStage("TitleState");
9+
TitleState.initialized = false;
10+
TitleState.closedState = false;
11+
FlxG.sound.music.fadeOut(0.3);
12+
if(FreeplayState.vocals != null)
13+
{
14+
FreeplayState.vocals.fadeOut(0.3);
15+
FreeplayState.vocals = null;
16+
}
17+
FlxG.camera.fade(FlxColor.BLACK, 0.5, false, FlxG.resetGame, false);
18+
}
19+
}

source/import.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#if !macro
22
// Engine Config DONT DELETE THIS OR IT WILL BREAK EVERYTHING
33
import EngineConfig;
4+
import Game;
45

56
//Discord API
67
#if DISCORD_ALLOWED

source/states/CustomStage.hx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,23 @@ class CustomStage extends MusicBeatState {
4040
trace("[CustomStage] Script API injected successfully for " + script.name + ".");
4141
}
4242

43-
private function tryCall(script:HScript, func:String, args:Array<Dynamic> = null):Void {
43+
private function tryCall(script:HScript, func:String, args:Array<Dynamic> = null, threaded:Bool = false):Void {
4444
if (script.exists(func)) {
45-
Subprocess.run(() -> {
45+
if (threaded) {
46+
Subprocess.run(() -> {
47+
try {
48+
script.call(func, args);
49+
} catch (e:haxe.Exception) {
50+
trace('[${script.name}] Error in $func(): ${e.message}');
51+
}
52+
});
53+
} else {
4654
try {
4755
script.call(func, args);
4856
} catch (e:haxe.Exception) {
4957
trace('[${script.name}] Error in $func(): ${e.message}');
5058
}
51-
});
59+
}
5260
}
5361
}
5462

@@ -57,7 +65,7 @@ class CustomStage extends MusicBeatState {
5765
try {
5866
newScript = new HScript(null, file);
5967
stageAPI(newScript);
60-
tryCall(newScript, "onCreate");
68+
tryCall(newScript, "onCreate", null, true);
6169
hscriptArray.push(newScript);
6270
trace('Initialized HScript successfully: $file');
6371
} catch (e:IrisError) {
@@ -85,7 +93,7 @@ class CustomStage extends MusicBeatState {
8593

8694
public function new(stateName:String) {
8795
super();
88-
Mods.loadTopMod();
96+
// Mods.loadTopMod();
8997
instance = this;
9098
stageName = stateName;
9199
stagePath = Paths.customStage(stateName);
@@ -123,8 +131,9 @@ class CustomStage extends MusicBeatState {
123131
}
124132
if (combinedError != "") {
125133
MusicBeatState.switchState(new states.ErrorState(
126-
"HMM, you got some error in your code:\n" + combinedError + "\n\nPress ACCEPT to reload CustomStage.",
127-
function() reload()
134+
"HMM, you got some error in your code:\n" + combinedError + "\n\nPress ACCEPT to reload CustomStage.\n\nPress ESC to restart game ( Not recommended to Restart Game )",
135+
function() reload(),
136+
function() Game.restartGame()
128137
));
129138
}
130139
}

source/states/ModsMenuState.hx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,7 @@ class ModsMenuState extends MusicBeatState
314314
FlxG.sound.play(Paths.sound('cancelMenu'));
315315
if(waitingToRestart)
316316
{
317-
//MusicBeatState.switchCustomStage("TitleState");
318-
TitleState.initialized = false;
319-
TitleState.closedState = false;
320-
FlxG.sound.music.fadeOut(0.3);
321-
if(FreeplayState.vocals != null)
322-
{
323-
FreeplayState.vocals.fadeOut(0.3);
324-
FreeplayState.vocals = null;
325-
}
326-
FlxG.camera.fade(FlxColor.BLACK, 0.5, false, FlxG.resetGame, false);
317+
Game.restartGame();
327318
}
328319
else MusicBeatState.switchCustomStage("MainMenuState");
329320

0 commit comments

Comments
 (0)