Passing a type in a function.

Blitz3D Forums/Blitz3D Beginners Area/Passing a type in a function.

Farflame(Posted 2005) [#1]
I want to call a function which saves all of the information in my type to a file. Since there are 50 instances of this type, I want to call it 50 times. I don't need a return value. How do I just say Callmyfunction() and pass the type to the function? If I just leave it empty, the function has no idea what the type/instance is. I assume I could make my type global, but is there a way to just tell the function?

Sorry if I'm using the wrong words here, types STILL confuse me :)


BlitzSupport(Posted 2005) [#2]
Not too sure if this is what you mean, but this passes Rocket objects to a function that then lists the fields of the object passed -- could easily be redirected to a file instead...


; The type...

Type Rocket
	Field x
	Field y
	Field fuel
End Type



; Create 10 rockets...

For a = 1 To 10
	r.Rocket = New Rocket
	r\x = Rand (0, 639)
	r\y = Rand (0, 479)
	r\fuel = Rand (0, 100)
Next

; Call function to print out field info...

For r.Rocket = Each Rocket
	counter = counter + 1
	Print "Info for rocket " + counter + ":"
	Print ""
	PrintRocketFields (r)
	Print ""
Next




; The function...

Function PrintRocketFields (passed.Rocket)
	Print passed\x
	Print passed\y
	Print passed\fuel
End Function



BlitzSupport(Posted 2005) [#3]
An alternative is to call Str on the object instead, which produces formatted output (I think this is undocumented still)...

Type Rocket
	Field x
	Field y
	Field fuel
	Field name$
End Type

r.Rocket = New Rocket
r\x = Rand (0, 639)
r\y = Rand (0, 479)
r\fuel = Rand (0, 100)
r\name = "Player 1"

Print Str (r)



Farflame(Posted 2005) [#4]
Yep, that's what I meant, and it works fine now. Thanks very much :)


Farflame(Posted 2005) [#5]
Didn't know that either, but it's handy. Whilst we're on the subject of formatted output, is there any way to get comma's in big numbers, so that 123456789 comes out as 123,456,789? Other than writing my own function that is?