QB64 Phoenix Edition
Array out of passing arrays... - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2)
+--- Thread: Array out of passing arrays... (/showthread.php?tid=3956)



Array out of passing arrays... - Pete - 09-21-2025

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


RE: Array out of passing arrays... - bplus - 09-22-2025

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.php?tid=2414&pid=22809#pid22809

You can do stuff with strings you can't with normal arrays like concat them!


RE: Array out of passing arrays... - ahenry3068 - 09-22-2025

(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