Hey, I came up with this solution to GUI in games, I thought it was clever way of linking gui to specific functions without having to hardcore it with ifs.
You will need IanM's plugins for this one, but I hope it's helpful.
`set sync on, as usual
sync on:sync rate 0
#constant max_gui 10
`create a type that our gui buttons will use
type gui_type
act `to check whether the gui is active
x `x position of the gui
y `y position of the gui
obj `since I plan to use this a 3d plain locked to the screen, this is the object id
co `this is the coroutine id
func$ `this is the function the gui will perform
endtype
`create the array to hold the data about our gui
dim gui( max_gui ) as gui_type
`create a button with the print_hello() function
make_gui( 0,0,"print_hello" )
`our main loop
do
`check if the mouse has been clicked
if mouseclick() = 1
`check the 'click' flag to see if it was clicked last loop, if so, ignore the mouseclick
if click = 0
`set the flag to indicate the mouse was indeed clicked in this loop
click = 1
`use the pick object command to find which object is on screen where the mouse is
temp = pick object( mousex(),mousey(),0,100 )
`if there is an object there loop through our gui
if temp > 0
for i = 1 to max_gui
`if the object clicked matches our gui object
if gui( i ).obj = temp
`activate the gui function
activate_gui( i )
endif
next i
endif
endif
else
`if the mouse is not clicked, set the flag to 0
click = 0
endif
`as always, sync the screen.
sync
loop
function activate_gui( i )
`this is the cool bit :)
`set the co variable in our gui array to the id of a free coroutine
gui( i ).co = find free coroutine()
`get a pointer to the function stored in the func$ variable
pointer = get ptr to function( gui( i ).func$ )
`if there is a pointer returned, the value will be greater than 0, if not, the function does not exist
if pointer > 0
`create the coroutine using our free coroutine id, and the pointer to the function
create coroutine gui( i ).co, pointer
endif
`use this command to run the coroutine we have just created, it will be destroyed after running
switch to coroutine gui( i ).co
endfunction
function print_hello()
`this is just a test function, you can change it to do whatever you wish it to do
print "HELLO"
print "Press Any Key"
sync
wait key
endfunction
function make_gui( x,y,func$ )
`this code loops through our gui to find an index which isn't in use
for i = 1 to max_gui
if gui( i ).act = 0
exit
endif
next i
`it fills in the variables of that array
gui( i ).func$ =func$
gui( i ).act = 1
gui( i ).x = x
gui( i ).y = y
`and creates the object we see on screen
gui( i ).obj = find free object()
make object plain gui( i ).obj,1,1
endfunction
