passing typed array

Previous topic - Next topic

Richard Rae

Sorry if this has been covered elsewhere but I have searched the forum and couldn't find the answer.How do I pass a typed array to a function and manipulate it's values?

Kitty Hello

Code (glbasic) Select
TYPE Tfoo
bar
ENDTYPE

FUNCTION phoo: f[] AS Tfoo
   FOREACH pf IN f[]
      DEBUG pf.bar+"\n"
   NEXT
ENDFUNCTION
Arrays are always passed by reference. Types, too.

Richard Rae

Say I had a function that determined empty spaces on  a grid.This could obviously be used in several differant games.Would I always have the name of the type the same in each game or is there any way of passing the name of the type to the function.  eg function phoo:f[] as whatever type name. or would the name of the type have to be hardcoded into the function.

Schranz0r

No, you dont need a function !


Code (glbasic) Select
TYPE Tfoo
bar
ENDTYPE

LOCAL  f[] AS Tfoo

WHILE TRUE

   FOREACH pf IN f[]
      DEBUG pf.bar+"\n"
   NEXT

SHOWSCREEN
WEND
END
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Richard Rae

But that wouldn't be a reusable function.I don't want to be reinventing the wheel for every game I make.I would be handy if i hade a function library with premade files so that these could be included if I needed them.

Kitty Hello

then pack the TYPE and the function within the same gbas file.