Skip to content

Commit cec1c43

Browse files
authored
Add async runner and functions (#76)
1 parent a6c6038 commit cec1c43

11 files changed

+343
-205
lines changed

Editor/EcsactRuntimeSettingsEditor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,21 @@ public override void OnInspectorGUI() {
230230
EditorUtility.SetDirty(settings);
231231
}
232232

233+
if(settings.runner == EcsactRuntimeSettings.RunnerType.DefaultRunner) {
234+
settings.defaultRegistry.registryName =
235+
EditorGUILayout.TextField("Registry Name", "Default Registry");
236+
settings.defaultRegistry.updateMethod =
237+
(EcsactRuntimeDefaultRegistry.UpdateMethod)EditorGUILayout.EnumPopup(
238+
"Update Method",
239+
settings.defaultRegistry.updateMethod
240+
);
241+
}
242+
243+
if(settings.runner == EcsactRuntimeSettings.RunnerType.AsyncRunner) {
244+
settings.deltaTime =
245+
EditorGUILayout.IntField("Delta Time", settings.deltaTime);
246+
}
247+
233248
if(settings.enableUnitySync) {
234249
EditorGUILayout.LabelField(
235250
label: $"Unity Sync Scripts ({settings.unitySyncScripts.Count})",

Runtime/AsyncRunner.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
using UnityEngine;
2+
using System.Runtime.InteropServices;
23

34
#nullable enable
45

56
namespace Ecsact {
67

78
[AddComponentMenu("")]
8-
public class AsyncRunner : MonoBehaviour {
9-
private static AsyncRunner? instance = null;
10-
11-
private static void OnRuntimeLoad() {
12-
if(instance != null) {
13-
return;
14-
}
15-
16-
var settings = EcsactRuntimeSettings.Get();
17-
if(!settings.useAsyncRunner) {
18-
return;
9+
public class AsyncRunner : EcsactRunner {
10+
private EcsactRuntime? runtime;
11+
12+
private void Enqueue() {
13+
var localExecutionOptions = executionOptions;
14+
15+
try {
16+
executionOptions = new();
17+
LoadEntityCallbacks(localExecutionOptions);
18+
// NOTE: Temporary, this should be abstracted out
19+
// Everything involving create_entities_placeholders should go elsewhere
20+
localExecutionOptions.executionOptions.createEntities =
21+
localExecutionOptions.create_entities_placeholders.ToArray();
22+
localExecutionOptions.Alloc();
23+
Ecsact.Defaults.Runtime.async.EnqueueExecutionOptions(
24+
localExecutionOptions.C()
25+
);
26+
} finally {
27+
executionOptions.Free();
1928
}
20-
21-
var gameObject = new GameObject("Ecsact Async Runner");
22-
instance = gameObject.AddComponent<AsyncRunner>();
23-
DontDestroyOnLoad(gameObject);
2429
}
2530

2631
void Update() {
27-
Ecsact.Defaults.Runtime.async.FlushEvents();
28-
}
29-
30-
void OnDestroy() {
31-
if(this.Equals(instance)) {
32-
instance = null;
32+
if(Ecsact.Defaults.Runtime != null) {
33+
if(!executionOptions.isEmpty()) {
34+
Enqueue();
35+
}
36+
Ecsact.Defaults.Runtime.async.Flush();
3337
}
3438
}
3539
}

Runtime/DynamicEntity.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ private void CreateEntityIfNeeded(global::System.Action callback) {
9595
if(entityId == -1 && callbackPending == false) {
9696
var runtimeSettings = EcsactRuntimeSettings.Get();
9797
callbackPending = true;
98+
UnityEngine.Debug.Log("Trying to create entity");
9899
Ecsact.Defaults.Runner!.executionOptions.CreateEntity((id) => {
99100
Debug.Log("CreateEntityIfNeeded", this);
100101
entityId = id;
@@ -111,8 +112,10 @@ private void CreateEntityIfNeeded(global::System.Action callback) {
111112
pending_callbacks.Clear();
112113
});
113114
} else if(entityId == -1 && callbackPending == true) {
115+
Debug.Log("entityId == - 1");
114116
pending_callbacks.Add(callback);
115117
} else {
118+
Debug.Log("Callback");
116119
callback();
117120
}
118121
}

Runtime/EcsactExecutionOptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public BuilderEntity CreateEntity(EcsactRuntime.EntityIdCallback callback) {
119119
return builder;
120120
}
121121

122+
public BuilderEntity CreateEntity() {
123+
return CreateEntity(entity => {});
124+
}
125+
122126
public void DestroyEntity(Int32 entityId) {
123127
var remove = new EcsactRuntime.EcsactComponentId {
124128
componentId = entityId,

Runtime/EcsactRegistry.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public Int32[] GetEntities() {
5353
return rt.core.GetEntities(ID);
5454
}
5555

56-
public void AddComponent<C>(int entityId, C component)
57-
where C : Ecsact.Component {
56+
internal void AddComponent<C>(int entityId, C component)
57+
where C : Ecsact.Component {
5858
rt.core.AddComponent<C>(ID, entityId, component);
5959
}
6060

61-
public void AddComponent(
61+
internal void AddComponent(
6262
Int32 entityId,
6363
Int32 componentId,
6464
object componentData
@@ -100,17 +100,25 @@ IntPtr callbackUserData
100100
rt.core.EachComponent(ID, entityId, callback, callbackUserData);
101101
}
102102

103-
public void UpdateComponent<C>(Int32 entityId, C component)
104-
where C : Ecsact.Component {
103+
internal void UpdateComponent(
104+
Int32 entityId,
105+
Int32 componentId,
106+
object componentData
107+
) {
108+
rt.core.UpdateComponent(ID, entityId, componentId, componentData);
109+
}
110+
111+
internal void UpdateComponent<C>(Int32 entityId, C component)
112+
where C : Ecsact.Component {
105113
rt.core.UpdateComponent<C>(ID, entityId, component);
106114
}
107115

108-
public void RemoveComponent<C>(Int32 entityId)
109-
where C : Ecsact.Component {
116+
internal void RemoveComponent<C>(Int32 entityId)
117+
where C : Ecsact.Component {
110118
rt.core.RemoveComponent<C>(ID, entityId);
111119
}
112120

113-
public void RemoveComponent(Int32 entityId, Int32 componentId) {
121+
internal void RemoveComponent(Int32 entityId, Int32 componentId) {
114122
rt.core.RemoveComponent(ID, entityId, componentId);
115123
}
116124

Runtime/EcsactRunner.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,24 @@ public class EcsactRunner : MonoBehaviour {
2323
private Ecsact.Details.ExecutionEntityCallbacks entityCallbacks = new();
2424

2525
void Start() {
26-
Ecsact.Defaults.Runtime.OnEntityCreated((entityId, placeholderId) => {
27-
EcsactRuntime.EntityIdCallback callback;
28-
29-
var hasCallback =
30-
entityCallbacks.GetAndClearCallback(placeholderId, out callback);
31-
if(hasCallback) {
32-
callback(entityId);
33-
}
26+
Ecsact.Defaults.WhenReady(() => {
27+
Ecsact.Defaults.Runtime.OnEntityCreated((entityId, placeholderId) => {
28+
EcsactRuntime.EntityIdCallback callback;
29+
30+
var hasCallback =
31+
entityCallbacks.GetAndClearCallback(placeholderId, out callback);
32+
if(hasCallback) {
33+
callback(entityId);
34+
}
35+
});
3436
});
3537
}
3638

3739
public Ecsact.ExecutionOptions executionOptions = new();
3840

3941
internal static EcsactRunner CreateInstance<ComponentT>(
40-
EcsactRuntimeDefaultRegistry.RunnerType runnerType,
41-
EcsactRuntimeSettings settings,
42-
string name
42+
EcsactRuntimeSettings settings,
43+
string name
4344
)
4445
where ComponentT : EcsactRunner {
4546
var gameObjectName = name;
@@ -68,7 +69,6 @@ protected void Execute() {
6869
try {
6970
executionOptions = new();
7071
LoadEntityCallbacks(localExecutionOptions);
71-
// NOTE: Temporary, this should be abstracted out
7272
localExecutionOptions.executionOptions.createEntities =
7373
localExecutionOptions.create_entities_placeholders.ToArray();
7474
Ecsact.Defaults.Registry.ExecuteSystems(localExecutionOptions);
@@ -89,7 +89,6 @@ protected void LoadEntityCallbacks(ExecutionOptions localExecutionOptions) {
8989
for(int i = 0; i < localExecutionOptions.create_entities.Count; i++) {
9090
var builder = localExecutionOptions.create_entities[i];
9191
var id = entityCallbacks.AddCallback(builder.callback);
92-
// NOTE: Temporary, this should be abstracted out
9392
localExecutionOptions.create_entities_placeholders.Add(id);
9493
}
9594
}

0 commit comments

Comments
 (0)