Scripting: FindEntityWithName and get script instance

+ Some C# UUID stuff
+ Added some Mono functions
+ ScriptEngine:
  + ID -> ScriptInstance map in ScriptEngine
  + GetManagedInstance

+ Fixed Scene not visible after loading a new scene
  + Renamed OnViewportResize to SetViewportSize for Scene and SceneRenderer
This commit is contained in:
Simon Lübeß
2023-07-17 20:27:18 +02:00
parent 66d66d3308
commit e8505f6bdb
16 changed files with 492 additions and 61 deletions
@@ -90,6 +90,11 @@ class SharpClass : SharpType
ExtractFields();
}
internal bool IsType(MonoType* type)
{
return Mono.mono_class_get_type(_monoClass) == type;
}
private void ExtractFields()
{
//_monoFields = new List<MonoClassField*>();´
+12 -6
View File
@@ -151,13 +151,13 @@ static class ScriptEngine
private static Dictionary<StringView, ScriptClass> _entityScripts = new .() ~ DeleteDictionaryAndReleaseValues!(_);
/*private static Dictionary<UUID, ScriptInstance> _entityScriptInstances = new .() ~ {
private static Dictionary<UUID, ScriptInstance> _entityScriptInstances = new .() ~ {
for (var entry in _)
{
entry.value?.ReleaseRef();
}
delete _;
};*/
}
public static Dictionary<StringView, ScriptClass> EntityClasses => _entityScripts;
@@ -197,18 +197,18 @@ static class ScriptEngine
public static void OnRuntimeStop()
{
/*for (var entry in _entityScriptInstances)
for (var entry in _entityScriptInstances)
{
entry.value.ReleaseRef();
}
_entityScriptInstances.Clear();*/
_entityScriptInstances.Clear();
SetContext(null);
}
public static void InitializeInstance(Entity entity, ScriptComponent* script)
{
//_entityScriptInstances[entity.UUID] = script.Instance..AddRef();
_entityScriptInstances[entity.UUID] = script.Instance..AddRef();
script.Instance.Instantiate(entity.UUID);
@@ -403,7 +403,13 @@ static class ScriptEngine
return _entityFields[uuid];
}
public static MonoObject* GetManagedInstance(UUID entityId)
{
if (_entityScriptInstances.TryGetValue(entityId, let scriptInstance))
return scriptInstance.MonoInstance;
return null;
}
+24 -4
View File
@@ -11,6 +11,8 @@ using Box2D;
namespace GlitchyEngine.Scripting;
using internal GlitchyEngine.Scripting;
static class ScriptGlue
{
private static Dictionary<MonoType*, function void(Entity entityId)> s_AddComponentMethods = new .() ~ delete _;
@@ -176,11 +178,29 @@ static class ScriptGlue
else
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
}
/*[RegisterCall("Input::IsMouseButtonReleasing")]
static void Destroy()
[RegisterCall("ScriptGlue::Entity_FindEntityWithName")]
static void Entity_FindEntityWithName(MonoString* monoName, UUID* outUuid)
{
// TODO:
}*/
*outUuid = UUID(0);
char8* entityName = Mono.mono_string_to_utf8(monoName);
StringView nameString = StringView(entityName);
Result<Entity> entityResult = ScriptEngine.Context.GetEntityByName(nameString);
Mono.mono_free(entityName);
if (entityResult case .Ok(let entity))
*outUuid = entity.UUID;
}
[RegisterCall("ScriptGlue::Entity_GetScriptInstance")]
static MonoObject* Entity_GetScriptInstance(UUID entityId)
{
return ScriptEngine.GetManagedInstance(entityId);
}
#endregion
@@ -37,6 +37,8 @@ class ScriptInstance : RefCounter
_scriptClass?.ReleaseRef();
}
internal MonoObject* MonoInstance => _instance;
public void Instantiate(UUID uuid)
{
_instance = _scriptClass.CreateInstance(uuid);
+19 -4
View File
@@ -92,7 +92,8 @@ namespace GlitchyEngine.World
Entity targetEntity = target.GetEntityByID(sourceEntity.UUID);
ScriptComponent* targetComponent = targetEntity.AddComponent<ScriptComponent>();
targetComponent.ScriptClass = sourceComponent.ScriptClass;
targetComponent.Instance = new ScriptInstance(sourceComponent.ScriptClass);
targetComponent.Instance..ReleaseRef();
@@ -116,7 +117,7 @@ namespace GlitchyEngine.World
}
}
target.OnViewportResize(_viewportWidth, _viewportHeight);
//target.SetViewportSize(_viewportWidth, _viewportHeight);
}
private static void CopyComponents<TComponent>(Scene source, Scene target) where TComponent : struct, new
@@ -376,9 +377,23 @@ namespace GlitchyEngine.World
return .Err;
}
/// Sets the size of the viewport into which the scene will be rendered.
public void OnViewportResize(uint32 width, uint32 height)
public Result<Entity> GetEntityByName(StringView name)
{
for (let (ecsEntity, nameComponent) in GetEntities<NameComponent>())
{
if (nameComponent.Name == name)
return .Ok(Entity(ecsEntity, this));
}
return .Err;
}
/// Sets the size of the viewport into which the scene will be rendered.
public void SetViewportSize(uint32 width, uint32 height)
{
if (_viewportWidth == width && _viewportHeight == height)
return;
_viewportWidth = width;
_viewportHeight = height;
+4 -1
View File
@@ -45,8 +45,11 @@ class SceneRenderer
}
/// Sets the size of the viewport into which the scene will be rendered.
public void OnViewportResize(uint32 width, uint32 height)
public void SetViewportSize(uint32 width, uint32 height)
{
if (_viewportWidth == width && _viewportHeight == height)
return;
_viewportWidth = width;
_viewportHeight = height;