An array containing a type

BlitzMax Forums/BlitzMax Beginners Area/An array containing a type

Dax Trajero(Posted 2006) [#1]
Cna someone tell me why this isn't working:

Type Tplayer
Field x
Field y
Field x_inc
Field y_inc
Field frame
End Type

Global player:Tplayer[] =New Tplayer[4]

player(1).x=0
player(1).y=200
player(1).x_inc=1
player(1).y_inc=1
player(1).frame=0

player(2).x=0
player(2).y=220
player(2).x_inc=2
player(2).y_inc=2
player(2).frame=32


Azathoth(Posted 2006) [#2]
You need New for every instance.
Also arrays in Bmax are like that of C, they include the index of 0 but end before the number you sized it with, in this case 0 to 3 which gives 4 elements.

Type Tplayer
	Field x
	Field y
	Field x_inc
	Field y_inc
	Field frame
End Type

Global player:Tplayer[] =New Tplayer[4]

player[0]=New Tplayer
player[0].x=0
player[0].y=200
player[0].x_inc=1
player[0].y_inc=1
player[0].frame=0

player[1]=New Tplayer
player[1].x=0
player[1].y=220
player[1].x_inc=2
player[1].y_inc=2
player[1].frame=32 



bradford6(Posted 2006) [#3]
hi, here are some tips:



First, you can Encapsulate the array within the Tplayer Type. This is called a Static Field

Type Tplayer
	Global PlayerArray:Tplayer[]


second, you can use a Constructor to help initialize the fields and add the Object to the list

Function AddPlayer:Tplayer(pname:String ,xpos:Float , ypos:Float)
		Local p = Len(Tplayer.PlayerArray)						' get the current number of elements in the array
		Tplayer.PlayerArray = Tplayer.PlayerArray[..p+1]		' Slice to resize the array
		Local temp:Tplayer = New Tplayer					' create a player and assign attributes
			temp.name = pname
			temp.x = xpos
			temp.y = ypos
		
		Tplayer.PlayerArray[p] = temp						' add the newly created player to the array
		Return temp	' return a reference to the newly crreated object (there if you need it)
	End Function 


Third, Instead of creating an array of a fixed size, you can slice it whenever you need to add a new player.

Tplayer.PlayerArray = Tplayer.PlayerArray[..p+1]		' Slice to resize the array



Dax Trajero(Posted 2006) [#4]
thanks all