Type Field initializers

Blitz3D Forums/Blitz3D Programming/Type Field initializers

WolRon(Posted 2005) [#1]
I did a quick search but didn't come up with anything.

Is it possible to initialize Type Fields?

This bit of code compiles:
Type TypeWeapon
	Field WeaponID% = 1
	Field Ammo% = 500
	Field FireRate% = 2
	Field Velocity% = 100
	Field Range% = 500
	Field Damage% = 1
	Field Force% = 1
	Field Weight% = 50
End Type


but it doesn't seem to actually work...


Jeremy Alessi(Posted 2005) [#2]
No, but you could do something like ...

Function CreateTypeWeapon()

   tw.TypeWeapon = New TypeWeapon
   tw\WeaponID% = 1
   tw\Ammo% = 500
   tw\FireRate% = 2
   tw\Velocity% = 100
   tw\Range% = 500
   tw\Damage% = 1
   tw\Force% = 1
   tw\Weight% = 50

End Function


Then you could just call the function to create a new instance with all the variables initialized. Of course you probably do that already and don't care but I thought I'd try something more than just ... no.


Michael Reitzenstein(Posted 2005) [#3]
This is possible in BlitzMax.


DH(Posted 2005) [#4]
This is possible in BlitzMax.


Great, is a stable win32 possible in Blitzmax? is it released officially?

Thought so, until then, keep your blitzmax in its proper forum.

/tired of blitzmax hype with no show.


Damien Sturdy(Posted 2005) [#5]
Would it be difficult for Mark to fix?


John Pickford(Posted 2005) [#6]
It might be an idea to return the instance.

Function CreateTypeWeapon.TypeWeapon()

   tw.TypeWeapon = New TypeWeapon
   tw\WeaponID% = 1
   tw\Ammo% = 500
   tw\FireRate% = 2
   tw\Velocity% = 100
   tw\Range% = 500
   tw\Damage% = 1
   tw\Force% = 1
   tw\Weight% = 50
   Return tw

End Function



WolRon(Posted 2005) [#7]
Hmm. How sad that it COMPILES but that it doesn't work...

Do you think this could be called a BUG that needs fixing? ;)


RGR(Posted 2005) [#8]
On the other hand
John pickfords solution ( I personally prefere the Handle / Object Variant ) gives you additional Options.
You could for instance pre-initialize your complete weaponset or fleet of spaceships.
Type TypeWeapon
	Field WeaponID%
	Field Ammo%
	Field FireRate%
	Field Velocity%
	Field Range%
	Field Damage%
	Field Force%
	Field Weight%
End Type
For i=1 To 2
	x.TypeWeapon=Object.TypeWeapon(CreateTypeWeapon(i))
	Print x\WeaponID
	Print x\Ammo
Next
WaitKey()
End

Function CreateTypeWeapon(Flag=0)
	tw.TypeWeapon = New TypeWeapon
	If Flag=1
		tw\WeaponID% = 1
		tw\Ammo% = 500
		tw\FireRate% = 2
		tw\Velocity% = 100
		tw\Range% = 500
		tw\Damage% = 1
		tw\Force% = 1
		tw\Weight% = 50
	ElseIf Flag=2
		tw\WeaponID% = 2
		tw\Ammo% = 2000
		tw\FireRate% = 30
		tw\Velocity% = 1000
		tw\Range% = 20000
		tw\Damage% = 4
		tw\Force% = 5
		tw\Weight% = 250
	EndIf
	Return Handle(tw)
End Function



Damien Sturdy(Posted 2005) [#9]
Ahhh yes, This is how the weapon system works in Supernova. Useful for say, a shoot function where you just want to tell it what kind of bullet needs to be shot.