Start of edit mode scripts

- Initialize script instances in edit mode
- Run OnCreate and OnUpdate for Entities with RunInEditMode-Attribute
This commit is contained in:
Simon Lübeß
2023-12-02 00:28:04 +01:00
parent 23b38a7a31
commit 5852e11ccb
5 changed files with 48 additions and 5 deletions
@@ -283,6 +283,8 @@ class ScriptClass : SharpClass
typealias OnUpdateMethod = function MonoObject*(MonoObject* instance, float deltaTime, MonoException** exception);
typealias OnDestroyMethod = function MonoObject*(MonoObject* instance, MonoException** exception);
private bool _runInEditMode;
private MonoMethod* _constructor;
private OnCreateMethod _onCreate;
@@ -300,6 +302,8 @@ class ScriptClass : SharpClass
public bool HasCollisionEnter2D => _onCollisionEnter2DMethod != null;
public bool HasCollisionLeave2D => _onCollisionLeave2DMethod != null;
public bool RunInEditMode => _runInEditMode;
[AllowAppend]
public this(StringView classNamespace, StringView className, MonoImage* image, ScriptFieldType scriptFieldType = .Class) :
base(classNamespace, className, image, scriptFieldType)
@@ -314,6 +318,19 @@ class ScriptClass : SharpClass
_onCollisionEnter2DMethod = GetMethod("OnCollisionEnter2D", 1);
_onCollisionLeave2DMethod = GetMethod("OnCollisionLeave2D", 1);
DetermineRunInEditMode();
}
/// Determines whether or not scripts of this class will be executed in edit mode.
private void DetermineRunInEditMode()
{
// TODO: This is only relevant for the editor!
MonoCustomAttrInfo* attributes = Mono.mono_custom_attrs_from_class(_monoClass);
if (attributes != null)
_runInEditMode = Mono.mono_custom_attrs_has_attr(attributes, ScriptEngine.Attributes.s_RunInEditModeAttribute);
}
private MonoMethod* FindConstructor()
@@ -5,6 +5,7 @@ using System.Collections;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
using GlitchyEngine.World;
using System.Diagnostics;
namespace GlitchyEngine.Scripting;
@@ -104,6 +105,7 @@ static class ScriptEngine
internal static class Attributes
{
internal static MonoClass* s_ShowInEditorAttribute;
internal static MonoClass* s_RunInEditModeAttribute;
}
public static void Init()
@@ -553,7 +555,12 @@ static class ScriptEngine
/// Retrieves the classes for Attributes that are defined in the Core library
static void GetCoreAttributes()
{
// We definitely need these attributes!
// TODO: Some only for the editor!
Attributes.s_ShowInEditorAttribute = Mono.mono_class_from_name(s_CoreAssemblyImage, "GlitchyEngine.Editor", "ShowInEditorAttribute");
Debug.Assert(Attributes.s_ShowInEditorAttribute != null);
Attributes.s_RunInEditModeAttribute = Mono.mono_class_from_name(s_CoreAssemblyImage, "GlitchyEngine.Editor", "RunInEditModeAttribute");
Debug.Assert(Attributes.s_RunInEditModeAttribute != null);
}
private static void GetEntitiesFromAssemblies()
+8 -5
View File
@@ -600,12 +600,12 @@ namespace GlitchyEngine.World
{
/// No special update configuration (this does NOT mean nothing will be updated!)
None = 0x00,
/// Update editor-specific stuff
Editor = 0x01,
/// Update the physics related stuff
Physics = 0x02,
Physics = 0x01,
/// Execute scripts.
Scripts = 0x04,
Scripts = 0x02,
/// Update editor-specific stuff and also scripts (most only ever initialize, but some update!)
Editor = 0x04 | Scripts,
/// Update the runtume related stuff (e.g. execute scripts). Also run physics!
Runtime = Scripts | Physics,
}
@@ -630,7 +630,10 @@ namespace GlitchyEngine.World
if (!script.IsInitialized)
ScriptEngine.InitializeInstance(Entity(entity, this), script);
if (script.Instance == null)
// Skip OnCreate and OnUpdate invocation if we didn't create an instance
// (happens, if script component has no script class associated)
// Also skip if we are in edit mode and the class doesn't have the RunInEditMode-Attribute
if (script.Instance == null || (mode.HasFlag(.Editor) && !script.Instance.ScriptClass.RunInEditMode))
continue;
if (mode.HasFlag(.Runtime))