@@ -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
0 commit comments