From dc03e378dfdb3293421cb78ec59b9dbbc77b6ea1 Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Wed, 2 Nov 2022 02:23:45 +0100 Subject: [PATCH] hot reloading (F5) --- buildnumber.txt | 2 +- source/funkin/game/PlayState.hx | 3 +++ source/funkin/scripting/HScript.hx | 17 +++++++++++++++++ source/funkin/scripting/Script.hx | 7 +++++++ source/funkin/scripting/ScriptPack.hx | 4 ++++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/buildnumber.txt b/buildnumber.txt index 972fd778..20d91f52 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -958 \ No newline at end of file +963 \ No newline at end of file diff --git a/source/funkin/game/PlayState.hx b/source/funkin/game/PlayState.hx index 01a20da8..e5fae60d 100644 --- a/source/funkin/game/PlayState.hx +++ b/source/funkin/game/PlayState.hx @@ -746,6 +746,9 @@ class PlayState extends MusicBeatState DiscordClient.changePresence("Chart Editor", null, null, true); #end } + if (FlxG.keys.justPressed.F5) { + scripts.reload(); + } // FlxG.watch.addQuick('VOL', vocals.amplitudeLeft); // FlxG.watch.addQuick('VOLRight', vocals.amplitudeRight); diff --git a/source/funkin/scripting/HScript.hx b/source/funkin/scripting/HScript.hx index 1c51861b..9f008826 100644 --- a/source/funkin/scripting/HScript.hx +++ b/source/funkin/scripting/HScript.hx @@ -36,6 +36,23 @@ class HScript extends Script { if (expr != null) interp.execute(expr); } + public override function reload() { + // save variables + var savedVariables:Map = []; + for(k=>e in interp.variables) { + if (!Reflect.isFunction(e)) { + savedVariables[k] = e; + } + } + var oldParent = interp.scriptObject; + onCreate(path); + load(); + setParent(oldParent); + for(k=>e in savedVariables) { + interp.variables.set(k, e); + } + } + private override function onCall(funcName:String, parameters:Array):Dynamic { if (interp == null) return null; diff --git a/source/funkin/scripting/Script.hx b/source/funkin/scripting/Script.hx index 4d9c4323..7fa4c737 100644 --- a/source/funkin/scripting/Script.hx +++ b/source/funkin/scripting/Script.hx @@ -128,6 +128,13 @@ class Script extends FlxBasic implements IFlxDestroyable { curScript = oldScript; } + /** + * Hot-reloads the script, if possible + */ + public function reload() { + + } + /** * Calls the function `func` defined in the script. * @param func Name of the function diff --git a/source/funkin/scripting/ScriptPack.hx b/source/funkin/scripting/ScriptPack.hx index 6f0b2170..811864f7 100644 --- a/source/funkin/scripting/ScriptPack.hx +++ b/source/funkin/scripting/ScriptPack.hx @@ -43,6 +43,10 @@ class ScriptPack extends Script { return null; } + public override function reload() { + for(e in scripts) e.reload(); + } + public override function set(val:String, value:Dynamic) { for(e in scripts) e.set(val, value); }