Modified how scripts are added and updated to support runtime adding of scripts #197

Merged
Pycorax merged 3 commits from SP3-6-c-scripting into main 2022-11-13 16:38:32 +08:00
15 changed files with 23 additions and 27 deletions
Showing only changes of commit 4dc8527395 - Show all commits

View File

@ -226,4 +226,9 @@
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0.5, z: 0}
Scripts: ~
Scripts:
- Type: Item
currCategory: 0
- Type: PickAndThrow
throwForce: [100, 200, 100]
item: 51000

View File

@ -51,7 +51,6 @@ public class AIPrototype : Script
private GameObject? player;
public AIPrototype(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{

View File

@ -7,7 +7,6 @@ namespace SHADE_Scripting
{
public float turnSpeed = 0.5f;
public CameraControl(GameObject go) : base(go) { }
protected override void update()
{
//Camera

View File

@ -3,7 +3,6 @@ using System;
public class CameraFix : Script
{
public CameraFix(GameObject gameObj) : base(gameObj) { }
private Transform tranform;
public Vector3 pos = Vector3.Zero;

View File

@ -10,7 +10,6 @@ public class Item : Script
}
public ItemCategory currCategory;
public Item(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{

View File

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

View File

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

View File

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

View File

@ -2,8 +2,6 @@
public class PrintWhenActive : Script
{
public PrintWhenActive(GameObject gameObj) : base(gameObj) { }
protected override void update()
{
Debug.Log("Active!");

View File

@ -23,7 +23,6 @@ public class RaccoonShowcase : Script
[Range(-5, 5)]
public List<int> intList = new List<int>(new int[] { 2, 8, 2, 6, 8, 0, 1 });
public List<Light.Type> enumList = new List<Light.Type>(new Light.Type[] { Light.Type.Point, Light.Type.Directional, Light.Type.Ambient });
public RaccoonShowcase(GameObject gameObj) : base(gameObj) {}
protected override void awake()
{

View File

@ -14,8 +14,6 @@ public class RaccoonSpin : Script
[SerializeField]
private CallbackEvent<int, double, Vector3> testEvent3 = new CallbackEvent<int, double, Vector3>();
private Transform Transform;
public RaccoonSpin(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{

View File

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

View File

@ -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 }
{}
/*---------------------------------------------------------------------------------*/

View File

@ -165,7 +165,7 @@ namespace SHADE
internal:
/*-----------------------------------------------------------------------------*/
/* Properties */
/* Fields */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// If true, the OnGizmosDraw function was overridden.
@ -176,6 +176,10 @@ namespace SHADE
/* "All-Time" Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Used to initialize a Script with a GameObject.
/// </summary>
void Initialize(GameObject newOwner);
/// <summary>
/// Used to call onAttached(). This is called immediately when this script is
/// attached to a GameObject.
/// </summary>
@ -272,13 +276,9 @@ namespace SHADE
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for Script to tie it to a specific GameObject.
/// Constructors of derived Scripts should call this Constructor.
/// Default Constructor
/// </summary>
/// <param name="gameObj">
/// GameObject that this Script will be tied to.
/// </param>
Script(GameObject gameObj);
Script();
/*-----------------------------------------------------------------------------*/
/* Virtual "All-Time" Lifecycle Functions */

View File

@ -40,8 +40,7 @@ namespace SHADE
T ScriptStore::AddScript(Entity entity)
{
// Create the script and add it in
array<System::Object^>^ params = gcnew array<System::Object^>{GameObject(entity)};
Script^ script = safe_cast<Script^>(System::Activator::CreateInstance(T::typeid, params));
Script^ script = safe_cast<Script^>(System::Activator::CreateInstance(T::typeid));
return safe_cast<T>(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;
}