Initializing a Custom Type Array

BlitzMax Forums/BlitzMax Beginners Area/Initializing a Custom Type Array

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I think tonyg misssed this out:
Type TPlayer
	
	Field playerHand:TCard[] 
	
	Function Create:TPlayer()   'instantiates a player
		Local temp:TPlayer = New TPlayer
		temp.playerHand = New TCard[5]
		Return temp
	End Function
	
End Type


To make a new player, do this
main code blah blah
Local aplayer:TPlayer = TPlayer.Create()
aplayer.blahblahblah



Don't forget that = New TCard[5] sets up an array, but doesn't set up the cards inside. You need to to this seperately. If you don't know how I can tell you.

A create function like this is useful because you can add some parameters for your new object. Here's a make believe example:

Type TPlayer
	
	Field playerHand:TCard[] 
	
	Function Create:TPlayer(numCards:Int)   'instantiates a player
		Local temp:TPlayer = New TPlayer
		temp.playerHand = New TCard[numCards]
		Return temp
	End Function
	
End Type

Local aplayer:TPlayer = TPlayer.Create(5)
Local otherplayer:TPlayer = TPlayer.Create(4)

So this lets you set up players with any number of cards. Nifty eh?


PS Your original code didn't compile because of the unneeded []