Initializing a Custom Type Array

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

Hi all,

I'm working on a card game right now, and am creating a player type that (amongst other things) will keep track of their hand of 5 card objects.

I'm not sure why, but while the first example below compiles, while the second one doesn't. Can anyone help me figure out why? Thanks much.

Type TPlayer
	
	Field playerHand:TCard[] = New TCard[5] 
	
End Type


(This next one doesn't work - I get "Expecting expression but encountered '='")

Type TPlayer
	
	Field playerHand:TCard[] 
	
	Function Create()   'instantiates a player
	
		playerHand[] = New TCard[5] 
	
	End Function
End Type


Change the function into a method and don't use the [] for the array, it's already defined in the Field

Type TPlayer
	
	Field playerHand:TCard[] 
	
	Method Create()   'instantiates a player
	
		playerHand = New TCard[5] 
	
	End Method 
End Type


... I would do the following :
Type TPlayer
	
	Field playerHand:TCard[] 
	
	Function Create()   'instantiates a player
		Local temp:TPlayer = New TPlayer
		temp.playerHand = New TCard[5] 
	End Function
	
End Type

Depending on the rest of the program I might even make the temp.playerhand assignment the return from another function/method call.

Yep that's correct! Overlooked the 'instantiates a player
comment.

Ok. I follow what you are doing, tonyg, but I'm not sure why. Can you explain why you'd do it that way, please? I'm assuming it's a cleaner way to do things, but I'm not positive on what's underpinning it.

Also - you guys understand why the original way I had it doesn't compile?

Thanks very much for the help.

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 []

Ah! Got it guys, thanks. Part of the problem was the mixup on my end of methods and functions when dealing with a not-yet-created type.

Thanks on the extra explanation CZ - I think I'm on the right track, I'll give a shout if this flummoxes me again.

One more question - to return temp as above, do I need to cast it somehow? Trying the below doesn't work for me, Unable to Convert from Int to Foo.

Strict

Type Foo
	Function Create() 
		Local temp:Foo = New Foo
		Return temp
	End Function
End Type

Global fooInstance:Foo = New Foo.Create()


Thanks.

EDIT: Ah. I was doing Create() instead of Create:Foo()

I'm figuring when you use :<type> after a function, you're specifying return type?

I'm figuring when you use :<type> after a function, you're specifying return type?
Yulp.

Hello.

Another way to initialize fields are the method new()

If new() exist it is called at creation of object.
example:
Type TPlayer
	Field playerHand:TCard[] 
	
	method new()	
	     playerHand = New TCard[5] 
	end method
End Type


If you use SuperStrict you correct your code more well.

Ok, thanks. I'm planning on moving to SuperStrict once the support is in for Grey Alien's Framework, which I'm using.