Types in an Array

BlitzMax Forums/BlitzMax Programming/Types in an Array

Can you put a type into an array or access a type variable using numbers similar to an array so you can single out certain ones?

IIRC you can put types in an array.

For types and relatives, have a look here: http://blitzwiki.org/index.php/Object_Oriented_Programming

yep, you just create a normal variable of the type you want and stick [100] on the end (replace 100 of course) e.g

Local Coords:TPoint[MAX_POINTS]

Then you can do Coords[0].X = 5 or Coords[55].Y = 10 etc.

When you create an array of types, you are only creating NULL pointers. You must remember to initialize the elements before using the array.
Local Coords:TPoint[MAX_POINTS]

For Local t:Int = 0 to MAX_POINTS - 1
     Coords[t] = New TPoint
Next

Coords[0].X = 5
etc...


oops yeah I forgot to mentionthat. Furthermore you might want to manually clean them out when killing the array.

Thanks that works awesome!

here is another example:


Graphics 640,480


Type Tveggie
	Field name:String
	Field freshness:Int

End Type


Type Tpot
	Field name:String
	Field x:Int
	Field y:Int
	Field PotContents:Tveggie[10]
	Field MAXVEGGIES:Int = 10
	Field Veggiecount:Int
	
	Method AddVeggie(vname:String="Generic Vegetable",fresh:Int=100) 
		Veggiecount:+1
		PotContents[Veggiecount].name = vname
		PotContents[Veggiecount].freshness = fresh
	
	End Method
	
	Method Display()
		SetColor 255,0,0
		DrawText name,x,y
		SetColor 255,255,0
		For Local t:Int = 0 To MAXVEGGIES - 1
	     	DrawText PotContents[t].name,x,y+(t*TextHeight("H"))
		Next

	
	
	End Method
	
	
	
	Function create:Tpot(pname:String,xp,yp)
		Local temp:Tpot = New Tpot
		temp.name = pname
		temp.x = xp
		temp.y = yp
		For Local t:Int = 0 To temp.MAXVEGGIES - 1
	     temp.PotContents[t] = New Tveggie
		Next
	
		
		Return temp
	End Function
End Type


Local Dinner:Tpot = Tpot.create("Bill's Stew",100,100)
Dinner.AddVeggie("Carrot",5)
Dinner.AddVeggie("Beet",80)
Dinner.AddVeggie("Lettuce",5)
Dinner.AddVeggie("Corn",80)

Repeat
	Dinner.display()

Flip
Cls
Until KeyDown(KEY_ESCAPE)



lol @ bradford6, nice one. Actually it's one of the methods I was discussing in this thread that no one replied to:

http://www.blitzbasic.com/Community/posts.php?topic=60050

this one is a lttle better. this will dynamically resize the Array as you add and delete elements:

the speed of arrays with some of the useability of lists

the cool thing about this is that an instance of an object can have an array of "instances of another type".

could be useful for lots of stuff that changes.

I'll knock up a different example later:

Graphics 640,480


Type Tveggie
	Field name:String
	Field freshness:Int

End Type


Type Tpot
	Field name:String
	Field x:Int
	Field y:Int
	Field PotContents:Tveggie[10]
	Field MAXVEGGIES:Int = 1000
	Field Veggiecount:Int
	
	Method AddVeggie(vname:String="Generic Vegetable",fresh:Int=100) 
		
		veggiecount:+1
		self.PotContents = self.PotContents[..veggiecount]
		Print "Potcontents="+Len(potcontents)
		PotContents[veggiecount-1] = New Tveggie
		PotContents[veggiecount-1].name = vname
		PotContents[veggiecount-1].freshness = fresh
	
	End Method
	
	Method DeleteVeggie(vegname:String,all:Int=-1,index:Int=-1)
		For Local t:Int = 0 To veggiecount - 1
			If vegname = PotContents[t].name 
			    Local deleted:Int = 1
				Print "Index:="+t
				Print "Deleting:"+PotContents[t].name
				
				Local PL:Int = Len(self.PotContents)
				Print "PL="+PL
				
				
				For XX = 0 To PL-1
					Print "xx:"+xx+"  potindex:"+t
					If XX+potindex>PL-1 Then Exit
					
					Print self.Potcontents[xx].name+"::"+self.potcontents[xx+potindex].name
					
					If XX = t Then potindex=1
					If XX = PL-1 Then potindex=0
					self.Potcontents[xx] = self.potcontents[XX+potindex]
								
				Next
				
				
				veggiecount:-1
				self.PotContents = self.PotContents[..veggiecount]
				Exit
				
				
			EndIf
			
		Next
		
		Return Deleted
	End Method
	
	
	
	
	Method Display()
		SetColor 255,0,0
		DrawText name,x,y
		SetColor 255,255,0
		Local TH=TextHeight("H")
		For Local t:Int = 0 To veggiecount - 1
	     	DrawText t+":"+PotContents[t].name,x,y+(TH+t*TH)
		Next
		DrawText "Total Veggies:"+veggiecount,x,y+(TH*(veggiecount+1))
		DrawText "array size:"+Len(potcontents),x,y+(TH*(veggiecount+2))

	
	End Method
	
	Method Exists(vegname:String)
		For Local t:Int = 0 To veggiecount - 1
			If vegname = PotContents[t].name 
				Return True
			EndIf
		Next
	
	Return False
	
	End Method
	
	Function create:Tpot(pname:String,xp,yp)
		Local temp:Tpot = New Tpot
		temp.name = pname
		temp.x = xp
		temp.y = yp
		
	     temp.PotContents[0] = New Tveggie
		
	
		
		Return temp
	End Function
End Type


Local Dinner:Tpot = Tpot.create("Bill's Stew",100,100)
Dinner.AddVeggie("Carrot",5)
Dinner.AddVeggie("Beet",80)
Dinner.AddVeggie("Lettuce",5)
Dinner.AddVeggie("Dirty Sock",80)
Dinner.AddVeggie("Okra",80)
Dinner.AddVeggie("Peas",80)
Dinner.AddVeggie("Telephone",80)



Repeat
	
	If Dinner.exists("Dirty Sock") = 1 Then DrawText "Left Click to remove Sock",MouseX(),MouseY()
	If Dinner.exists("Telephone") = 1Then DrawText "Right Click to remove Telephone",MouseX(),MouseY()+16
	Dinner.display()
	If MouseDown(1)=1 
		d = Dinner.DeleteVeggie("Dirty Sock")
		Print "Deleted = "+d
	EndIf
	If MouseDown(2)=1 
		d = Dinner.DeleteVeggie("Telephone")
		Print "Deleted = "+d
	EndIf




Flip
Cls
Until KeyDown(KEY_ESCAPE)