Added example of talking to another script

dev
Joey Pollack 3 years ago
parent 1e38af39aa
commit 320b776fcd

@ -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).

@ -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)) {
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
}
}

@ -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, ...)

Loading…
Cancel
Save