Skip to content

Commit 2d0a4a9

Browse files
authored
Use create entity execution options in dynamic entity (#74)
1 parent f871681 commit 2d0a4a9

File tree

3 files changed

+64
-32
lines changed

3 files changed

+64
-32
lines changed

Runtime/DynamicEntity.cs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public class DynamicEntity : MonoBehaviour {
5757
public global::System.Int32 entityId { get; private set; } = -1;
5858
public List<SerializableEcsactComponent> ecsactComponents = new();
5959

60+
public List<global::System.Action> pending_callbacks = new();
61+
62+
bool callbackPending = false;
63+
6064
public void AddEcsactCompnent<C>(C component)
6165
where C : Ecsact.Component {
6266
if(Application.isPlaying) {
@@ -87,12 +91,29 @@ object componentData
8791
});
8892
}
8993

90-
private void CreateEntityIfNeeded() {
91-
if(entityId == -1) {
92-
entityId = Ecsact.Defaults.Registry.CreateEntity();
93-
if(Ecsact.Defaults.Pool != null) {
94-
Ecsact.Defaults.Pool.SetPreferredEntityGameObject(entityId, gameObject);
95-
}
94+
private void CreateEntityIfNeeded(global::System.Action callback) {
95+
if(entityId == -1 && callbackPending == false) {
96+
var runtimeSettings = EcsactRuntimeSettings.Get();
97+
callbackPending = true;
98+
Ecsact.Defaults.Runner!.executionOptions.CreateEntity((id) => {
99+
Debug.Log("CreateEntityIfNeeded", this);
100+
entityId = id;
101+
if(Ecsact.Defaults.Pool != null) {
102+
Ecsact.Defaults.Pool.SetPreferredEntityGameObject(
103+
entityId,
104+
gameObject
105+
);
106+
}
107+
callback();
108+
foreach(var callback in pending_callbacks) {
109+
callback();
110+
}
111+
pending_callbacks.Clear();
112+
});
113+
} else if(entityId == -1 && callbackPending == true) {
114+
pending_callbacks.Add(callback);
115+
} else {
116+
callback();
96117
}
97118
}
98119

@@ -101,12 +122,14 @@ private void AddInitialEcsactComponents() {
101122
for(int i = 0; ecsactComp.otherEntities.Count > i; ++i) {
102123
var otherEntity = ecsactComp.otherEntities[i];
103124
if(otherEntity != null && otherEntity.entityId == -1) {
104-
otherEntity.CreateEntityIfNeeded();
105-
var fieldName = ecsactComp.entityFieldNames[i];
106-
var compType = ecsactComp.data!.GetType();
107-
var field = compType.GetField(fieldName);
108-
Debug.Assert(field != null, this);
109-
field!.SetValue(ecsactComp.data, otherEntity.entityId);
125+
otherEntity.CreateEntityIfNeeded(() => {
126+
var fieldName = ecsactComp.entityFieldNames[i];
127+
var compType = ecsactComp.data!.GetType();
128+
var field = compType.GetField(fieldName);
129+
Debug.Log("Add components");
130+
Debug.Assert(field != null, this);
131+
field!.SetValue(ecsactComp.data, otherEntity.entityId);
132+
});
110133
}
111134
}
112135
}
@@ -119,8 +142,7 @@ private void AddInitialEcsactComponents() {
119142

120143
void OnEnable() {
121144
Ecsact.Defaults.WhenReady(() => {
122-
CreateEntityIfNeeded();
123-
AddInitialEcsactComponents();
145+
CreateEntityIfNeeded(() => { AddInitialEcsactComponents(); });
124146
});
125147
}
126148

Runtime/EcsactRunner.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ public class EcsactRunner : MonoBehaviour {
2424

2525
void Start() {
2626
Ecsact.Defaults.Runtime.OnEntityCreated((entityId, placeholderId) => {
27-
var callback = entityCallbacks.GetAndClearCallback(placeholderId);
28-
callback(entityId);
27+
EcsactRuntime.EntityIdCallback callback;
28+
29+
var hasCallback =
30+
entityCallbacks.GetAndClearCallback(placeholderId, out callback);
31+
if(hasCallback) {
32+
callback(entityId);
33+
}
2934
});
3035
}
3136

@@ -59,33 +64,33 @@ protected void Execute() {
5964
executionOptions.Alloc();
6065
}
6166

67+
var localExecutionOptions = executionOptions;
6268
try {
63-
LoadEntityCallbacks();
69+
executionOptions = new();
70+
LoadEntityCallbacks(localExecutionOptions);
6471
// NOTE: Temporary, this should be abstracted out
65-
executionOptions.executionOptions.createEntities =
66-
executionOptions.create_entities_placeholders.ToArray();
67-
Ecsact.Defaults.Registry.ExecuteSystems(executionOptions);
72+
localExecutionOptions.executionOptions.createEntities =
73+
localExecutionOptions.create_entities_placeholders.ToArray();
74+
Ecsact.Defaults.Registry.ExecuteSystems(localExecutionOptions);
6875
} finally {
69-
executionOptions.Free();
76+
localExecutionOptions.Free();
7077
#if UNITY_EDITOR
7178
executionTimeWatch.Stop();
72-
executionOptions.create_entities_placeholders = new();
79+
localExecutionOptions.create_entities_placeholders = new();
7380
debugExecutionTimeMs = (int)executionTimeWatch.ElapsedMilliseconds;
7481
debugExecutionCountTotal += 1;
7582
#endif
76-
77-
executionOptions.executionOptions = new EcsactRuntime.CExecutionOptions();
7883
}
7984

8085
Ecsact.Defaults.Runtime.wasm.PrintAndConsumeLogs();
8186
}
8287

83-
protected void LoadEntityCallbacks() {
84-
for(int i = 0; i < executionOptions.create_entities.Count; i++) {
85-
var builder = executionOptions.create_entities[i];
88+
protected void LoadEntityCallbacks(ExecutionOptions localExecutionOptions) {
89+
for(int i = 0; i < localExecutionOptions.create_entities.Count; i++) {
90+
var builder = localExecutionOptions.create_entities[i];
8691
var id = entityCallbacks.AddCallback(builder.callback);
8792
// NOTE: Temporary, this should be abstracted out
88-
executionOptions.create_entities_placeholders.Add(id);
93+
localExecutionOptions.create_entities_placeholders.Add(id);
8994
}
9095
}
9196
}

Runtime/ExecutionEntityCallbacks.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ public Int32 AddCallback(EcsactRuntime.EntityIdCallback callback) {
1010
return entity_id_counter++;
1111
}
1212

13-
public EcsactRuntime.EntityIdCallback GetAndClearCallback(Int32 placeholderId
13+
public bool GetAndClearCallback(
14+
Int32 placeholderId,
15+
out EcsactRuntime.EntityIdCallback callback
1416
) {
15-
EcsactRuntime.EntityIdCallback callback;
1617
var hasCallback = callbacks.TryGetValue(placeholderId, out callback);
1718

18-
callbacks.Remove(placeholderId);
19-
return callback;
19+
if(hasCallback) {
20+
callbacks.Remove(placeholderId);
21+
return true;
22+
}
23+
24+
return false;
2025
}
2126

2227
private Int32 entity_id_counter = 0;

0 commit comments

Comments
 (0)