Skip to content

Commit 842ec2c

Browse files
AbnormalPoofEliteMasterEric
authored andcommitted
feat: Scoped Modules
1 parent 4268462 commit 842ec2c

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

source/funkin/modding/module/Module.hx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,27 @@ class Module implements IPlayStateScriptedClass implements IStateChangingScripte
3939
return value;
4040
}
4141

42+
/**
43+
* The state this module is associated with.
44+
* If set, this module will only receive events when the game is in this state.
45+
*/
46+
public var state:Null<Class<Dynamic>> = null;
47+
4248
/**
4349
* Called when the module is initialized.
4450
* It may not be safe to reference other modules here since they may not be loaded yet.
4551
*
4652
* NOTE: To make the module start inactive, call `this.active = false` in the constructor.
4753
*/
48-
public function new(moduleId:String, priority:Int = 1000):Void
54+
public function new(moduleId:String, priority:Int = 1000, state:Null<Class<Dynamic>> = null):Void
4955
{
5056
this.moduleId = moduleId;
5157
this.priority = priority;
58+
59+
if (state != null)
60+
{
61+
this.state = state;
62+
}
5263
}
5364

5465
public function toString()

source/funkin/modding/module/ModuleHandler.hx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import funkin.modding.events.ScriptEvent;
66
import funkin.modding.events.ScriptEventDispatcher;
77
import funkin.modding.module.Module;
88
import funkin.modding.module.ScriptedModule;
9+
import flixel.FlxG;
910

1011
/**
1112
* Utility functions for loading and manipulating active modules.
@@ -145,6 +146,14 @@ class ModuleHandler
145146
// The module needs to be active to receive events.
146147
if (module != null && module.active)
147148
{
149+
if (module.state != null)
150+
{
151+
// Only call the event if the current state is what the module's state is.
152+
if (!(Type.getClass(FlxG.state) == module.state) && !(Type.getClass(FlxG.state?.subState) == module.state))
153+
{
154+
continue;
155+
}
156+
}
148157
ScriptEventDispatcher.callEvent(module, event);
149158
}
150159
}

0 commit comments

Comments
 (0)