Rebooting particle system with a jump label.

Blitz3D Forums/Blitz3D Programming/Rebooting particle system with a jump label.

Yue(Posted 2010) [#1]
Hello, please restart aid to a system of particles. by a jump label. I can not find the right way.

HidePointer
AppTitle ("Emisor de partículas")

Graphics3D 640,480,16,0
SetBuffer BackBuffer()
.inicio
particula=LoadSprite("particula.png",1)
particula2=LoadSprite("particula2.png",1)


Type emisor 
    ; Emisor
    Field X_Emisor#,Y_Emisor#,Z_Emisor#    ; Posicion del emisor 
    Field RangoX#,RangoY#,RangoZ#          ; Tamaño del emisor    
    ; Particula   
    Field Entidad                          ; Entidad Sprite que forma la particula 
    Field Vida                             ; Vida de la particula una vez emitida
    Field Alfa#                            ; Transparencia de la particula
    Field Angulo_Rotacion#                 ; Angulo de rotacion de cada particula
    Field Xvel#,Yvel#,Zvel#                ; Velocidad inicial de las particulas 
End Type  

For x = 1 To 200
	b.emisor = New emisor 
    If Rand(1,2)=1                         ; Aplicamos dos tipos de sprites
     	b\Entidad =  CopyEntity(particula) 
     Else
     	b\Entidad =  CopyEntity(particula2) 
    EndIf
	b\Alfa# = Rnd(.1,.8) 
	EntityAlpha(b\Entidad,b\Alfa#) 
	EntityBlend b\Entidad,3 
Next  

Camara=CreateCamera()

Luz=CreateLight()
RotateEntity luz,0,0,0
LightColor luz,255,20,30

Emisor=CreateCone()
PositionEntity Emisor,0,-7,15

Plano=CreatePlane()
PositionEntity Plano,0,-8,15
EntityColor Plano,100,30,40


RangoX_Emisor#=0 
RangoY_Emisor#=4  ; Altura que alcanza cada partícula
RangoZ_Emisor#=0
VeloX_Particula#=.05
VeloY_Particula#=.2
VeloZ_Particula#=.05
Vida_Particula=100
Tamano_Particula#=1
Peso_Particula#=.0025
Dir_Viento#=0
Vel_Viento#=0





While Not KeyDown(1)

	
	If KeyDown(17) ;Reboot Particles Error!!!
		ClearWorld 
		Goto inicio
		
	End If 
	
	If KeyDown(205) MoveEntity Emisor,.1,0,0    
   If KeyDown(203) MoveEntity Emisor,-.1,0,0    

   If KeyDown(200) MoveEntity Emisor,0,.1,0    
   If KeyDown(208) MoveEntity Emisor,0,-.1,0    

;   If KeyDown(57) ; Fuente original
;      RangoX_Emisor#=0:RangoY_Emisor#=4:RangoZ_Emisor#=0
;      VeloX_Particula#=.05:VeloY_Particula#=.2:VeloZ_Particula#=.05
;      Vida_Particula=100:Tamano_Particula#=1:Peso_Particula#=.0025:Dir_Viento#=0:Vel_Viento#=0
;   EndIf
;   If KeyDown(2) ; Fuente 2
;      RangoX_Emisor#=0:RangoY_Emisor#=4:RangoZ_Emisor#=0
;      VeloX_Particula#=0:VeloY_Particula#=.5:VeloZ_Particula#=0
;      Vida_Particula=100:Tamano_Particula#=1:Peso_Particula#=.0025:Dir_Viento#=0:Vel_Viento#=0
;   EndIf
;   If KeyDown(3); Fuente 3
;      RangoX_Emisor#=1:RangoY_Emisor#=0:RangoZ_Emisor#=1
;      VeloX_Particula#=.3:VeloY_Particula#=.2:VeloZ_Particula#=.3
;      Vida_Particula=100:Tamano_Particula#=1:Peso_Particula#=.005:Dir_Viento#=0:Vel_Viento#=0
;   EndIf
;
;   If KeyDown(4) ; Fuente 4 particulas
;      RangoX_Emisor#=0:RangoY_Emisor#=0:RangoZ_Emisor#=0
;      VeloX_Particula#=.2:VeloY_Particula#=.01:VeloZ_Particula#=.2
;      Vida_Particula=20:Tamano_Particula#=2:Peso_Particula#=-.005:Dir_Viento#=0:Vel_Viento#=0
;   EndIf
;
;   If KeyDown(5) ; Fuego con viento
;      RangoX_Emisor#=0:RangoY_Emisor#=5:RangoZ_Emisor#=0
;      VeloX_Particula#=0:VeloY_Particula#=.1:VeloZ_Particula#=0
;      Vida_Particula=50:Tamano_Particula#=2:Peso_Particula#=.0001:Dir_Viento#=.005:Vel_Viento#=0
;   EndIf
;   If KeyDown(6) ; Explosion particulas
;      RangoX_Emisor#=0:RangoY_Emisor#=0:RangoZ_Emisor#=0
;      VeloX_Particula#=.3:VeloY_Particula#=.1:VeloZ_Particula#=.3
;      Vida_Particula=300:Tamano_Particula#=2:Peso_Particula#=.002:Dir_Viento#=0:Vel_Viento#=0
;   EndIf

		PosX_Emisor#=EntityX(Emisor): PosY_Emisor#=EntityY(Emisor): PosZ_Emisor#=EntityZ(Emisor)
   emisorparticulas (PosX_Emisor#,PosY_Emisor#,PosZ_Emisor#,RangoX_Emisor#,RangoY_Emisor#,RangoZ_Emisor#,VeloX_Particula#,VeloY_Particula#,VeloZ_Particula#,Vida_Particula,Tamano_Particula#,Peso_Particula#,Dir_Viento#,Vel_Viento#)

   RenderWorld
   Text 0,0,"Cursores para mover el emisor"
   Text 0,15,"Espacio  Emisión Estándar"
   Text 0,30,"1 - 5 Diferentes emisiones"
   Flip
   
Wend

End


Function emisorparticulas (PosX_Emisor#,PosY_Emisor#,PosZ_Emisor#,RangoX_Emisor#,RangoY_Emisor#,RangoZ_Emisor#,VeloX_Particula#,VeloY_Particula#,VeloZ_Particula#,Vida_Particula,Tamano_Particula#,Peso_Particula#,Dir_Viento#,Vel_Viento#)

  For  b.emisor = Each emisor

   If b\Vida<0
      b\X_Emisor#=Rnd(PosX_Emisor,PosX_Emisor+RangoX_Emisor) 
      b\Y_Emisor#=Rnd(PosY_Emisor,PosY_Emisor+RangoY_Emisor) 
      b\Z_Emisor#=Rnd(PosZ_Emisor,PosZ_Emisor+RangoZ_Emisor) 
	b\Xvel#=Rnd(-VeloX_Particula#,VeloX_Particula#) 
      b\Yvel#=VeloY_Particula# 
      b\Zvel#=Rnd(-VeloX_Particula#,VeloZ_Particula#) 
      Escala#=Rnd(.1,Tamano_Particula)
      ScaleSprite  (b\Entidad,Escala#,Escala#)
	b\Vida=Rnd(Vida_Particula-40,Vida_Particula+40) 
	
EndIf

   b\X_Emisor#=b\X_Emisor#+b\Xvel# 
   b\Xvel#=b\Xvel#-Dir_Viento#        ; Resistencia al viento 
   b\Y_Emisor#=b\Y_Emisor#+b\YVel# 
   b\Yvel#=b\Yvel#-Peso_Particula#     ; Gravedad
   b\Z_Emisor#=b\Z_Emisor#+b\Zvel# 
   b\Zvel#=b\Zvel#-Vel_Viento#        ; Aumenta la velocidad por el viento
   b\Vida = b\Vida - 1 
   PositionEntity b\Entidad,b\X_Emisor#,b\Y_Emisor#,b\Z_Emisor# 
   b\Angulo_Rotacion#=b\ Angulo_Rotacion #+4
   If b\ Angulo_Rotacion #>360 b\ Angulo_Rotacion #=1 
   RotateSprite b\Entidad,b\ Angulo_Rotacion # 
    
 Next 

End Function 




Yasha(Posted 2010) [#2]
When you jump to inicio, you call ClearWorld, which deletes all 3D objects, but you don't do anything about the existing instances of "emisor". Later, when you call "emisorparticulas", the original 200 "emisor" objects still exist (as well as 200 new ones), but no longer have their sprites attached to them as those were destroyed when you called ClearWorld; when the function tries to update the nonexistent entities, it crashes.

Add this:
Delete Each emisor

...after ClearWorld, and it no longer crashes.

The thing to remember is that ClearWorld and EndGraphics only affect the graphics engine and have no effect on logical objects like instances of types ("emisor", in this case), banks, strings or anything else like that. You need to remember to clear these manually.

It's generally good practice not to rely on functions like ClearWorld when designing robust applications, and instead add deletion functions for each thing you create, as you go along. That way you can guarantee everything is destroyed as and when you need it to be, and no "dangling pointers" remain, like they do in the above example.


Kryzon(Posted 2010) [#3]
Sweet post, Yasha.


Yue(Posted 2010) [#4]
Thank you Yasha learns something new.