GLBasic forum

Main forum => GLBasic - en => Topic started by: Richard Rae on 2007-Dec-18

Title: passing typed array
Post by: Richard Rae on 2007-Dec-18
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?
Title: passing typed array
Post by: Kitty Hello on 2007-Dec-18
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.
Title: passing typed array
Post by: Richard Rae on 2007-Dec-18
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.
Title: passing typed array
Post by: Schranz0r on 2007-Dec-18
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
Title: passing typed array
Post by: Richard Rae on 2007-Dec-18
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.
Title: passing typed array
Post by: Kitty Hello on 2007-Dec-18
then pack the TYPE and the function within the same gbas file.