From fd7a47b4c01bdff66d9a5bee8ae69bb314f48f69 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 13 Nov 2022 12:44:23 +0800 Subject: [PATCH 1/6] Added changes to hide internal types and constructors that should not be available to SHADE_Scripting --- SHADE_Managed/src/Components/Component.hxx | 3 +-- SHADE_Managed/src/Engine/GenericHandle.hxx | 2 +- SHADE_Managed/src/Scripts/ScriptStore.hxx | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/SHADE_Managed/src/Components/Component.hxx b/SHADE_Managed/src/Components/Component.hxx index e52ab3a7..a1d83eaf 100644 --- a/SHADE_Managed/src/Components/Component.hxx +++ b/SHADE_Managed/src/Components/Component.hxx @@ -110,7 +110,7 @@ namespace SHADE /// Component to check. static operator bool(BaseComponent^ c); - protected: + internal: /*-----------------------------------------------------------------------------*/ /* Constructors */ /*-----------------------------------------------------------------------------*/ @@ -193,7 +193,6 @@ namespace SHADE /// NativeComponent* GetNativeComponent(); - protected: /*-----------------------------------------------------------------------------*/ /* Constructors */ /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Engine/GenericHandle.hxx b/SHADE_Managed/src/Engine/GenericHandle.hxx index 3f8e395f..3d77f54d 100644 --- a/SHADE_Managed/src/Engine/GenericHandle.hxx +++ b/SHADE_Managed/src/Engine/GenericHandle.hxx @@ -21,7 +21,7 @@ namespace SHADE /// /// Managed version of the generic Handle. /// - public value struct GenericHandle + private value struct GenericHandle { public: /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Scripts/ScriptStore.hxx b/SHADE_Managed/src/Scripts/ScriptStore.hxx index 23440f3d..2b2540e6 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.hxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.hxx @@ -25,7 +25,7 @@ namespace SHADE /// Responsible for managing all scripts attached to Entities as well as executing /// all lifecycle functions of scripts. /// - public ref class ScriptStore abstract sealed + private ref class ScriptStore abstract sealed { public: /*-----------------------------------------------------------------------------*/ From a83a38eba81a2c44347149cecf9769c8a263de7c Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 13 Nov 2022 13:07:49 +0800 Subject: [PATCH 2/6] Fixed bin and obj folders respawning and causing engine crashes --- SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index f279bec1..18c2b9e3 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -17,6 +17,7 @@ of DigiPen Institute of Technology is prohibited. #include // std::fstream #include // std::filesystem::canonical, std::filesystem::remove #include // std::shared_ptr +#include // std::this_thread::sleep_for // Project Headers #include "Tools/SHLogger.h" #include "Tools/SHStringUtils.h" @@ -25,7 +26,6 @@ of DigiPen Institute of Technology is prohibited. #include "Events/SHEventReceiver.h" #include "Events/SHEventManager.hpp" #include "Physics/SHPhysicsSystem.h" - #include "Assets/SHAssetMacros.h" namespace SHADE @@ -177,10 +177,10 @@ namespace SHADE } // Prepare directory (delete useless files) - deleteFolder(CSPROJ_DIR + "\\net5.0"); - deleteFolder(CSPROJ_DIR + "\\ref"); - deleteFolder(CSPROJ_DIR + "\\obj"); - deleteFolder(CSPROJ_DIR + "\\bin"); + deleteFolder(CSPROJ_DIR + "/net5.0"); + deleteFolder(CSPROJ_DIR + "/ref"); + deleteFolder(CSPROJ_DIR + "/obj"); + deleteFolder(CSPROJ_DIR + "/bin"); // Attempt to build the assembly std::ostringstream oss; @@ -214,7 +214,10 @@ namespace SHADE // Clean up built files deleteFolder("./tmp"); - deleteFolder(CSPROJ_DIR + "\\obj"); + deleteFolder(CSPROJ_DIR + "/bin"); + using namespace std::chrono_literals; + std::this_thread::sleep_for(50ms); // Not sure why this works but it prevents the folders from respawning + deleteFolder(CSPROJ_DIR + "/obj"); // Read the build log and output to the console dumpBuildLog(BUILD_LOG_PATH); From 635d999c2c47b781686faeb8f232ff2ece8fbaae Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 13 Nov 2022 13:16:36 +0800 Subject: [PATCH 3/6] Fixed redo not working correctly --- SHADE_Managed/src/Editor/UndoRedoStack.cxx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/SHADE_Managed/src/Editor/UndoRedoStack.cxx b/SHADE_Managed/src/Editor/UndoRedoStack.cxx index 789d285d..a83db119 100644 --- a/SHADE_Managed/src/Editor/UndoRedoStack.cxx +++ b/SHADE_Managed/src/Editor/UndoRedoStack.cxx @@ -34,7 +34,8 @@ namespace SHADE bool UndoRedoStack::RedoActionPresent::get() { - return latestActionIndex >= 0 && latestActionIndex < commandStack->Count - 1; + const int REDO_ACTION_INDEX = latestActionIndex + 1; + return REDO_ACTION_INDEX >= 0 && REDO_ACTION_INDEX < commandStack->Count; } /*---------------------------------------------------------------------------------*/ @@ -69,8 +70,9 @@ namespace SHADE { if (!RedoActionPresent) return; - - ICommand^ cmd = commandStack[latestActionIndex]; + + const int REDO_ACTION_INDEX = latestActionIndex + 1; + ICommand^ cmd = commandStack[REDO_ACTION_INDEX]; cmd->Execute(); ++latestActionIndex; } From 23320863e39de9dd661aea38a73926406fc8904b Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 13 Nov 2022 13:38:09 +0800 Subject: [PATCH 4/6] Added support for undo-redo of adding/removing scripts --- SHADE_Managed/src/Editor/Editor.cxx | 34 +++++++++-- SHADE_Managed/src/Editor/Editor.hxx | 22 +++---- SHADE_Managed/src/Editor/UndoRedoStack.cxx | 60 ++++++++++++++++++- SHADE_Managed/src/Editor/UndoRedoStack.hxx | 30 ++++++++++ SHADE_Managed/src/Scripts/ScriptStore.cxx | 40 +++++++++---- SHADE_Managed/src/Scripts/ScriptStore.hxx | 31 ++++++++-- .../Serialisation/SerialisationUtilities.h++ | 11 ---- 7 files changed, 183 insertions(+), 45 deletions(-) diff --git a/SHADE_Managed/src/Editor/Editor.cxx b/SHADE_Managed/src/Editor/Editor.cxx index 68dddf34..7b2e0982 100644 --- a/SHADE_Managed/src/Editor/Editor.cxx +++ b/SHADE_Managed/src/Editor/Editor.cxx @@ -79,7 +79,10 @@ namespace SHADE if (SHEditorUI::Selectable(Convert::ToNative(type->Name))) { // Add the script - ScriptStore::AddScriptViaName(entity, type->Name); + Script^ script; + ScriptStore::AddScriptViaNameWithRef(entity, type->Name, script); + registerUndoScriptAddAction(entity, script); + break; } } @@ -120,7 +123,7 @@ namespace SHADE SHEditorUI::Indent(); { // Right Click Menu - renderScriptContextMenu(entity, script); + renderScriptContextMenu(entity, script, index); // Go through all fields and output them auto fields = ReflectionUtilities::GetInstanceFields(script); @@ -143,7 +146,7 @@ namespace SHADE } else { - renderScriptContextMenu(entity, script); + renderScriptContextMenu(entity, script, index); } SHEditorUI::PopID(); } @@ -336,7 +339,7 @@ namespace SHADE return false; } - void Editor::renderScriptContextMenu(Entity entity, Script^ script) + void Editor::renderScriptContextMenu(Entity entity, Script^ script, int scriptIndex) { // Right Click Menu if (SHEditorUI::BeginPopupContextItem("scriptContextMenu")) @@ -345,6 +348,7 @@ namespace SHADE { // Mark script for removal ScriptStore::RemoveScript(entity, script); + registerUndoScriptRemoveAction(entity, script, scriptIndex); } SHEditorUI::EndPopup(); } @@ -392,6 +396,28 @@ namespace SHADE SHCommandManager::RegisterCommand(std::reinterpret_pointer_cast(std::make_shared())); } + void Editor::registerUndoScriptAddAction(EntityID id, Script^ script) + { + if (script == nullptr) + return; + + actionStack.Add(gcnew ScriptAddCommand(id, script)); + + // Inform the C++ Undo-Redo stack + SHCommandManager::RegisterCommand(std::reinterpret_pointer_cast(std::make_shared())); + } + + void Editor::registerUndoScriptRemoveAction(EntityID id, Script^ script, int originalIndex) + { + if (script == nullptr) + return; + + actionStack.Add(gcnew ScriptRemoveCommand(id, script, originalIndex)); + + // Inform the C++ Undo-Redo stack + SHCommandManager::RegisterCommand(std::reinterpret_pointer_cast(std::make_shared())); + } + generic Attribute Editor::hasAttribute(System::Reflection::FieldInfo^ field) { diff --git a/SHADE_Managed/src/Editor/Editor.hxx b/SHADE_Managed/src/Editor/Editor.hxx index 64c445e5..79625274 100644 --- a/SHADE_Managed/src/Editor/Editor.hxx +++ b/SHADE_Managed/src/Editor/Editor.hxx @@ -69,7 +69,7 @@ namespace SHADE static UndoRedoStack actionStack; /*-----------------------------------------------------------------------------*/ - /* Helper Functions */ + /* Helper Functions - Inspector Rendering */ /*-----------------------------------------------------------------------------*/ /// /// Renders a single specified Script's inspector. @@ -170,25 +170,25 @@ namespace SHADE /// True if the field is modified. template static bool renderFieldEditorInternal(const std::string& fieldName, interior_ptr managedValPtr, EditorFieldFunc fieldEditor, bool* isHovered, RangeAttribute^ rangeAttrib); - /// /// Renders a context menu when right clicked for the scripts /// /// The Entity to render the Scripts of. /// The Script to render the inspector for. - static void renderScriptContextMenu(Entity entity, Script^ script); - /// - /// Adds changes to a variable as an undo-able/redo-able action on the Undo-Redo - /// stack. - /// - /// The object that changes are applied to. - /// The field that was changed. - /// New data to set. - /// Data that was overriden. + /// Index at which the Script is stored. + static void renderScriptContextMenu(Entity entity, Script^ script, int scriptIndex); + /*-----------------------------------------------------------------------------*/ + /* Helper Functions - Undo */ + /*-----------------------------------------------------------------------------*/ static void registerUndoAction(System::Object^ object, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData); static void registerUndoListChangeAction(System::Type^ type, System::Collections::IList^ list, int index, System::Object^ newData, System::Object^ oldData); static void registerUndoListAddAction(System::Type^ type, System::Collections::IList^ list, int index, System::Object^ data); static void registerUndoListRemoveAction(System::Type^ type, System::Collections::IList^ list, int index, System::Object^ data); + static void registerUndoScriptAddAction(EntityID id, Script^ script); + static void registerUndoScriptRemoveAction(EntityID id, Script^ script, int originalIndex); + /*-----------------------------------------------------------------------------*/ + /* Helper Functions - Others */ + /*-----------------------------------------------------------------------------*/ /// /// Checks if a specific field has the specified attribute /// diff --git a/SHADE_Managed/src/Editor/UndoRedoStack.cxx b/SHADE_Managed/src/Editor/UndoRedoStack.cxx index a83db119..3d1f04e9 100644 --- a/SHADE_Managed/src/Editor/UndoRedoStack.cxx +++ b/SHADE_Managed/src/Editor/UndoRedoStack.cxx @@ -21,6 +21,7 @@ of DigiPen Institute of Technology is prohibited. // Project Headers #include "Utility/Debug.hxx" #include "Utility/Convert.hxx" +#include "Scripts/ScriptStore.hxx" namespace SHADE { @@ -182,7 +183,7 @@ namespace SHADE } /*---------------------------------------------------------------------------------*/ - /* ListElementAddCommand - ICommand Functions */ + /* ListElementAddCommand - Constructor */ /*---------------------------------------------------------------------------------*/ ListElementAddCommand::ListElementAddCommand(System::Collections::IList^ list, int addIndex, System::Object^ data) : list { list } @@ -222,7 +223,7 @@ namespace SHADE } /*---------------------------------------------------------------------------------*/ - /* ListElementRemoveCommand - ICommand Functions */ + /* ListElementRemoveCommand - Constructor */ /*---------------------------------------------------------------------------------*/ ListElementRemoveCommand::ListElementRemoveCommand(System::Collections::IList^ list, int removeIndex, System::Object^ data) : list { list } @@ -260,4 +261,59 @@ namespace SHADE // Not allowed return false; } + + /*---------------------------------------------------------------------------------*/ + /* ScriptAddCommand - Constructor */ + /*---------------------------------------------------------------------------------*/ + ScriptAddCommand::ScriptAddCommand(EntityID id, Script^ script) + : entity { id } + , addedScript { script } + {} + + /*---------------------------------------------------------------------------------*/ + /* ScriptAddCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + bool ScriptAddCommand::Execute() + { + return ScriptStore::AddScript(entity, addedScript) != nullptr; + } + + bool ScriptAddCommand::Unexceute() + { + return ScriptStore::RemoveScript(entity, addedScript); + } + + bool ScriptAddCommand::Merge(ICommand^) + { + // Not allowed + return false; + } + + /*---------------------------------------------------------------------------------*/ + /* ScriptRemoveCommand - Constructor */ + /*---------------------------------------------------------------------------------*/ + ScriptRemoveCommand::ScriptRemoveCommand(EntityID id, Script^ script, int index) + : entity { id } + , removedScript { script } + , originalIndex { index } + {} + + /*---------------------------------------------------------------------------------*/ + /* ScriptRemoveCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + bool ScriptRemoveCommand::Execute() + { + return ScriptStore::RemoveScript(entity, removedScript); + } + + bool ScriptRemoveCommand::Unexceute() + { + return ScriptStore::AddScript(entity, removedScript, originalIndex) != nullptr; + } + + bool ScriptRemoveCommand::Merge(ICommand^) + { + // Not allowed + return false; + } } diff --git a/SHADE_Managed/src/Editor/UndoRedoStack.hxx b/SHADE_Managed/src/Editor/UndoRedoStack.hxx index dea458bc..c377e2b7 100644 --- a/SHADE_Managed/src/Editor/UndoRedoStack.hxx +++ b/SHADE_Managed/src/Editor/UndoRedoStack.hxx @@ -12,6 +12,7 @@ Reproduction or disclosure of this file or its contents without the prior writte of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ #pragma once +#include "Scripts/Script.hxx" namespace SHADE { @@ -102,6 +103,35 @@ namespace SHADE System::Object^ data; }; + private ref class ScriptAddCommand sealed : public ICommand + { + public: + ScriptAddCommand(EntityID id, Script^ script); + + bool Execute() override; + bool Unexceute() override; + bool Merge(ICommand^ command) override; + + private: + EntityID entity; + Script^ addedScript; + }; + + private ref class ScriptRemoveCommand sealed : public ICommand + { + public: + ScriptRemoveCommand(EntityID id, Script^ script, int index); + + bool Execute() override; + bool Unexceute() override; + bool Merge(ICommand^ command) override; + + private: + EntityID entity; + Script^ removedScript; + int originalIndex; + }; + /// /// Class that is able to store a stack of actions that can be done and redone. /// diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index d11e70c3..b42f7508 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -38,6 +38,20 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ generic 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)); + + return safe_cast(AddScript(entity, script)); + } + + Script^ ScriptStore::AddScript(Entity entity, Script^ script) + { + return AddScript(entity, script, System::Int32::MaxValue); + } + + Script^ ScriptStore::AddScript(Entity entity, Script^ script, int index) { // Check if entity exists if (!EntityUtils::IsValid(entity)) @@ -57,15 +71,13 @@ namespace SHADE entityScriptList = scripts[entity]; } - // Create the script and add it in - array^ params = gcnew array{GameObject(entity)}; - Script^ script = safe_cast(System::Activator::CreateInstance(T::typeid, params)); - entityScriptList->Add(script); + // Add the script in + entityScriptList->Insert(System::Math::Clamp(index, 0, entityScriptList->Count), script); awakeList.Add(script); startList.Add(script); script->OnAttached(); - return safe_cast(script); + return script; } bool ScriptStore::AddScriptViaName(Entity entity, System::String^ scriptName) @@ -364,7 +376,10 @@ namespace SHADE } } startList.Clear(); - startList.AddRange(%inactiveStartList); + for each (Script ^ script in startList) + { + startList.Add(script); + } inactiveStartList.Clear(); SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") @@ -373,9 +388,8 @@ namespace SHADE { SAFE_NATIVE_CALL_BEGIN // Clear the queue - while (disposalQueue.Count > 0) - { - Script^ script = disposalQueue.Dequeue(); + for each (Script^ script in disposalQueue) + {; if (Application::IsPlaying) { script->OnDestroy(); @@ -388,6 +402,7 @@ namespace SHADE scripts.Remove(entity); } } + disposalQueue.Clear(); SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") } void ScriptStore::Exit() @@ -677,9 +692,9 @@ namespace SHADE void ScriptStore::removeScript(Script^ script) { // Prepare for disposal - disposalQueue.Enqueue(script); + disposalQueue.Add(script); - // Also remove it fromm awake and start queues if they were created but not initialised + // Also remove it from awake and start queues if they were created but not initialised awakeList.Remove(script); startList.Remove(script); script->OnDetached(); @@ -749,7 +764,8 @@ namespace SHADE void ScriptStore::getGenericMethods() { - addScriptMethod = ScriptStore::typeid->GetMethod("AddScript"); + array^ paramTypes = gcnew array{ Entity::typeid }; + addScriptMethod = ScriptStore::typeid->GetMethod("AddScript", paramTypes); if (addScriptMethod == nullptr) { Debug::LogError("[ScriptStore] Failed to get MethodInfo of \"AddScript()\". Adding of scripts from native code will fail."); diff --git a/SHADE_Managed/src/Scripts/ScriptStore.hxx b/SHADE_Managed/src/Scripts/ScriptStore.hxx index 2b2540e6..62e3003a 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.hxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.hxx @@ -46,6 +46,27 @@ namespace SHADE generic where T : ref class, Script static T AddScript(Entity entity); /// + /// Adds a specified pre-constructed Script to a specified Entity. + /// + /// The entity to add a script to. + /// The pre-constructed Script to add. + /// Reference to the script added. + /// + /// If the specified Entity is invalid. + /// + static Script^ AddScript(Entity entity, Script^ script); + /// + /// Adds a specified pre-constructed Script to a specified Entity. + /// + /// The entity to add a script to. + /// The pre-constructed Script to add. + /// Location in the script list to add. + /// Reference to the script added. + /// + /// If the specified Entity is invalid. + /// + static Script^ AddScript(Entity entity, Script^ script, int index); + /// /// Adds a Script to a specified Entity. ///
/// This function is meant for consumption from native code. If you are writing @@ -281,16 +302,16 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ using ScriptList = System::Collections::Generic::List; using ScriptDictionary = System::Collections::Generic::Dictionary; - using ScriptQueue = System::Collections::Generic::Queue; + using ScriptSet = System::Collections::Generic::HashSet; /*-----------------------------------------------------------------------------*/ /* Static Data Members */ /*-----------------------------------------------------------------------------*/ static ScriptDictionary scripts; - static ScriptList awakeList; - static ScriptList startList; - static ScriptList inactiveStartList; - static ScriptQueue disposalQueue; + static ScriptSet awakeList; + static ScriptSet startList; + static ScriptSet inactiveStartList; + static ScriptSet disposalQueue; static System::Collections::Generic::IEnumerable^ scriptTypeList; static System::Reflection::MethodInfo^ addScriptMethod; diff --git a/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ index c1728fe6..3e756ce4 100644 --- a/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ +++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ @@ -28,7 +28,6 @@ namespace SHADE template bool SerialisationUtilities::fieldInsertYaml(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, YAML::Node& fieldNode) { - Debug::Log(FieldType::typeid->Name); return varInsertYamlInternal(fieldInfo->GetValue(object), fieldNode); } template @@ -123,16 +122,6 @@ namespace SHADE return true; } } - else if constexpr (std::is_same_v) - { - if (ReflectionUtilities::FieldIsList(fieldInfo)) - { - System::Collections::IList^ iList = safe_cast(object); - object = gcnew - if (node.IsSequence() ) - - } - } else { if (object->GetType() == FieldType::typeid) From af361062e709ac38fecf6bdcb93233ea8cc3cb92 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 13 Nov 2022 16:07:14 +0800 Subject: [PATCH 5/6] 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 6/6] 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; }