Sign in to follow this  
Followers 0
avery

[SOLVED] Var scope

5 posts in this topic

#1 ·  Posted (edited)

I downloaded my first UDF! Yay me...

I put the UDF in its own file and had to remove a bunch of '\' that gave me warnings/errors on 'Go'.

It worked!! My background changed to the one I made, woooot!

I've now moved the function call to the UDF cut-n-paste job I did into my .au3 file.

For some reason it doesn't work now ;)

Please help!

; Desktop wallpaper changer vars
Global $pic = "DoD-SECRET.bmp"; (S) Pic
Global $style = 0; 0=center, 1=stretched, 2=tiled
Global $warn = 1; warnings 0=off, 1=on
; I also tried Const here as well as Global Const (if that is even possible).

...Click a button that calls -> create_dod_inf()

Func create_dod_inf()
    ....my stuff here....
    _setwallpaper($pic, $style, $warn)
EndFunc

Func _setwallpaper($pic, $style, $warn)
    ....UDF stuff here....
EndFunc
Edited by avery

www.abox.orgAvery HowellVisit My AutoIt Websitehttp://www.abox.org

Share this post


Link to post
Share on other sites



it doesn't work now

Is this the exact error message you get?

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

Is this the exact error message you get?

Lar.

I get no error at all and nothing changes. I have $warn = 1 on the UDF but I see no difference when toggled. It all worked fine until I put it in my script. Any chance I can screw up the scope of the variables? Can you tell I have no idea what I'm talking about yet?


www.abox.orgAvery HowellVisit My AutoIt Websitehttp://www.abox.org

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Your problem is reusing the variable names of the Globals as input variables to the function _setwallpaper(). Input variables to a function are automatically declared local to that function, and therefore do not contain the data passed from the Globals.

NVM, calling _setwallpaper() from within create_dod_inf() does in fact pass the Global values into the local variables. Note however that changes made inside that function will only happen to the Locals, not the Globals.

;)

Global $pic = "DoD-SECRET.bmp"
Global $style = 0
Global $warn = 1

create_dod_inf()

Func create_dod_inf()
    _setwallpaper($pic, $style, $warn)
EndFunc

Func _setwallpaper($pic, $style, $warn)
    MsgBox(64, "Inputs", "$pic = " & $pic & @CRLF & _
            "$style = " & $style & @CRLF & _
            "$warn = " & $warn)
EndFunc
Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Your problem is reusing the variable names of the Globals as input variables to the function _setwallpaper(). Input variables to a function are automatically declared local to that function, and therefore do not contain the data passed from the Globals.

NVM, calling _setwallpaper() from within create_dod_inf() does in fact pass the Global values into the local variables. Note however that changes made inside that function will only happen to the Locals, not the Globals.

;)

Global $pic = "DoD-SECRET.bmp"
Global $style = 0
Global $warn = 1

create_dod_inf()

Func create_dod_inf()
    _setwallpaper($pic, $style, $warn)
EndFunc

Func _setwallpaper($pic, $style, $warn)
    MsgBox(64, "Inputs", "$pic = " & $pic & @CRLF & _
            "$style = " & $style & @CRLF & _
            "$warn = " & $warn)
EndFunc

Interesting, thanks. It worked now.

And I updated my version of au3 (oops)


www.abox.orgAvery HowellVisit My AutoIt Websitehttp://www.abox.org

Share this post


Link to post
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
Sign in to follow this  
Followers 0