Arrays inside Types


Attention! 👉Starting November 2024, BlitzCoder.org will now be BlitzBasic.org.

 

Tweet blitz3d code-archives miscellaneous tutorials
BlitzCoder

How to use arrays (DIM) in Types

Notes:

  • multidimensional arrays are not supported
  • As a workaround for the multidimensional array limitation above, multiplying the range of each by the others can be applied for integers. Ex: Field hhmmss[24*60*60]
Graphics 800,600,0,2

Type CHAIR 
Field X[2]
Field Y[2]
Field HEIGHT 
End Type 

For tempx = 1 To 5 
For tempy = 1 To 5 
room.chair = New Chair 
room\x[1] = tempx 
room\y[1] = tempy 
room\x[2] = tempx+100
room\y[2] = tempy+100
room\height = Rnd(0,10) ; set a random height 0 to 10 
Next 
Next 

For room.chair = Each chair 
Print room\x[1] + " " + room\y[1]
Next 

WaitKey

For room.chair = Each chair 
Print room\x[2] + " " + room\y[2]
Next 

WaitKey
End
ManoBrz commented:

Obrigado Rontec, eu gostaria de aproveitar a oportunidade para fazer uma pergunta, é possível pegar um objeto de dentro de uma Type sem usar o loop For?
Por exemplo, eu quero evitar isso:

Type colecao

    Field numero
    Field Multiplicado

End Type

For num=1 To 10

    Novo.colecao=New colecao

    Novo\Numero=num
    Novo\Multiplicado=num*100

Next

For Novo.colecao=Each colecao
    If Novo\Numero=3
    Print Novo\Multiplicado
    EndIf
Next

WaitKey
End

Nesse caso como eu faria para pegar o valor da Field Multiplicado sem usar o loop For Novo.colecao=Each colecao, também não quero usar After, Before, First nem Last(eu acho que vc recomendaria usar uma dessas).

BlitzCoder commented:

You can just save one or more instance by placing the value in another instance of that type perhaps set it to Global, example..

Graphics 800,600,0,2

Type chair
    Field Name$
    Field Height
End Type

Global room.chair

;New instance to save
Global bad.chair = New chair

room = New chair
room\Name = "Nice Chair"

room = New chair
room\Name = "Bad Chair"

For room = Each chair
    Print room\Name
    ; save the name value to bad.chair if Name = Bad Chair
    If room\Name = "Bad Chair" Then bad\Name = room\Name
Next

WaitKey

; access a particular instance without using iterations that is saved from the conditional statement above
Print bad\Name

WaitKey()
End

Reply To Topic (minimum 10 characters)

Please log in to reply