Jump to content

Access Local Scope


Recommended Posts

How I can access a Local Scope from outside
Is there a better way except creating Globals ?

 

#include <GUIConstants.au3>

#cs
Global $GUI_Window
Global $GUI_Label
#ce

Opt("GUIOnEventMode", 1)

Gui_P1()

While 1
    Sleep(100)
WEnd
Func Gui_P1()
     $GUI_Window = GUICreate("P1", 1050, 720, -1, -1)
     $GUI_Label = GUICtrlCreateLabel("<Nothing>", 25, 20)

    Local $GUI_Button = GUICtrlCreateButton("Modify",25,300,100)
    GUICtrlSetOnEvent($GUI_Button, "Modify")

    GUISetOnEvent($GUI_EVENT_CLOSE, "Gui_P1_Close")
    GUISetState(@SW_SHOW, $GUI_Window)
EndFunc

Func Gui_P1_Close()
    GUIDelete($GUI_Window)
    Exit
EndFunc

Func Modify()
    ConsoleWrite(@CRLF & ">Move")
    GUICtrlSetPos($GUI_Label, Random(25,250,1), Random(25,250,1))
    ConsoleWrite(@CRLF & ">Rewrite")
    GUICtrlSetData($GUI_Label,"Number: " & Random(0,99,1))
EndFunc

 

Edited by AndroidZero
Link to comment
Share on other sites

Byref parameters or return from functions to variables and pass those to other functions.  or don't use local for variables needed globally

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

The only way to access local is to pass them between the functions. If you're not calling the other function directly from within the first function, make the variables Global, that's the point of using Global variables, so they can be accessed anywhere in the script in any function. Also, if you're declaring them as Global, do not declare them inside the functions, declare them at the start of the script. Because you're using OnEvent mode, you could call one of the functions using the global before you called the function they might be declared in.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Yes, you can use nested\functions & parameters sent from your main loop 

Gui_P1_Close($GUI_Window)

Func Gui_P1_Close($hGUI_close = "")
    GUIDelete($hGUI_close)
    Exit
EndFunc   ;==>Gui_P1_Close

Modify($GUI_Label)

Func Modify($GUI_Label = "") 
    ConsoleWrite(@CRLF & ">Move")
    GUICtrlSetPos($GUI_Label, Random(25, 250, 1), Random(25, 250, 1))
    ConsoleWrite(@CRLF & ">Rewrite")
    GUICtrlSetData($GUI_Label, "Number: " & Random(0, 99, 1))
EndFunc   ;==>Modify

 

Edited by Deye
Link to comment
Share on other sites

4 minutes ago, Deye said:

Yes, you can use nested\functions & parameters sent from your main loop 

He's using OnEvent mode to call those functions, he'd have to rewrite the script to use MessageLoop mode, but it can be done.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

1 hour ago, BrewManNH said:

He's using OnEvent mode

Oops, didn't notice that line

Then in that case :

1 hour ago, BrewManNH said:

make the variables Global, that's the point of using Global variables

Or use MessageLoop mode for more of other available options

Deye

 

Edited by Deye
Link to comment
Share on other sites

Ok I understand but my wish is to reduce the ammount of globals in my script to a minimum to avoid side effects and ress memory usage.
The thing with MessageLoops like GuiGetMsg() or TrayGetMsg() is that I can access the Local Scope BUT I can't go simultaneously into another MessageLoop.
That means I can't open another GUI or click in the TrayIcon.

Just to make it clear what my goal is:
I want to generate a TrayIcon with TrayMenus/items where I can open different GUIs or simply call functions but always being able to control the TrayIcon at any time and start further functions.
I extended the example script above with my needs using MessageLoops but how explained it doesn't work how I wish it should.
Any suggestion what is the best way to realize my plan ? Or is there no way to bypass declaring Globals ?

#include <GUIConstants.au3>

#cs
Global $GUI_Window
Global $GUI_Label
#ce

;~ Opt("GUIOnEventMode", 1)
Opt("TrayMenuMode", 3)

Tray_T1()

