Particles (and Functions)

Blitz3D Forums/Blitz3D Beginners Area/Particles (and Functions)

adsta(Posted 2003) [#1]
Hi, a while back I made a particle system that basically worked of two functions, one to create and set the properties of the particles and another to update the particls. Well, as you can imagine setting about 20 properties in a single function can get confusing, so I decided to redesign it using functions to set individual settings (ie position, life, magnitude etc).
I started coding it, and I have ran into a problem that I can't figure out.
The problem is, in the SetLife function, when I define the life (say 150), i want that value to be set to all newly created particles, and in the UpdateParticles function, i want the value of p\life to subtract a value of 1 each loop.
But the code I've made, doesn't work. The value of p\life is always 149 (if i set it to 150). So I'm guessing that, I am calling the function in the wrong place or something because the value of p\life keeps getting reset to 150.
Can someone please look at my code and try and help me.
Cheers


Graphics 800,600,32,2
SetBuffer BackBuffer()

Type particle
	Field id$
	Field intensity#
	Field life%
End Type

SetIntensity(1)

While Not KeyDown(1)

	For i = 0 To intensity

		SetLife(p.particle, 150)
		spark.particle = CreateParticle()

		Delay(250) 

	Next


Flip
Wend

;Functions

Function CreateParticle.particle()

	p.particle = New particle
	p\id = id
	p\life = life
	
	Return p

End Function

Function SetIntensity(intensity#)

	For p.particle = Each particle

		p\intensity = intensity
		
	Next

End Function

Function SetLife(p.particle, life%)

	UpdateParticle(p.particle, life)
		
End Function

Function LifeDuration(amount%)

	amount% = life%

End Function

Function UpdateParticle(p.particle, life%)

	For p.particle = Each particle
	
	Cls
	
	p\life = life
	
	If p\life = 0
		
		Delete p
	
	Else
		
		p\life = p\life - 1
	
		Text 10,10, p\life
	
	End If
	
	Next
	
End Function




poopla(Posted 2003) [#2]
You need an emitter that will store all base values for your particles.

IE:

[Code]
Type Emitter
Field life
end type
[/code]

Then the particles you release from that emitter will inherit the emitters properties.

function ReleaseParticle(e.emitter)
     p.particle = new particle
     p\life = e\life
end funciton


no every particle emitted from the emitter will have that value for it's life.

The way your doing it won't be very efficient, so look into doing it this way :). Hope this helps.


adsta(Posted 2003) [#3]
Thanks, it's working now