@@ -15,6 +15,7 @@ pc.extend(pc, function () {
1515 this . _scriptsIndex = { } ;
1616 this . _scriptsData = null ;
1717 this . _oldState = true ;
18+ this . _beingEnabled = false ;
1819 this . on ( 'set_enabled' , this . _onSetEnabled , this ) ;
1920 } ;
2021 ScriptComponent = pc . inherits ( ScriptComponent , pc . Component ) ;
@@ -160,7 +161,14 @@ pc.extend(pc, function () {
160161 pc . extend ( ScriptComponent . prototype , {
161162 onEnable : function ( ) {
162163 ScriptComponent . _super . onEnable . call ( this ) ;
164+ this . _beingEnabled = true ;
163165 this . _checkState ( ) ;
166+
167+ if ( ! this . entity . _beingEnabled ) {
168+ this . onPostStateChange ( ) ;
169+ }
170+
171+ this . _beingEnabled = false ;
164172 } ,
165173
166174 onDisable : function ( ) {
@@ -170,10 +178,10 @@ pc.extend(pc, function () {
170178
171179 onPostStateChange : function ( ) {
172180 var script ;
173- for ( var i = 0 ; i < this . scripts . length ; i ++ ) {
181+ for ( var i = 0 , len = this . scripts . length ; i < len ; i ++ ) {
174182 script = this . scripts [ i ] ;
175183
176- if ( script . _initialized && ! script . _postInitialized ) {
184+ if ( script . _initialized && ! script . _postInitialized && script . enabled ) {
177185 script . _postInitialized = true ;
178186
179187 if ( script . postInitialize )
@@ -182,8 +190,13 @@ pc.extend(pc, function () {
182190 }
183191 } ,
184192
193+ // We also need this handler because it is fired
194+ // when value === old instead of onEnable and onDisable
195+ // which are only fired when value !== old
185196 _onSetEnabled : function ( prop , old , value ) {
197+ this . _beingEnabled = true ;
186198 this . _checkState ( ) ;
199+ this . _beingEnabled = false ;
187200 } ,
188201
189202 _checkState : function ( ) {
@@ -193,22 +206,13 @@ pc.extend(pc, function () {
193206
194207 this . _oldState = state ;
195208
196- this . fire ( this . enabled ? 'enable' : 'disable' ) ;
197- this . fire ( 'state' , this . enabled ) ;
209+ this . fire ( state ? 'enable' : 'disable' ) ;
210+ this . fire ( 'state' , state ) ;
198211
199212 var script ;
200213 for ( var i = 0 , len = this . scripts . length ; i < len ; i ++ ) {
201214 script = this . scripts [ i ] ;
202215 script . enabled = script . _enabled ;
203-
204- if ( ! script . _initialized && script . enabled ) {
205- script . _initialized = true ;
206-
207- script . __initializeAttributes ( true ) ;
208-
209- if ( script . initialize )
210- this . _scriptMethod ( script , ScriptComponent . scriptMethods . initialize ) ;
211- }
212216 }
213217 } ,
214218
@@ -257,16 +261,7 @@ pc.extend(pc, function () {
257261 } ,
258262
259263 _onPostInitialize : function ( ) {
260- var script , scripts = this . _scripts ;
261-
262- for ( var i = 0 , len = scripts . length ; i < len ; i ++ ) {
263- script = scripts [ i ] ;
264- if ( ! script . _postInitialized && script . enabled ) {
265- script . _postInitialized = true ;
266- if ( script . postInitialize )
267- this . _scriptMethod ( script , ScriptComponent . scriptMethods . postInitialize ) ;
268- }
269- }
264+ this . onPostStateChange ( ) ;
270265 } ,
271266
272267 _onUpdate : function ( dt ) {
@@ -379,17 +374,23 @@ pc.extend(pc, function () {
379374
380375 this . system . app . scripts . on ( 'swap:' + scriptType . __name , this . _scriptsIndex [ scriptType . __name ] . onSwap ) ;
381376
382- if ( ! args . preloading && this . enabled && scriptInstance . enabled && ! scriptInstance . _initialized ) {
383- scriptInstance . _initialized = true ;
384- scriptInstance . _postInitialized = true ;
377+ if ( ! args . preloading ) {
378+
379+ if ( scriptInstance . enabled && ! scriptInstance . _initialized ) {
380+ scriptInstance . _initialized = true ;
385381
386- if ( scriptInstance . initialize )
387- this . _scriptMethod ( scriptInstance , ScriptComponent . scriptMethods . initialize ) ;
382+ if ( scriptInstance . initialize )
383+ this . _scriptMethod ( scriptInstance , ScriptComponent . scriptMethods . initialize ) ;
384+ }
388385
389- if ( scriptInstance . postInitialize )
390- this . _scriptMethod ( scriptInstance , ScriptComponent . scriptMethods . postInitialize ) ;
386+ if ( scriptInstance . enabled && ! scriptInstance . _postInitialized ) {
387+ scriptInstance . _postInitialized = true ;
388+ if ( scriptInstance . postInitialize )
389+ this . _scriptMethod ( scriptInstance , ScriptComponent . scriptMethods . postInitialize ) ;
390+ }
391391 }
392392
393+
393394 return scriptInstance ;
394395 } else {
395396 console . warn ( 'script \'' + scriptName + '\' is already added to entity \'' + this . entity . name + '\'' ) ;
0 commit comments