Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array out of passing arrays...
#1
And it's way out!

So here is a method to use type variables, and still work with arrays, even though for some ****ing reason we can't assign arrays to a TYPE.

Code: (Select All)
Randomize Timer
Type foo
    x As Integer
    y As Integer
    one As Integer
    two As Integer
    three As Integer
    ind As Integer
    way As Integer
    object As String
End Type
Dim hp As foo
hp.object = "12345"
For hp.ind = 1 To 3
    Select Case hp.ind
        Case 1: hp.y = 1: hp.x = 35: hp.one = 1: hp.two = 2: hp.three = 3: hp.object = "first"
        Case 2: hp.y = 3: hp.x = 35: hp.one = 10: hp.two = 20: hp.three = 30: hp.object = "second"
        Case 3: hp.y = 5: hp.x = 35: hp.one = 100: hp.two = 200: hp.three = 300: hp.object = "third"
    End Select
    pete hp
Next
_Delay 1.5: hp.way = 1
Do
    hp.ind = Int(Rnd * 3) + 1
    If Int(Rnd * 10) = 1 Then hp.way = -hp.way
    pete hp
    _Limit 30
Loop

Sub pete (hp As foo)
    Static ArrayCounter$, oldind, i
    Static way(), y(), x(), one(), two(), three(), object$()
    i = hp.ind
    If InStr(ArrayCounter$, LTrim$(Str$(i)) + ",") = 0 Then
        ArrayCounter$ = ArrayCounter$ + LTrim$(Str$(i)) + ","
        ReDim _Preserve way(i), y(i), x(i), one(i), two(i), three(i), object$(i)
        hp.way = way(i): y(i) = hp.y: x(i) = hp.x: one(i) = hp.one: two(i) = hp.two: three(i) = hp.three: object$(i) = hp.object
    End If
    hp.y = y(i): hp.x = x(i): hp.one = one(i): hp.two = two(i): hp.three = three(i): hp.object$ = object$(i)
    way(i) = hp.way
    If way(i) < 0 Then
        If hp.x + way(i) > 0 Then hp.x = hp.x + way(i)
    Else
        If way(i) > 0 Then If hp.x + way(i) + Len(hp.object) <= _Width Then hp.x = hp.x + way(i)
    End If
    Color 8 - hp.ind
    Locate hp.y, 1: Print Space$(_Width);: Locate hp.y, hp.x: Print hp.object$;
    oldind = hp.ind: x(i) = hp.x
End Sub

So we don't have to declare our array in the main or pass it into the sub routine. Rather, the sub routine builds the arrays, and keeps them in memory. Now we just need to pass an index, as a type variable, so the sub routine knows which set of variables to pull from memory.

In this example, we have three words displayed on three text lines, and because of the sub routine memory, each can be moved independently. The conventional method would have required all of our variables built as arrays.

What do you guys think? And if you like it, please contribute to the "Buy Steve an aspirin" patreon account.

Pete
Reply
#2
Another way to have arrays in UDT's by using a String Type and loading it with all your array info that you want by separating each item with a delimiter, here are the tools:

https://qb64phoenix.com/forum/showthread...9#pid22809

You can do stuff with strings you can't with normal arrays like concat them!
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#3
(09-21-2025, 07:19 PM)Pete Wrote: And it's way out!

So here is a method to use type variables, and still work with arrays, even though for some ****ing reason we can't assign arrays to a TYPE.

Code: (Select All)
Randomize Timer
Type foo
    x As Integer
    y As Integer
    one As Integer
    two As Integer
    three As Integer
    ind As Integer
    way As Integer
    object As String
End Type
Dim hp As foo
hp.object = "12345"
For hp.ind = 1 To 3
    Select Case hp.ind
        Case 1: hp.y = 1: hp.x = 35: hp.one = 1: hp.two = 2: hp.three = 3: hp.object = "first"
        Case 2: hp.y = 3: hp.x = 35: hp.one = 10: hp.two = 20: hp.three = 30: hp.object = "second"
        Case 3: hp.y = 5: hp.x = 35: hp.one = 100: hp.two = 200: hp.three = 300: hp.object = "third"
    End Select
    pete hp
Next
_Delay 1.5: hp.way = 1
Do
    hp.ind = Int(Rnd * 3) + 1
    If Int(Rnd * 10) = 1 Then hp.way = -hp.way
    pete hp
    _Limit 30
Loop

Sub pete (hp As foo)
    Static ArrayCounter$, oldind, i
    Static way(), y(), x(), one(), two(), three(), object$()
    i = hp.ind
    If InStr(ArrayCounter$, LTrim$(Str$(i)) + ",") = 0 Then
        ArrayCounter$ = ArrayCounter$ + LTrim$(Str$(i)) + ","
        ReDim _Preserve way(i), y(i), x(i), one(i), two(i), three(i), object$(i)
        hp.way = way(i): y(i) = hp.y: x(i) = hp.x: one(i) = hp.one: two(i) = hp.two: three(i) = hp.three: object$(i) = hp.object
    End If
    hp.y = y(i): hp.x = x(i): hp.one = one(i): hp.two = two(i): hp.three = three(i): hp.object$ = object$(i)
    way(i) = hp.way
    If way(i) < 0 Then
        If hp.x + way(i) > 0 Then hp.x = hp.x + way(i)
    Else
        If way(i) > 0 Then If hp.x + way(i) + Len(hp.object) <= _Width Then hp.x = hp.x + way(i)
    End If
    Color 8 - hp.ind
    Locate hp.y, 1: Print Space$(_Width);: Locate hp.y, hp.x: Print hp.object$;
    oldind = hp.ind: x(i) = hp.x
End Sub

So we don't have to declare our array in the main or pass it into the sub routine. Rather, the sub routine builds the arrays, and keeps them in memory. Now we just need to pass an index, as a type variable, so the sub routine knows which set of variables to pull from memory.

In this example, we have three words displayed on three text lines, and because of the sub routine memory, each can be moved independently. The conventional method would have required all of our variables built as arrays.

What do you guys think? And if you like it, please contribute to the "Buy Steve an aspirin" patreon account.

Pete
HERE'S ANOTHER WAY TO ACCOMPLISH THIS !!!

Code: (Select All)

Type mytype
    x As Integer
    y As Integer
    m_array As _MEM
End Type


Dim my_array(0 To 100) As Integer

Dim myvar As mytype

myvar.m_array = _MemNew(Len(my_array()))

my_array(2) = 155

_MemPut myvar.m_array, myvar.m_array.OFFSET, my_array()

Print "ARRAY VALUE: "; MYINTVALUE(myvar, 2)
Print
End

Function MYINTVALUE (T As mytype, INDX As Long)
    Dim TMP As Integer
    _MemGet T.m_array, T.m_array.OFFSET + (INDX * 2), TMP
    MYINTVALUE = TMP
End Function
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Arrays inside Types? Ikerkaz 7 158 10 hours ago
Last Post: ahenry3068
  Arrays as UDTs Pete 26 1,092 02-06-2026, 04:31 AM
Last Post: bplus
  Preserving multi-dim arrays Pete 5 385 12-19-2025, 03:17 PM
Last Post: Dimster
  Huge array of variable length strings mdijkens 9 1,781 10-17-2024, 02:01 PM
Last Post: mdijkens
  An Array v's A Dictionary Dimster 10 1,828 10-08-2024, 06:59 PM
Last Post: TempodiBasic

Forum Jump:


Users browsing this thread: 1 Guest(s)