From 07ae6b643c573e300bb4ed64546389def02fcd12 Mon Sep 17 00:00:00 2001 From: ItsLJcool Date: Fri, 4 Jul 2025 16:46:42 -0700 Subject: [PATCH] FINALLY fixed the issue with GlobalState blowing up when pressing F5. (#666) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * peak is happening RED * oops * oops x2 forgot I left a trace when loading up global.hx from `Assets` lol * **State / SubState reloading broken rn but pushing changes to see if anyone can fix** * Human... I remember you disabling reloading substates cuz it broke... * forgor to edit assets global.hx * lemme see * kk * opa --------- Co-authored-by: ⍚~Nex <87421482+NexIsDumb@users.noreply.github.com> --- assets/data/global.hx | 3 -- assets/songs/stress/cutscene.hx | 2 - source/funkin/backend/MusicBeatState.hx | 20 +++++----- source/funkin/backend/MusicBeatSubstate.hx | 16 ++++---- .../funkin/backend/scripting/GlobalScript.hx | 39 +++++++++++++------ source/funkin/backend/scripting/ScriptPack.hx | 2 +- source/funkin/backend/system/Controls.hx | 23 +++++++++++ source/funkin/game/PlayState.hx | 12 +----- source/funkin/options/Options.hx | 39 ++++++++++++++++--- .../options/keybinds/KeybindsOptions.hx | 9 +++++ 10 files changed, 115 insertions(+), 50 deletions(-) delete mode 100644 assets/data/global.hx diff --git a/assets/data/global.hx b/assets/data/global.hx deleted file mode 100644 index 4c49ef69..00000000 --- a/assets/data/global.hx +++ /dev/null @@ -1,3 +0,0 @@ -// -function update(elapsed:Float) - if (FlxG.keys.justPressed.F5) FlxG.resetState(); \ No newline at end of file diff --git a/assets/songs/stress/cutscene.hx b/assets/songs/stress/cutscene.hx index dd55a3da..7684f8b1 100644 --- a/assets/songs/stress/cutscene.hx +++ b/assets/songs/stress/cutscene.hx @@ -59,8 +59,6 @@ function create() { } function update(elapsed) { - if (FlxG.keys.justPressed.F5) - FlxG.resetState(); switch(step) { case 0: lipSync(tankman, 0, 16750); diff --git a/source/funkin/backend/MusicBeatState.hx b/source/funkin/backend/MusicBeatState.hx index 6ef650bc..cfb632ff 100644 --- a/source/funkin/backend/MusicBeatState.hx +++ b/source/funkin/backend/MusicBeatState.hx @@ -101,6 +101,8 @@ class MusicBeatState extends FlxState implements IBeatReceiver public static var skipTransOut:Bool = false; public static var skipTransIn:Bool = false; + public static var ALLOW_DEBUG_RELOAD:Bool = true; + inline function get_controls():Controls return PlayerSettings.solo.controls; inline function get_controlsP1():Controls @@ -147,16 +149,20 @@ class MusicBeatState extends FlxState implements IBeatReceiver call("postUpdate", [elapsed]); } - if (_requestSubStateReset) - { + if (_requestSubStateReset) { _requestSubStateReset = false; resetSubState(); } - if (subState != null) - { - subState.tryUpdate(elapsed); + + if (/*subState == null && */(ALLOW_DEBUG_RELOAD && controls.DEBUG_RELOAD)) { + Logs.trace("Reloading Current State...", INFO, YELLOW); + FlxG.resetState(); } + + if (subState != null) + subState.tryUpdate(elapsed); } + override function create() { loadScript(); @@ -189,10 +195,6 @@ class MusicBeatState extends FlxState implements IBeatReceiver override function update(elapsed:Float) { - // TODO: DEBUG MODE!! - if (FlxG.keys.justPressed.F5) { - loadScript(); - } call("update", [elapsed]); super.update(elapsed); diff --git a/source/funkin/backend/MusicBeatSubstate.hx b/source/funkin/backend/MusicBeatSubstate.hx index e2bbb312..4a4d3de8 100644 --- a/source/funkin/backend/MusicBeatSubstate.hx +++ b/source/funkin/backend/MusicBeatSubstate.hx @@ -126,15 +126,19 @@ class MusicBeatSubstate extends FlxSubState implements IBeatReceiver call("postUpdate", [elapsed]); } - if (_requestSubStateReset) - { + // if (subState == null && (MusicBeatState.ALLOW_DEBUG_RELOAD && controls.DEBUG_RELOAD)) { + // Logs.trace("Reloading Current SubState...", INFO, YELLOW); + // var test = Type.createInstance(Type.getClass(this), [this.scriptsAllowed, this.scriptName]); + // parent.openSubState(test); + // } + + if (_requestSubStateReset) { _requestSubStateReset = false; resetSubState(); } + if (subState != null) - { subState.tryUpdate(elapsed); - } } override function close() { @@ -171,10 +175,6 @@ class MusicBeatSubstate extends FlxSubState implements IBeatReceiver override function update(elapsed:Float) { - // TODO: DEBUG MODE!! - if (FlxG.keys.justPressed.F5) { - loadScript(); - } call("update", [elapsed]); super.update(elapsed); } diff --git a/source/funkin/backend/scripting/GlobalScript.hx b/source/funkin/backend/scripting/GlobalScript.hx index 5c037aec..a4dca576 100644 --- a/source/funkin/backend/scripting/GlobalScript.hx +++ b/source/funkin/backend/scripting/GlobalScript.hx @@ -11,6 +11,9 @@ import funkin.backend.assets.ModsFolder; class GlobalScript { public static var scripts:ScriptPack; + private static var reloading:Bool = false; + private static var _lastAllow_Reload:Bool = false; + public static function init() { #if MOD_SUPPORT ModsFolder.onModSwitch.add(onModSwitch); @@ -42,16 +45,12 @@ class GlobalScript { }); FlxG.signals.postUpdate.add(function() { call("postUpdate", [FlxG.elapsed]); - if (FlxG.keys.justPressed.F5) { - if (scripts.scripts.length > 0) { - Logs.trace('Reloading global script...', WARNING, YELLOW); - scripts.reload(); - Logs.trace('Global script successfully reloaded.', WARNING, GREEN); - } else { - Logs.trace('Loading global script...', WARNING, YELLOW); - onModSwitch(#if MOD_SUPPORT ModsFolder.currentModFolder #else null #end); - } + + if (reloading) { + reloading = false; + MusicBeatState.ALLOW_DEBUG_RELOAD = _lastAllow_Reload; } + if (FlxG.keys.justPressed.F2) NativeAPI.allocConsole(); }); @@ -70,12 +69,29 @@ class GlobalScript { FlxG.signals.preStateSwitch.add(function() { call("preStateSwitch", []); }); + FlxG.signals.preUpdate.add(function() { call("preUpdate", [FlxG.elapsed]); call("update", [FlxG.elapsed]); - }); - onModSwitch(#if MOD_SUPPORT ModsFolder.currentModFolder #else null #end); + if (FlxG.keys.pressed.SHIFT) { + var resetKey = FlxG.keys.justPressed.F5; // we default the key to F5, but really this shouldn't matter, as every state will be at minimum a MusicBeatState. + if ((FlxG.state is MusicBeatState)) resetKey = cast(FlxG.state, MusicBeatState).controls.DEBUG_RELOAD; + + // If we want, we could just make reseting GlobalScript it's own keybind, but for now this works. + if (resetKey) { + reloading = true; + Logs.trace("Reloading Global Scripts...", INFO, YELLOW); + + // yeah its a bit messy, sorry. This just prevents actually reloading the actual state. + _lastAllow_Reload = MusicBeatState.ALLOW_DEBUG_RELOAD; + MusicBeatState.ALLOW_DEBUG_RELOAD = false; + + // Would be better to just re-initalize GlobalScript so there aren't any lose ends. + onModSwitch(#if MOD_SUPPORT ModsFolder.currentModFolder #else null #end); + } + } + }); } public static function event(name:String, event:T):T { @@ -88,6 +104,7 @@ class GlobalScript { if (scripts != null) scripts.call(name, args); } + public static function onModSwitch(newMod:String) { call("destroy"); scripts = FlxDestroyUtil.destroy(scripts); diff --git a/source/funkin/backend/scripting/ScriptPack.hx b/source/funkin/backend/scripting/ScriptPack.hx index 32c590fc..30107746 100644 --- a/source/funkin/backend/scripting/ScriptPack.hx +++ b/source/funkin/backend/scripting/ScriptPack.hx @@ -53,7 +53,7 @@ class ScriptPack extends Script { public override function call(func:String, ?parameters:Array):Dynamic { for(e in scripts) - if(e.active) + if (e.active) e.call(func, parameters); return null; } diff --git a/source/funkin/backend/system/Controls.hx b/source/funkin/backend/system/Controls.hx index a09da1c7..113ded8b 100644 --- a/source/funkin/backend/system/Controls.hx +++ b/source/funkin/backend/system/Controls.hx @@ -44,6 +44,9 @@ enum abstract Action(String) to String from String { var RESET = "reset"; var CHEAT = "cheat"; var SWITCHMOD = "switchmod"; + + // Debugs + var DEBUG_RELOAD = "debug-reload"; } enum Device @@ -73,6 +76,9 @@ enum Control PAUSE; CHEAT; SWITCHMOD; + + // Debugs + DEBUG_RELOAD; } enum KeyboardScheme @@ -122,6 +128,8 @@ class Controls extends FlxActionSet var _reset = new FlxActionDigital(Action.RESET); var _cheat = new FlxActionDigital(Action.CHEAT); var _switchMod = new FlxActionDigital(Action.SWITCHMOD); + + var debug_reload = new FlxActionDigital(Action.DEBUG_RELOAD); #if (haxe >= "4.0.0") var byName:Map = []; @@ -372,6 +380,14 @@ class Controls extends FlxActionSet inline function set_SWITCHMOD(val) return @:privateAccess _switchMod._checked = val; + public var DEBUG_RELOAD(get, set):Bool; + + inline function get_DEBUG_RELOAD() + return debug_reload.check(); + + inline function set_DEBUG_RELOAD(val) + return @:privateAccess debug_reload._checked = val; + public function new(name, scheme = None) { super(name); @@ -408,6 +424,7 @@ class Controls extends FlxActionSet add(_reset); add(_cheat); add(_switchMod); + add(debug_reload); for (action in digitalActions) byName[action.name] = action; @@ -469,6 +486,7 @@ class Controls extends FlxActionSet case RESET: _reset; case CHEAT: _cheat; case SWITCHMOD: _switchMod; + case DEBUG_RELOAD: debug_reload; } } @@ -532,6 +550,8 @@ class Controls extends FlxActionSet func(_cheat, JUST_PRESSED); case SWITCHMOD: func(_switchMod, JUST_PRESSED); + case DEBUG_RELOAD: + func(debug_reload, JUST_PRESSED); } } @@ -687,6 +707,7 @@ class Controls extends FlxActionSet inline bindKeys(Control.PAUSE, Options.SOLO_PAUSE); inline bindKeys(Control.RESET, Options.SOLO_RESET); inline bindKeys(Control.SWITCHMOD, Options.SOLO_SWITCHMOD); + inline bindKeys(Control.DEBUG_RELOAD, Options.SOLO_DEBUG_RELOAD); case Duo(true): inline bindKeys(Control.UP, Options.P1_UP); inline bindKeys(Control.DOWN, Options.P1_DOWN); @@ -701,6 +722,7 @@ class Controls extends FlxActionSet inline bindKeys(Control.PAUSE, Options.P1_PAUSE); inline bindKeys(Control.RESET, Options.P1_RESET); inline bindKeys(Control.SWITCHMOD, Options.P1_SWITCHMOD); + inline bindKeys(Control.DEBUG_RELOAD, Options.P1_DEBUG_RELOAD); case Duo(false): inline bindKeys(Control.UP, Options.P2_UP); inline bindKeys(Control.DOWN, Options.P2_DOWN); @@ -715,6 +737,7 @@ class Controls extends FlxActionSet inline bindKeys(Control.PAUSE, Options.P2_PAUSE); inline bindKeys(Control.RESET, Options.P2_RESET); inline bindKeys(Control.SWITCHMOD, Options.P2_SWITCHMOD); + inline bindKeys(Control.DEBUG_RELOAD, Options.P2_DEBUG_RELOAD); case None: // nothing case Custom: // nothing } diff --git a/source/funkin/game/PlayState.hx b/source/funkin/game/PlayState.hx index bff696ce..3d416095 100644 --- a/source/funkin/game/PlayState.hx +++ b/source/funkin/game/PlayState.hx @@ -1266,16 +1266,8 @@ class PlayState extends MusicBeatState if (updateRatingStuff != null) updateRatingStuff(); - if (canAccessDebugMenus) { - if (chartingMode && FlxG.keys.justPressed.SEVEN) { - FlxG.switchState(new funkin.editors.charter.Charter(SONG.meta.name, difficulty, false)); - } - if (FlxG.keys.justPressed.F5) { - Logs.trace('Reloading scripts...', WARNING, YELLOW); - scripts.reload(); - Logs.trace('Song scripts successfully reloaded.', WARNING, GREEN); - } - } + if (canAccessDebugMenus && chartingMode && FlxG.keys.justPressed.SEVEN) + FlxG.switchState(new funkin.editors.charter.Charter(SONG.meta.name, difficulty, false)); if (doIconBop) for (icon in iconArray) diff --git a/source/funkin/options/Options.hx b/source/funkin/options/Options.hx index a9bf72ac..a191c2ad 100644 --- a/source/funkin/options/Options.hx +++ b/source/funkin/options/Options.hx @@ -69,12 +69,16 @@ class Options public static var charterAutoSavesSeperateFolder:Bool = false; /** - * PLAYER 1 CONTROLS - */ + * PLAYER 1 CONTROLS + */ + + // Notes public static var P1_NOTE_LEFT:Array = [A]; public static var P1_NOTE_DOWN:Array = [S]; public static var P1_NOTE_UP:Array = [W]; public static var P1_NOTE_RIGHT:Array = [D]; + + // Menus public static var P1_LEFT:Array = [A]; public static var P1_DOWN:Array = [S]; public static var P1_UP:Array = [W]; @@ -82,16 +86,25 @@ class Options public static var P1_ACCEPT:Array = [ENTER]; public static var P1_BACK:Array = [BACKSPACE]; public static var P1_PAUSE:Array = [ENTER]; + + // Misc public static var P1_RESET:Array = [R]; public static var P1_SWITCHMOD:Array = [TAB]; + + // Debugs + public static var P1_DEBUG_RELOAD:Array = [F5]; /** - * PLAYER 2 CONTROLS (ALT) - */ + * PLAYER 2 CONTROLS (ALT) + */ + + // Notes public static var P2_NOTE_LEFT:Array = [LEFT]; public static var P2_NOTE_DOWN:Array = [DOWN]; public static var P2_NOTE_UP:Array = [UP]; public static var P2_NOTE_RIGHT:Array = [RIGHT]; + + // Menus public static var P2_LEFT:Array = [LEFT]; public static var P2_DOWN:Array = [DOWN]; public static var P2_UP:Array = [UP]; @@ -99,16 +112,25 @@ class Options public static var P2_ACCEPT:Array = [SPACE]; public static var P2_BACK:Array = [ESCAPE]; public static var P2_PAUSE:Array = [ESCAPE]; + + // Misc public static var P2_RESET:Array = []; public static var P2_SWITCHMOD:Array = []; + + // Debugs + public static var P2_DEBUG_RELOAD:Array = []; /** - * SOLO GETTERS - */ + * SOLO GETTERS + */ + + // Notes public static var SOLO_NOTE_LEFT(get, null):Array; public static var SOLO_NOTE_DOWN(get, null):Array; public static var SOLO_NOTE_UP(get, null):Array; public static var SOLO_NOTE_RIGHT(get, null):Array; + + // Menus public static var SOLO_LEFT(get, null):Array; public static var SOLO_DOWN(get, null):Array; public static var SOLO_UP(get, null):Array; @@ -116,9 +138,14 @@ class Options public static var SOLO_ACCEPT(get, null):Array; public static var SOLO_BACK(get, null):Array; public static var SOLO_PAUSE(get, null):Array; + + // Misc public static var SOLO_RESET(get, null):Array; public static var SOLO_SWITCHMOD(get, null):Array; + // Debugs + public static var SOLO_DEBUG_RELOAD(get, null):Array; + public static function load() { if (__save == null) __save = new FlxSave(); __save.bind("options", "CodenameEngine"); diff --git a/source/funkin/options/keybinds/KeybindsOptions.hx b/source/funkin/options/keybinds/KeybindsOptions.hx index e4e7d26c..b7cb6bbb 100644 --- a/source/funkin/options/keybinds/KeybindsOptions.hx +++ b/source/funkin/options/keybinds/KeybindsOptions.hx @@ -74,6 +74,15 @@ class KeybindsOptions extends MusicBeatSubstate { control: 'SWITCHMOD' }, ] + }, + { + name: 'DEBUG', + settings: [ + { + name: 'Reload', + control: 'DEBUG_RELOAD' + }, + ] } ];