diff --git a/docs/tasks/core.todo b/docs/tasks/core.todo index 19331e1..d900b8e 100644 --- a/docs/tasks/core.todo +++ b/docs/tasks/core.todo @@ -42,7 +42,10 @@ Physics: ☐ Joint Component to allow parent/child entities to both use the physics system Scripting: - ☐ Allow scripts to interact (calling methods in other scripts) + + ☐ Add a Log method to the CoreAPI + + ✔ Allow scripts to interact (calling methods in other scripts) @done(23-01-10 16:54) - In order for a script to be registered with the World API it MUST - contain a class that inherits from EntityBehavior and that class MUST - have the same name as the script (case sensitive). diff --git a/src/scripting/internal_scripts/world_interface.wren b/src/scripting/internal_scripts/world_interface.wren index c5d9d0d..ac32e09 100644 --- a/src/scripting/internal_scripts/world_interface.wren +++ b/src/scripting/internal_scripts/world_interface.wren @@ -7,6 +7,7 @@ ******************************************************************************/ import "Components" for VelocityComponent, BlockOutComponent +import "CoreAPI" for CoreAPI // Manages all of the EntityBehaviors class WorldInterface { @@ -18,15 +19,12 @@ class WorldInterface { static RegisterScript(entity_id, script_name, script_class) { - System.print(entity_id.type) - System.print(script_name.type) - System.print(script_class.type) - if (!__ScriptObjects.containsKey(entity_id)){ __ScriptObjects[entity_id] = {} } __ScriptObjects[entity_id][script_name] = script_class.new(entity_id) + //System.print("Registered script: %(script_name) to entity: %(entity_id)") } ///////////////////////////////////////////////////////////////////// @@ -76,10 +74,13 @@ class WorldInterface { } static GetScriptObject(entity_id, script_name) { - if (__ScriptObjects.containsKey(entity_id) && __ScriptObjects[entity_id].containsKey(script_name)) { - return __ScriptObjects[entity_id][script_name] + if (__ScriptObjects.containsKey(entity_id)) { + if (__ScriptObjects[entity_id].containsKey(script_name)) { + return __ScriptObjects[entity_id][script_name] + } } + System.print("Could not find script: %(script_name) for entity: %(entity_id)") return null } } diff --git a/src/scripting/world_api.cpp b/src/scripting/world_api.cpp index 7e81c81..3906819 100644 --- a/src/scripting/world_api.cpp +++ b/src/scripting/world_api.cpp @@ -93,8 +93,9 @@ namespace lunarium // mScriptState.CallWrenMethod(mEventHandles.mWIRegisterScriptMethod, mEventHandles.mWIHandle, params, "WorldInterface.entity_id, script_name, script_class"); std::string script_class = script.GetScriptName(); + std::string module_name = "Register_" + script_class; std::string code = "WorldInterface.RegisterScript(\"" + std::to_string(entity) + "\", \"" + script_class + "\", " + script_class + ")"; - mScriptState.RunSnippet("RegisterScript", {{"WorldInterface", "WorldInterface"}, { script_class, script_class }}, code); + mScriptState.RunSnippet(module_name, {{"WorldInterface", "WorldInterface"}, { script_class, script_class }}, code); } void WorldAPI::InvokeEvent(Event e, ...)