Passing alot of variables to a function...

BlitzMax Forums/BlitzMax Beginners Area/Passing alot of variables to a function...

Dubious Drewski(Posted 2005) [#1]
One of my functions is getting out of control.

In my program, I have a fancy particle system with behaviours
and many attributes and I will only be expanding these behaviours as I go on.

Problem is, I'm getting lost in the long list of variables I'm passing into the function that initializes these particles:

'array, x,y,x1,y1,randomness, scale, kind, many, alpha
Function createParticle(p:particle[], x,y,x1,y1,r:Float, scale:Float, kind, many, alpha:Float)


All of these variables are needed. And unlike some other IDEs, In Blitz, I can't separate the variables by putting them each on their own line like so:

createParticle(
p,
s[a].x,
s[a].y,
-s[a].x1*b,
-s[a].y1*b,
.05,
.05,
1,
2,
.4
)
Being able to do it like the above example would be GREAT. Anyone have any work-arounds though?

By the way, the particle engine is used for explosion debris, bubbles under water, smoke, fire, etc.


Scott Shaver(Posted 2005) [#2]
why not pass in a config type instead of each individual parameter.

Type ParticleConfig
field x,y,z
...
end type


tonyg(Posted 2005) [#3]
Can you have seperate functions where CreateParticle creates a basic template with default values and then adjust it with calls to another function?
e.g. my_particle=CreateParticle()
SetParticleSpeed(my_particle,0.75)
SetParticleLife(my_particle,1000)
etc.


Yan(Posted 2005) [#4]
createParticle(..
p,..
s[a].x,..
s[a].y,..
-s[a].x1*b,..
-s[a].y1*b,..
.05,..
.05,..
1,..
2,..
.4..
)



Dubious Drewski(Posted 2005) [#5]
Scott, I'm not sure what you're saying, but I'd like to hear you explain it some more.

And tonyg, I considered that, but I decided I wanted to avoid doing that.

And TwoEyedPete, you're brilliant! Thank you! Works great.


kyoryu(Posted 2005) [#6]
I think what Scott's suggesting is the creation of a new Type, that would have all of the data necessary to create a new particle. You could make sure it had 'reasonable' defaults, set the things you want to override, and then pass the one object to your function.

This might even be faster if you're making a bunch of particles, as you could then tweak the object slightly and pass the same object to the createParticle() function multiple times.


Scott Shaver(Posted 2005) [#7]
exactly


WendellM(Posted 2005) [#8]
This definitely looks like a job for a Type.


Scott Shaver(Posted 2005) [#9]
Can someone enter that line continuation thing (..) into the wiki? I couldn't find it in there.


Dubious Drewski(Posted 2005) [#10]
Ah, I see what Scott was saying now, thanks, kyoryu.

And Wendel, I am using types, what made you think I wasn't?

If you'd like to know, I'm using an array instead of a list to handle
my particles for now. I see no obvious speed or convenience
benefits to using a list (yet), but I'm also a newbie.


kyoryu(Posted 2005) [#11]
Two advantages:

1) If you're passing in multiple types of parameters (strings, floats, etc.), a Type can be made typesafe.

2) By using a type, you can use named parameters, which will make it more clear what is going on. Consider (and sorry if my syntax is slightly screwed up)

Type ParticleParams
  Field xLoc:Int
  Field yLoc:Int
  Field Speed:Float
  Field RGB:Long
End Type
...

Local params:ParticleParams = new ParticleParams
params.xLoc = 100
params.yLoc = 200
params.Speed = 2.0
params.RGB = 48723 ' okay, this could be cleaner.

CreateParticle params


In this case, without even looking at the definition of CreateParticle, you know exactly what the code is doing. In comparison:

Local params[ 4 ]
params[ 0 ] = 100
params[ 1 ] = 200
params[ 2 ] = 2.5
params[ 3 ] = 34212

CreateParticle params


Less clear, and far more likely for you to misorder something (which simply can't happen with a Typed parameter)


Jay Kyburz(Posted 2005) [#12]
I recently made "Create" Function for many of my types but I find them quite ugly to use.

Kyoryu, Can you explain why you would use a params type rather that just set the properties on the newly instantiated object?

p:tParticle = New tParticle
p.Pos.x = 100
p.Pos.Y = 100
p.Speed = 50
p.Color.Set (255,255,255,1)



Jams(Posted 2005) [#13]
My particle system does it slightly differently and i think it's the most efficient way to do it, when a particle is created it inherits it's properties from it's parent emmitter.

example:

Type Emitter
  Field x:float, y:float, z:float
  Field ParticleMass:Int, ParticleMassVariation:Byte
  Field ParticleAngle:Float, ParticleAngleVariation:Byte

  Method IssueProperties( Target:Particle )
    Target.Mass = ParticleMass + ParticleMassVariation
    Target.Angle = ParticleAngle + ParticleAngleVariation
    'etc etc
  End Method
End Type

Type Particle
  Field Parent:Emmitter
  Field Mass:Int
  Field Angle:Float

  Function Create:Particle( NewParent:Emmitter )
    Local NewP:Particle = new Particle
    NewP.Parent = NewParent
    NewP.Parent.IssueProperties( NewP )
  End Function
End Type



kyoryu(Posted 2005) [#14]
Jay,

In that case, I'm not sure it really makes sense. The only reason I can think of doing it is if you had to create a lot of similar particles/objects. In which case, slightly modifying a parameter object and passing it to the create function may be slightly easier/more efficient.