Func Gui_P1()
    Local $GUI_Window = GUICreate("P1", 250, 250, -1, -1)
    Local $GUI_Label = GUICtrlCreateLabel("<Nothing>", 25, 20)
    Local $GUI_Button1 = GUICtrlCreateButton("Modify",0,175,250)
    Local $GUI_Button2 = GUICtrlCreateButton("Back",0,200,250)
    Local $GUI_Button3 = GUICtrlCreateButton("Exit",0,225,250)


    GUISetState(@SW_SHOW, $GUI_Window)

    While 1
        $idMsg = GUIGetMsg()
        Switch $idMsg
            Case $GUI_EVENT_CLOSE
                ConsoleWrite(@CRLF & ">$GUI_EVENT_CLOSE")
                Gui_Delete($GUI_Window)
                Tray_T1()
            Case $GUI_Button1
                ConsoleWrite(@CRLF & ">$GUI_Button1")
                Modify($GUI_Label)
            Case $GUI_Button2
                ConsoleWrite(@CRLF & ">$GUI_Button2")
                GUIDelete($GUI_Window)
                Tray_T1()
            Case $GUI_Button3
                ConsoleWrite(@CRLF & ">$GUI_Button3")
                Local $resp = MsgBox(262144 + 64 + 4, "Attention", "Do you really want to quit the whole program ?")
                If $resp == "1" Or $resp == "6" Or $resp == "11" Then
                    GUIDelete($GUI_Window)
                    Exit
                ElseIf $resp == "2" Or $resp == "3" Or $resp == "7" Then
                    ;Nothing
                EndIf
        EndSwitch
    WEnd
EndFunc
Func Gui_P2()
    Local $GUI_Window = GUICreate("P2", 250, 250, -1, -1)
    Local $GUI_Label = GUICtrlCreateLabel("<Nothing>", 25, 20)
    Local $GUI_Button1 = GUICtrlCreateButton("Modify",0,175,250)
    Local $GUI_Button2 = GUICtrlCreateButton("Back",0,200,250)
    Local $GUI_Button3 = GUICtrlCreateButton("Exit",0,225,250)


    GUISetState(@SW_SHOW, $GUI_Window)

    While 1
        $idMsg = GUIGetMsg()
        Switch $idMsg
            Case $GUI_EVENT_CLOSE
                ConsoleWrite(@CRLF & ">$GUI_EVENT_CLOSE")
                Gui_Delete($GUI_Window)
                Tray_T1()
            Case $GUI_Button1
                ConsoleWrite(@CRLF & ">$GUI_Button1")
                Modify($GUI_Label)
            Case $GUI_Button2
                ConsoleWrite(@CRLF & ">$GUI_Button2")
                GUIDelete($GUI_Window)
                Tray_T1()
            Case $GUI_Button3
                ConsoleWrite(@CRLF & ">$GUI_Button3")
                Local $resp = MsgBox(262144 + 64 + 4, "Attention", "Do you really want to quit the whole program ?")
                If $resp == "1" Or $resp == "6" Or $resp == "11" Then
                    GUIDelete($GUI_Window)
                    Exit
                ElseIf $resp == "2" Or $resp == "3" Or $resp == "7" Then
                    ;Nothing
                EndIf
        EndSwitch
    WEnd
EndFunc
Func Gui_Delete($gui)
    GUIDelete($gui)
EndFunc
Func Modify($control)
    ConsoleWrite(@CRLF & ">Move")
    GUICtrlSetPos($control, Random(0,200,1), Random(0,175,1))
    ConsoleWrite(@CRLF & ">Rewrite")
    GUICtrlSetData($control,"Number: " & Asc(Random(0,99,1)))
EndFunc

Func Tray_T1()
    $Tray_Item1 = TrayCreateItem("Gui_P1")
    $Tray_Item2 = TrayCreateItem("Gui_P2")
    $Tray_Item3 = TrayCreateItem("Exit")

    While 1
        Switch TrayGetMsg()
            Case $Tray_Item1
                ConsoleWrite(@CRLF & ">$Tray_Item1")
                TrayItemDelete($Tray_Item1)
                TrayItemDelete($Tray_Item2)
                TrayItemDelete($Tray_Item3)
                Gui_P1()
            Case $Tray_Item2
                ConsoleWrite(@CRLF & ">$Tray_Item2")
                TrayItemDelete($Tray_Item1)
                TrayItemDelete($Tray_Item2)
                TrayItemDelete($Tray_Item3)
                Gui_P2()
            Case $Tray_Item3
                ConsoleWrite(@CRLF & ">$Tray_Item3")
                Exit
        EndSwitch
    WEnd

EndFunc

 

Edited by AndroidZero
Link to comment
Share on other sites

15 minutes ago, AndroidZero said:

reduce the ammount of globals

Reduce by eliminating them all you mean? Use Globals where you need to use Globals, and don't use them if they're not actually used as global variables. In this case, the way it's written, you have to use globals. There's a reason why Global variables exist in every language, they're necessary.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewManNH okey you are right .. I just hoped for some magic trick
Well I read about functional programming time ago and how fancy it is without using globals just create Functions with 1 simple job and nest them inside each other to get your desired result like:

*PSEUDOCODE*

call OpenDoor(Move(LoadHand(LoadAvatar()),90°,45°,Down))

Thanks for all of your replies I will keep juggeling with my globals 🤹‍♂️

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...