From 842ec2c1cd0243741170c431e4095bad2a4842e0 Mon Sep 17 00:00:00 2001 From: Abnormal <86753001+AbnormalPoof@users.noreply.github.com> Date: Thu, 22 May 2025 03:22:44 -0500 Subject: [PATCH] feat: Scoped Modules --- source/funkin/modding/module/Module.hx | 13 ++++++++++++- source/funkin/modding/module/ModuleHandler.hx | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/source/funkin/modding/module/Module.hx b/source/funkin/modding/module/Module.hx index c04084f87..ae689c54b 100644 --- a/source/funkin/modding/module/Module.hx +++ b/source/funkin/modding/module/Module.hx @@ -39,16 +39,27 @@ class Module implements IPlayStateScriptedClass implements IStateChangingScripte return value; } + /** + * The state this module is associated with. + * If set, this module will only receive events when the game is in this state. + */ + public var state:Null> = null; + /** * Called when the module is initialized. * It may not be safe to reference other modules here since they may not be loaded yet. * * NOTE: To make the module start inactive, call `this.active = false` in the constructor. */ - public function new(moduleId:String, priority:Int = 1000):Void + public function new(moduleId:String, priority:Int = 1000, state:Null> = null):Void { this.moduleId = moduleId; this.priority = priority; + + if (state != null) + { + this.state = state; + } } public function toString() diff --git a/source/funkin/modding/module/ModuleHandler.hx b/source/funkin/modding/module/ModuleHandler.hx index e4e0ab7d3..46ab8eac4 100644 --- a/source/funkin/modding/module/ModuleHandler.hx +++ b/source/funkin/modding/module/ModuleHandler.hx @@ -6,6 +6,7 @@ import funkin.modding.events.ScriptEvent; import funkin.modding.events.ScriptEventDispatcher; import funkin.modding.module.Module; import funkin.modding.module.ScriptedModule; +import flixel.FlxG; /** * Utility functions for loading and manipulating active modules. @@ -145,6 +146,14 @@ class ModuleHandler // The module needs to be active to receive events. if (module != null && module.active) { + if (module.state != null) + { + // Only call the event if the current state is what the module's state is. + if (!(Type.getClass(FlxG.state) == module.state) && !(Type.getClass(FlxG.state?.subState) == module.state)) + { + continue; + } + } ScriptEventDispatcher.callEvent(module, event); } }