Small script engine improvements

- Fixed script class name not visible in Component Editor
- Retain user assembly file system watcher (fixed deletion while handling events)
- Fixed crash when a scritp constains a List or Array (by ignoring them for now)
- Fixed Typo in Genericinst
- Scene: Invoke OnCreate for copied entity scripts
This commit is contained in:
Simon Lübeß
2023-10-28 21:48:43 +02:00
parent 909012f518
commit 38fc1f63b3
5 changed files with 53 additions and 28 deletions
@@ -541,7 +541,7 @@ namespace GlitchyEditor.EditWindows
StringView search = StringView(); StringView search = StringView();
char8* scriptLabel = scriptComponent.Instance?.ScriptClass.FullName.ToScopeCStr!() ?? "Select Script..."; char8* scriptLabel = scriptComponent.ScriptClassName.ToScopeCStr!() ?? "Select Script...";
if (ImGui.Button(scriptLabel)) if (ImGui.Button(scriptLabel))
ImGui.OpenPopup("SelectScript"); ImGui.OpenPopup("SelectScript");
@@ -791,7 +791,6 @@ namespace GlitchyEditor.EditWindows
var value = field.GetData<bool>(); var value = field.GetData<bool>();
if (ImGui.Checkbox(fieldName.CStr(), &value)) if (ImGui.Checkbox(fieldName.CStr(), &value))
field.SetData(value); field.SetData(value);
case .SByte: case .SByte:
var value = field.GetData<int8>(); var value = field.GetData<int8>();
if (ImGui.DragScalar(fieldName.CStr(), .S8, &value)) if (ImGui.DragScalar(fieldName.CStr(), .S8, &value))
+34 -22
View File
@@ -27,7 +27,6 @@ static sealed class ScriptEngineHelper
("System.UInt64", .ULong), ("System.UInt64", .ULong),
("System.Single", .Float), ("System.Single", .Float),
// TODO: We probably want to switch the C#-Library to use the superior floatN-Names
("GlitchyEngine.Math.float2", .float2), ("GlitchyEngine.Math.float2", .float2),
("GlitchyEngine.Math.float3", .float3), ("GlitchyEngine.Math.float3", .float3),
("GlitchyEngine.Math.float4", .float4), ("GlitchyEngine.Math.float4", .float4),
@@ -153,38 +152,40 @@ static class ScriptEngine
String directory = scope .(); String directory = scope .();
Path.GetDirectoryPath(_appAssemblyPath, directory); Path.GetDirectoryPath(_appAssemblyPath, directory);
if (_userAssemblyWatcher != null) // _userAssemblyWatcher.Directory != directory /*if (_userAssemblyWatcher != null) // _userAssemblyWatcher.Directory != directory
{ {
delete _userAssemblyWatcher; delete _userAssemblyWatcher;
} }*/
String fileName = scope .("*/"); String fileName = scope .("*/");
Path.GetFileName(_appAssemblyPath, fileName); Path.GetFileName(_appAssemblyPath, fileName);
// TODO: Obviously don't hardcode path if (_userAssemblyWatcher == null)
_userAssemblyWatcher = new FileSystemWatcher(directory, fileName);
_userAssemblyWatcher.OnChanged.Add(new (fileName) =>
{ {
// TODO: Temporary, we want to be able to reload while in play-mode. (+ Editor Scripts will be a thing some day) _userAssemblyWatcher = new FileSystemWatcher(directory, fileName);
if (_entityScriptInstances.Count > 0) _userAssemblyWatcher.OnChanged.Add(new (fileName) =>
{ {
Log.EngineLogger.Warning("There are script instances. Skipping assembly reload."); // TODO: Temporary, we want to be able to reload while in play-mode. (+ Editor Scripts will be a thing some day)
return; if (_entityScriptInstances.Count > 0)
} {
Log.EngineLogger.Warning("There are script instances. Skipping assembly reload.");
return;
}
Log.EngineLogger.Info("Script reload requested."); Log.EngineLogger.Info("Script reload requested.");
_userAssemblyWatcher.StopRaisingEvents(); _userAssemblyWatcher.StopRaisingEvents();
Application.Instance.InvokeOnMainThread(new () => Application.Instance.InvokeOnMainThread(new () =>
{ {
Log.EngineLogger.Info("Reloading scripts..."); Log.EngineLogger.Info("Reloading scripts...");
ReloadAssemblies(); ReloadAssemblies();
Log.EngineLogger.Info("Scripts reloaded!"); Log.EngineLogger.Info("Scripts reloaded!");
_userAssemblyWatcher.StartRaisingEvents();
});
_userAssemblyWatcher.StartRaisingEvents();
}); });
}
});
_userAssemblyWatcher.StartRaisingEvents(); _userAssemblyWatcher.StartRaisingEvents();
} }
@@ -628,7 +629,7 @@ static class ScriptEngine
Mono.MonoTypeEnum fieldType = Mono.Mono.mono_type_get_type(monoType); Mono.MonoTypeEnum fieldType = Mono.Mono.mono_type_get_type(monoType);
MonoClass* monoClass = Mono.mono_type_get_class(monoType); MonoClass* monoClass = Mono.mono_class_from_mono_type(monoType);
if (monoClass == null) if (monoClass == null)
return null; return null;
@@ -667,6 +668,17 @@ static class ScriptEngine
{ {
scriptType = .Struct; scriptType = .Struct;
} }
// TODO! ?!
else if (fieldType == .Genericinst)
{
scriptType = .GenericClass;
return null;
}
else if (fieldType == .SzArray)
{
scriptType = .Array;
return null;
}
Log.EngineLogger.AssertDebug(scriptType != .None); Log.EngineLogger.AssertDebug(scriptType != .None);
@@ -12,6 +12,9 @@ enum ScriptFieldType
case Enum; case Enum;
case Struct; case Struct;
case GenericClass;
case Array;
case Bool; case Bool;
case SByte; case SByte;
+11
View File
@@ -841,6 +841,9 @@ namespace GlitchyEngine.World
// TODO: Copy Data from one instance to another // TODO: Copy Data from one instance to another
//ScriptEngine.CopyFieldsToInstance(targetScript, sourceScript); //ScriptEngine.CopyFieldsToInstance(targetScript, sourceScript);
// TODO: Only if we are in Runtime
targetScript.Instance.InvokeOnCreate();
} }
// This is kinda slow because it's in O(n*m) where n is the tree depth and m is the total number of entities in the scene... // This is kinda slow because it's in O(n*m) where n is the tree depth and m is the total number of entities in the scene...
@@ -920,6 +923,14 @@ namespace GlitchyEngine.World
} }
} }
private void OnComponentRemoved(Entity entity, Type componentType, void* component)
{
if (_onComponentAddedHandlers.TryGetValue(componentType, let handler))
{
handler(entity, componentType, component);
}
}
public WorldEnumerator<TComponent> GetEntities<TComponent>() where TComponent : struct public WorldEnumerator<TComponent> GetEntities<TComponent>() where TComponent : struct
{ {
return _ecsWorld.Enumerate<TComponent>(); return _ecsWorld.Enumerate<TComponent>();
+1 -1
View File
@@ -523,7 +523,7 @@ enum MonoTypeEnum : int32
Class = 0x12, /* arg: <type> token */ Class = 0x12, /* arg: <type> token */
Var = 0x13, /* number */ Var = 0x13, /* number */
Array = 0x14, /* type, rank, boundsCount, bound1, loCount, lo1 */ Array = 0x14, /* type, rank, boundsCount, bound1, loCount, lo1 */
Genericins = 0x15, /* <type> <type-arg-count> <type-1> \x{2026} <type-n> */ Genericinst = 0x15, /* <type> <type-arg-count> <type-1> \x{2026} <type-n> */
Typedbyref = 0x16, Typedbyref = 0x16,
I = 0x18, I = 0x18,
U = 0x19, U = 0x19,