January 26, 2021, 12:39:01 PM

### Author Topic: [bb] array of types by bradford6 [ 1+ years ago ]  (Read 566 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] array of types by bradford6 [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : array of types
Posted : 1+ years ago

Description : this is an example of a type with a field that is itself an array of types.
the array is dynamically resized with the ADD / Delete methods

Code :
Code: BlitzBasic
1. Graphics 640,480
2.
3.
4. Type Tveggie
5.         Field name:String
6.         Field freshness:Int
7.
8. End Type
9.
10.
11. Type Tpot
12.         Field name:String
13.         Field x:Int
14.         Field y:Int
15.         Field PotContents:Tveggie[10]
16.         Field MAXVEGGIES:Int = 1000
17.         Field Veggiecount:Int
18.
20.
21.                 veggiecount:+1
22.                 self.PotContents = self.PotContents[..veggiecount]
23.                 Print "Potcontents="+Len(potcontents)
24.                 PotContents[veggiecount-1] = New Tveggie
25.                 PotContents[veggiecount-1].name = vname
26.                 PotContents[veggiecount-1].freshness = fresh
27.
28.         End Method
29.
30.         Method DeleteVeggie(vegname:String,all:Int=-1,index:Int=-1)
31.                 For Local t:Int = 0 To veggiecount - 1
32.                         If vegname = PotContents[t].name
33.                             Local deleted:Int = 1
34.                                 Print "Index:="+t
35.                                 Print "Deleting:"+PotContents[t].name
36.
37.                                 Local PL:Int = Len(self.PotContents)
38.                                 Print "PL="+PL
39.
40.
41.                                 For XX = 0 To PL-1
42.                                         Print "xx:"+xx+"  potindex:"+t
43.                                         If XX+potindex>PL-1 Then Exit
44.
45.                                         Print self.Potcontents[xx].name+"::"+self.potcontents[xx+potindex].name
46.
47.                                         If XX = t Then potindex=1
48.                                         If XX = PL-1 Then potindex=0
49.                                         self.Potcontents[xx] = self.potcontents[XX+potindex]
50.
51.                                 Next
52.
53.
54.                                 veggiecount:-1
55.                                 self.PotContents = self.PotContents[..veggiecount]
56.                                 Exit
57.
58.
59.                         EndIf
60.
61.                 Next
62.
63.                 Return Deleted
64.         End Method
65.
66.
67.
68.
69.         Method Display()
70.                 SetColor 255,0,0
71.                 DrawText name,x,y
72.                 SetColor 255,255,0
73.                 Local TH=TextHeight("H")
74.                 For Local t:Int = 0 To veggiecount - 1
75.                 DrawText t+":"+PotContents[t].name,x,y+(TH+t*TH)
76.                 Next
77.                 DrawText "Total Veggies:"+veggiecount,x,y+(TH*(veggiecount+1))
78.                 DrawText "array size:"+Len(potcontents),x,y+(TH*(veggiecount+2))
79.
80.
81.         End Method
82.
83.         Method Exists(vegname:String)
84.                 For Local t:Int = 0 To veggiecount - 1
85.                         If vegname = PotContents[t].name
86.                                 Return True
87.                         EndIf
88.                 Next
89.
90.         Return False
91.
92.         End Method
93.
94.         Function create:Tpot(pname:String,xp,yp)
95.                 Local temp:Tpot = New Tpot
96.                 temp.name = pname
97.                 temp.x = xp
98.                 temp.y = yp
99.
100.              temp.PotContents[0] = New Tveggie
101.
102.
103.
104.                 Return temp
105.         End Function
106. End Type
107.
108.
109. Local Dinner:Tpot = Tpot.create("Bill's Stew",100,100)
117.
118.
119.
120. Repeat
121.
122.         If Dinner.exists("Dirty Sock") = 1 Then DrawText "Left Click to remove Sock",MouseX(),MouseY()
123.         If Dinner.exists("Telephone") = 1Then DrawText "Right Click to remove Telephone",MouseX(),MouseY()+16
124.         Dinner.display()
125.         If MouseDown(1)=1
126.                 d = Dinner.DeleteVeggie("Dirty Sock")
127.                 Print "Deleted = "+d
128.         EndIf
129.         If MouseDown(2)=1
130.                 d = Dinner.DeleteVeggie("Telephone")
131.                 Print "Deleted = "+d
132.         EndIf
133.
134.
135.
136.
137. Flip
138. Cls
139. Until KeyDown(KEY_ESCAPE)

N(Posted 1+ years ago)

I like my T when it comes in Tpots.

_PJ_(Posted 1+ years ago)

So do we just need to change the KEY_ESCAPE to 1 and the Method to Function to utilise this?