Gui handle undefined when initialized in a subroutine

Here we can discuss about the problem found
Post Reply
User avatar
karlkloss
Posts: 169
Joined: Fri Aug 18, 2023 12:21 pm
Has thanked: 24 times
Been thanked: 35 times

Gui handle undefined when initialized in a subroutine

Post by karlkloss »

So, I want to write some universal subroutines (like numpads and keyboards) that open GUI popups, and want to put them in files for importing.

As they define a lot of buttons, I want to use a single event for the clicks, and identify them by their handle.

When I define everything "flat" by using labels, it works as it should.

This sample open a second button when the button "Open" is pressed, and when the second button is pressed, prints it's handle value.
It works as expected.

Code: [Local Link Removed for Guests]

gosub Page0
gui.autorefresh 30,1
wait

Page0:      
numpadrunning = 0
GUI.init 50, blue 
but_Value = gui.button(10,35,70,40,"Open",4,0,0,1,black,white,red,red) 
gui.setevent but_Value,1,Valueclick  
Return            

Valueclick: 
If NOT numpadrunning Then 
   x=10           
   y=80
   gosub numpad      
Endif
Return

numpad:
numpadrunning = 1
num_1 = gui.button(x, y,70,40,"Close",4,4,0,10,black,white,red,black)
gui.setevent num_1, 1, numclick  
Return

numclick:                        
wlog(num_1)
Return
But when I define the code in "numpad" as a subroutine I get the error "No such variable".

Code: [Local Link Removed for Guests]

gosub Page0
gui.autorefresh 30,1
wait

Page0:      
numpadrunning = 0
GUI.init 50, blue 
but_Value = gui.button(10,35,70,40,"Open",4,0,0,1,black,white,red,red) 
gui.setevent but_Value,1,Valueclick  
Return            

Valueclick: 
If NOT numpadrunning Then 
   x=10           
   y=80
   numpad x, y      
Endif
Return

sub numpad(x, y)
numpadrunning = 1
num_1 = gui.button(x, y,70,40,"Close",4,4,0,10,black,white,red,black)
gui.setevent num_1, 1, numclick  
end sub

numclick:                        
wlog(num_1)
Return
Is this an expected behaviour or a bug?
How can I avoid/circumvent it?
Is there another method to put such functionality inside a subroutine?
User avatar
karlkloss
Posts: 169
Joined: Fri Aug 18, 2023 12:21 pm
Has thanked: 24 times
Been thanked: 35 times

Re: Gui handle undefined when initialized in a subroutine

Post by karlkloss »

I forgot to mention, when I define num_1 ouside of the sub, it also doesn't work.
And whether numclick is defined inside or outside of the sub doesn't matter.
Post Reply