From af361062e709ac38fecf6bdcb93233ea8cc3cb92 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 13 Nov 2022 16:07:14 +0800 Subject: [PATCH 1/2] Modified how scripts are added and updated to support runtime adding of scripts --- SHADE_Managed/src/Scripts/ScriptStore.cxx | 38 ++++++++++++++++------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index b42f7508..6b33b31b 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -73,8 +73,16 @@ namespace SHADE // Add the script in entityScriptList->Insert(System::Math::Clamp(index, 0, entityScriptList->Count), script); - awakeList.Add(script); - startList.Add(script); + if (Application::IsPlaying) + { + script->Awake(); + script->Start(); + } + else + { + awakeList.Add(script); + startList.Add(script); + } script->OnAttached(); return script; @@ -451,9 +459,10 @@ namespace SHADE continue; // Update each script - for each (Script^ script in entity.Value) + ScriptList^ scripts = entity.Value; + for (int i = 0; i < scripts->Count; ++i) { - script->FixedUpdate(); + scripts[i]->FixedUpdate(); } } SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") @@ -468,9 +477,10 @@ namespace SHADE continue; // Update each script - for each (Script^ script in entity.Value) + ScriptList^ scripts = entity.Value; + for (int i = 0; i < scripts->Count; ++i) { - script->Update(); + scripts[i]->Update(); } } SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") @@ -485,9 +495,10 @@ namespace SHADE continue; // Update each script - for each (Script^ script in entity.Value) + ScriptList^ scripts = entity.Value; + for (int i = 0; i < scripts->Count; ++i) { - script->LateUpdate(); + scripts[i]->LateUpdate(); } } SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") @@ -503,9 +514,10 @@ namespace SHADE continue; // Update each script - for each (Script^ script in entity.Value) + ScriptList^ scripts = entity.Value; + for (int i = 0; i < scripts->Count; ++i) { - script->OnDrawGizmos(); + scripts[i]->OnDrawGizmos(); } } SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") @@ -537,8 +549,9 @@ namespace SHADE auto entityScripts = scripts[entity.first]; if (entityScripts->Count > 0) { - for each (Script ^ script in entityScripts) + for (int i = 0; i < entityScripts->Count; ++i) { + Script^ script = entityScripts[i]; switch (collisionInfo.GetCollisionState()) { case SHCollisionEvent::State::ENTER: @@ -578,8 +591,9 @@ namespace SHADE auto entityScripts = scripts[entity.first]; if (entityScripts->Count > 0) { - for each (Script ^ script in entityScripts) + for (int i = 0; i < entityScripts->Count; ++i) { + Script^ script = entityScripts[i]; switch (triggerInfo.GetCollisionState()) { case SHCollisionEvent::State::ENTER: From 4dc85273956282c542a6116b39060baecf634ded Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 13 Nov 2022 16:29:25 +0800 Subject: [PATCH 2/2] Reworked scripts to no longer need definition of constructors --- Assets/Scenes/M2Scene.shade | 7 ++++++- Assets/Scripts/AIPrototype.cs | 1 - Assets/Scripts/CameraControl.cs | 1 - Assets/Scripts/CameraFix.cs | 1 - Assets/Scripts/Item.cs | 1 - Assets/Scripts/PhysicsTest.cs | 1 - Assets/Scripts/PickAndThrow.cs | 1 - Assets/Scripts/PlayerController.cs | 2 -- Assets/Scripts/PrintWhenActive.cs | 2 -- Assets/Scripts/RaccoonShowcase.cs | 1 - Assets/Scripts/RaccoonSpin.cs | 2 -- Assets/Scripts/ThirdPersonCamera.cs | 1 - SHADE_Managed/src/Scripts/Script.cxx | 10 +++++++--- SHADE_Managed/src/Scripts/Script.hxx | 14 +++++++------- SHADE_Managed/src/Scripts/ScriptStore.cxx | 5 +++-- 15 files changed, 23 insertions(+), 27 deletions(-) diff --git a/Assets/Scenes/M2Scene.shade b/Assets/Scenes/M2Scene.shade index b2a5683f..75b69d27 100644 --- a/Assets/Scenes/M2Scene.shade +++ b/Assets/Scenes/M2Scene.shade @@ -226,4 +226,9 @@ Bounciness: 0 Density: 1 Position Offset: {x: 0, y: 0.5, z: 0} - Scripts: ~ \ No newline at end of file + Scripts: + - Type: Item + currCategory: 0 + - Type: PickAndThrow + throwForce: [100, 200, 100] + item: 51000 \ No newline at end of file diff --git a/Assets/Scripts/AIPrototype.cs b/Assets/Scripts/AIPrototype.cs index d678de78..62255778 100644 --- a/Assets/Scripts/AIPrototype.cs +++ b/Assets/Scripts/AIPrototype.cs @@ -51,7 +51,6 @@ public class AIPrototype : Script private GameObject? player; - public AIPrototype(GameObject gameObj) : base(gameObj) { } protected override void awake() { diff --git a/Assets/Scripts/CameraControl.cs b/Assets/Scripts/CameraControl.cs index fc900f46..b25d65eb 100644 --- a/Assets/Scripts/CameraControl.cs +++ b/Assets/Scripts/CameraControl.cs @@ -7,7 +7,6 @@ namespace SHADE_Scripting { public float turnSpeed = 0.5f; - public CameraControl(GameObject go) : base(go) { } protected override void update() { //Camera diff --git a/Assets/Scripts/CameraFix.cs b/Assets/Scripts/CameraFix.cs index 5347a72f..0ca085ad 100644 --- a/Assets/Scripts/CameraFix.cs +++ b/Assets/Scripts/CameraFix.cs @@ -3,7 +3,6 @@ using System; public class CameraFix : Script { - public CameraFix(GameObject gameObj) : base(gameObj) { } private Transform tranform; public Vector3 pos = Vector3.Zero; diff --git a/Assets/Scripts/Item.cs b/Assets/Scripts/Item.cs index 96ec092d..5047a241 100644 --- a/Assets/Scripts/Item.cs +++ b/Assets/Scripts/Item.cs @@ -10,7 +10,6 @@ public class Item : Script } public ItemCategory currCategory; - public Item(GameObject gameObj) : base(gameObj) { } protected override void awake() { diff --git a/Assets/Scripts/PhysicsTest.cs b/Assets/Scripts/PhysicsTest.cs index cc01615d..9726a51c 100644 --- a/Assets/Scripts/PhysicsTest.cs +++ b/Assets/Scripts/PhysicsTest.cs @@ -8,7 +8,6 @@ public class PhysicsTest : Script private Transform Transform; private RigidBody RigidBody; private Collider Collider; - public PhysicsTest(GameObject gameObj) : base(gameObj) { } protected override void awake() { diff --git a/Assets/Scripts/PickAndThrow.cs b/Assets/Scripts/PickAndThrow.cs index ea814b36..ec8846c5 100644 --- a/Assets/Scripts/PickAndThrow.cs +++ b/Assets/Scripts/PickAndThrow.cs @@ -14,7 +14,6 @@ public class PickAndThrow : Script private float lastXDir; private float lastZDir; private bool inRange = false; - public PickAndThrow(GameObject gameObj) : base(gameObj) { } protected override void awake() { diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 86ba7c98..4a02d470 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -73,8 +73,6 @@ public class PlayerController : Script public float mediumMultiper = 0.5f; public float heavyMultiper = 0.25f; - public PlayerController(GameObject gameObj) : base(gameObj) { } - protected override void awake() { //default setup diff --git a/Assets/Scripts/PrintWhenActive.cs b/Assets/Scripts/PrintWhenActive.cs index 41afdd58..11d7f025 100644 --- a/Assets/Scripts/PrintWhenActive.cs +++ b/Assets/Scripts/PrintWhenActive.cs @@ -2,8 +2,6 @@ public class PrintWhenActive : Script { - public PrintWhenActive(GameObject gameObj) : base(gameObj) { } - protected override void update() { Debug.Log("Active!"); diff --git a/Assets/Scripts/RaccoonShowcase.cs b/Assets/Scripts/RaccoonShowcase.cs index dc9d914d..3c767f7f 100644 --- a/Assets/Scripts/RaccoonShowcase.cs +++ b/Assets/Scripts/RaccoonShowcase.cs @@ -23,7 +23,6 @@ public class RaccoonShowcase : Script [Range(-5, 5)] public List intList = new List(new int[] { 2, 8, 2, 6, 8, 0, 1 }); public List enumList = new List(new Light.Type[] { Light.Type.Point, Light.Type.Directional, Light.Type.Ambient }); - public RaccoonShowcase(GameObject gameObj) : base(gameObj) {} protected override void awake() { diff --git a/Assets/Scripts/RaccoonSpin.cs b/Assets/Scripts/RaccoonSpin.cs index efdfadeb..84100a21 100644 --- a/Assets/Scripts/RaccoonSpin.cs +++ b/Assets/Scripts/RaccoonSpin.cs @@ -14,8 +14,6 @@ public class RaccoonSpin : Script [SerializeField] private CallbackEvent testEvent3 = new CallbackEvent(); private Transform Transform; - public RaccoonSpin(GameObject gameObj) : base(gameObj) { } - protected override void awake() { diff --git a/Assets/Scripts/ThirdPersonCamera.cs b/Assets/Scripts/ThirdPersonCamera.cs index 141865e8..fed26ae9 100644 --- a/Assets/Scripts/ThirdPersonCamera.cs +++ b/Assets/Scripts/ThirdPersonCamera.cs @@ -15,7 +15,6 @@ namespace SHADE_Scripting public float turnSpeedPitch = 0.3f; public float turnSpeedYaw = 0.5f; public float pitchClamp = 45.0f; - public ThirdPersonCamera(GameObject go) : base(go) { } protected override void awake() { diff --git a/SHADE_Managed/src/Scripts/Script.cxx b/SHADE_Managed/src/Scripts/Script.cxx index 9d6cadb8..017242d6 100644 --- a/SHADE_Managed/src/Scripts/Script.cxx +++ b/SHADE_Managed/src/Scripts/Script.cxx @@ -93,6 +93,11 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* "All-time" Lifecycle Functions */ /*---------------------------------------------------------------------------------*/ + void Script::Initialize(GameObject newOwner) + { + owner = newOwner; + } + void Script::OnAttached() { SAFE_NATIVE_CALL_BEGIN @@ -198,9 +203,8 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Constructors */ /*---------------------------------------------------------------------------------*/ - Script::Script(GameObject gameObj) - : owner { gameObj } - , OnGizmosDrawOverriden { false } + Script::Script() + : OnGizmosDrawOverriden { false } {} /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Scripts/Script.hxx b/SHADE_Managed/src/Scripts/Script.hxx index bbe36784..fb564d27 100644 --- a/SHADE_Managed/src/Scripts/Script.hxx +++ b/SHADE_Managed/src/Scripts/Script.hxx @@ -165,7 +165,7 @@ namespace SHADE internal: /*-----------------------------------------------------------------------------*/ - /* Properties */ + /* Fields */ /*-----------------------------------------------------------------------------*/ /// /// If true, the OnGizmosDraw function was overridden. @@ -176,6 +176,10 @@ namespace SHADE /* "All-Time" Lifecycle Functions */ /*-----------------------------------------------------------------------------*/ /// + /// Used to initialize a Script with a GameObject. + /// + void Initialize(GameObject newOwner); + /// /// Used to call onAttached(). This is called immediately when this script is /// attached to a GameObject. /// @@ -272,13 +276,9 @@ namespace SHADE /* Constructors */ /*-----------------------------------------------------------------------------*/ /// - /// Constructor for Script to tie it to a specific GameObject. - /// Constructors of derived Scripts should call this Constructor. + /// Default Constructor /// - /// - /// GameObject that this Script will be tied to. - /// - Script(GameObject gameObj); + Script(); /*-----------------------------------------------------------------------------*/ /* Virtual "All-Time" Lifecycle Functions */ diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index 6b33b31b..a6f978a1 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -40,8 +40,7 @@ namespace SHADE T ScriptStore::AddScript(Entity entity) { // Create the script and add it in - array^ params = gcnew array{GameObject(entity)}; - Script^ script = safe_cast(System::Activator::CreateInstance(T::typeid, params)); + Script^ script = safe_cast(System::Activator::CreateInstance(T::typeid)); return safe_cast(AddScript(entity, script)); } @@ -72,6 +71,7 @@ namespace SHADE } // Add the script in + script->Initialize(GameObject(entity)); entityScriptList->Insert(System::Math::Clamp(index, 0, entityScriptList->Count), script); if (Application::IsPlaying) { @@ -130,6 +130,7 @@ namespace SHADE std::ostringstream oss; oss << "[ScriptStore] Failed to add Script named \"" << Convert::ToNative(scriptName) << "\" to Entity #" << entity << "! (" << Convert::ToNative(e->GetType()->Name) << ")"; + oss << Convert::ToNative(e->ToString()); Debug::LogError(oss.str()); return false; }