reading types

Blitz3D Forums/Blitz3D Programming/reading types

Rook Zimbabwe(Posted 2004) [#1]
What if I wanted to read the seventh type value and determine what info it holds??? or if I wanted to compare the seventh type value and all its fields with the 19th type value and all its fields???

I asked this question and then I got to thinking about it. Types are much faster and if all my pieces were types I could delete the ones I don't need and make new types... But how to read specific types???


Warren(Posted 2004) [#2]
You either have to maintain a manually updated array of types, or loop until you hit the one you want:

idx = 0
for var.mytype = each mytype
  idx = idx + 1
  if idx = 7
    exit
  endif
next

; var contains the 7th one...



N(Posted 2004) [#3]
You could try something similar to this.

Type Blah
   Field Value
End Type

For N = 0 To 300
   B.Blah = New Blah
   B\Value = Rand(30530)
   If N = 200 Then B2.Blah = B
Next


Function GetBlah.Blah(Index)
   Local N = 0
   B.Blah = First Blah
   While B <> Null And N < Index-1
      B = After B
      N = N + 1
   Wend
   Return B
End Function

B.Blah = GetBlah(200)
If B <> B2 Then Print "Same freakin' Blah"
WaitKey



Rook Zimbabwe(Posted 2004) [#4]
I tried this out of the example but I get nothing...
; Define the CHAIR Type

Type CHAIR
Field X
Field Y
Field HEIGHT
End Type

; Create 100 new chairs using FOR ... NEXT using the collection name of ROOM 

For tempx = 1 to 10
For tempy = 1 to 10
room.chair = New Chair
room\x = tempx
room\y = tempy
room\height = Rnd(0,10) ; set a random height 0 to 10
Next
Next 

; Move them all over 1 (like the description example) 

For room.chair = Each chair
room\x = room\x + 1
Next


While Not KeyHit(1)

For room.chair = Each chair

If chair=13 Then
		g=room\x
		j=room\y
		h=room\height
EndIf	

Next
Text 0,10,""+g
Text 0,20,""+j
Text 0,30,""+h

Wend

End



N(Posted 2004) [#5]
; Define the CHAIR Type

Type CHAIR
Field X
Field Y
Field HEIGHT
End Type

; Create 100 new chairs using FOR ... NEXT using the collection name of ROOM 

For tempx = 1 to 10
For tempy = 1 to 10
room.chair = New Chair
room\x = tempx
room\y = tempy
room\height = Rnd(0,10) ; set a random height 0 to 10
Next
Next 

; Move them all over 1 (like the description example) 

For room.chair = Each chair
room\x = room\x + 1
Next
cChair = 13

While Not KeyHit(1)

cChair = cChair + KeyHit(3) - KeyHit(2)

For room.chair = Each chair

If chair=cChair Then
		g=room\x
		j=room\y
		h=room\height
EndIf

chair = chair + 1 ;; WHAT YOU FORGOT TO ADD.

Next

chair = 0

Text 0,10,""+g
Text 0,20,""+j
Text 0,30,""+h

Flip
Cls

Wend

End



Zethrax(Posted 2004) [#6]
Just stick them in a type pointer array declared for that type, as EpicBoy said.

Type my_type
Field whatever
End Type

Dim my_type_array.my_type( 9999 )

my_type_array( 0 ) = New my_type
my_type_array( 1 ) = New my_type
my_type_array( 2 ) = New my_type
my_type_array( 3 ) = New my_type
my_type_array( 4 ) = New my_type
my_type_array( 5 ) = New my_type
my_type_array( 6 ) = New my_type
my_type_array( 7 ) = New my_type

whatever_var = my_type_array( 6 )\whatever


You will need a dynamic array slot number allocation system if you are creating and deleting types dynamically. Use another type list to store any deleted array slot numbers, and grab a new number from this list, by default, if there are any elements on the deleted array slot number list. When you delete a type element from the 'my_type' list in the example above, you would add it to the deleted array slot number list so that it can be reused when a new 'my_type' element is required.


Rook Zimbabwe(Posted 2004) [#7]
Very interesting... The only thing I don't understand is in Noels code... this line:
cChair = cChair + KeyHit(3) - KeyHit(2)
You have already set the cChair to 13... what does this do???
-RZ


aCiD2(Posted 2004) [#8]
its like toggle :) think of it this way...

you press 2, so keyhit(3) = 1 and presumably keyhit(2) = 0 so..
cChair = cChair + 1 - 0

Create aniter chair

you press 1 so keyhit(3) = 0 and keyhit(2) = 1 so...
cChair = cChair + 0 - 1

Destroy a chair.

Tis simple, and useful, just like Noel :P