john_2019 Posted April 1, 2020 Share Posted April 1, 2020 How to pass a variable from one function to another? I tried different ways like this one: #include <MsgBoxConstants.au3> Func CopyToPseudoClipboard() Local $string = "string" EndFunc Func _Test($string) MsgBox($MB_SYSTEMMODAL, "title", $string) EndFunc HotKeySet("+{Enter}", "_Test") While 1 Sleep(100) WEnd Also tried global variables and Return. Neither works. Could somebody show me how to use it? Link to comment Share on other sites More sharing options...
Subz Posted April 1, 2020 Share Posted April 1, 2020 (edited) There are a number of ways to do this, it really depends if you wish to use the variable throughout all your functions for example: Global $g_sString = "" ;~ Set $g_sString value _SetVariable("Example1 String") ;~ Pass the global variable as a parameter _Example1($g_sString) ;~ $g_sString is referenced within the _Example2 function _Example2() Func _SetVariable($_sString) ;~ $_sString is local to this function ;~ $g_sString has already been decalared in the global scope $g_sString = $_sString EndFunc Func _Example1($_sString = "") MsgBox(4096, "Example 1", "$_sString = " & $_sString) EndFunc Func _Example2() _SetVariable("Example2 String") MsgBox(4096, "Example 2", "$g_sString = " & $g_sString) EndFunc Edit: Added a couple of different examples Edited April 1, 2020 by Subz john_2019 1 Link to comment Share on other sites More sharing options...
mikell Posted April 1, 2020 Share Posted April 1, 2020 BTW HotkeySet : The called function can not be given parameters. They will be ignored. (helpfile) john_2019 1 Link to comment Share on other sites More sharing options...
Nine Posted April 1, 2020 Share Posted April 1, 2020 I like how @Subz calls his variables : $g_sString Subz 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
john_2019 Posted April 1, 2020 Author Share Posted April 1, 2020 (edited) @Subz Your examples are really useful, I'm very appreciated. It seems both of them utilize global variable, right? As I know, global variables are generally discouraged (except special cases). Could you also show the way without globals? Edited April 1, 2020 by john_2019 Link to comment Share on other sites More sharing options...
Subz Posted April 1, 2020 Share Posted April 1, 2020 Global variables are fine, especially if you wish to use the variable in multiple functions. You can also use functions with parameters (see _Example1 above, but as mikell mentioned if you wish to use HotKeySet, you would need to use a global variable or call a function within your HotKeySet function. Hope that made sense. john_2019 1 Link to comment Share on other sites More sharing options...
Nine Posted April 1, 2020 Share Posted April 1, 2020 Do not forget that there is Local Static $Variables. If you only need that var in a single function, but wants it to act like a global, think of it. I believe it is one of the underestimated and underused feature of AutoIt... Inpho and john_2019 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Sidley Posted April 1, 2020 Share Posted April 1, 2020 You should also check the ByRef keyword in the help file. Link to comment Share on other sites More sharing options...
jchd Posted April 1, 2020 Share Posted April 1, 2020 Byref has nothing to do with that. I second what @Nine said about Static variables. A function can easily be used both explicitely, on timeout and by hotkey and behave differently in all cases. HotKeySet("{F2}", fct) HotKeySet("{ESC}", stop) AdlibRegister(rst, 14000) ; reset static variable every 14s Func fct($p) Local Static $v = 26 If IsDeclared("p") Then If $p < 0 Then ConsoleWrite("fct invoked on timeout; value = " & $v & " and will be reset to 0" & @LF) $v = 0 Else ConsoleWrite("fct invoked explicitely; value = " & $v & " and will increment by " & $p & @LF) $v += $p EndIf Else ConsoleWrite("fct invoked thru hotkey; value = " & $v & " and will increment by " & 100 & @LF) $v += 100 EndIf EndFunc Func stop() Exit EndFunc Func rst() fct(-1) EndFunc While 1 Sleep(1000) fct(Random(1, 9, 1)) WEnd If you need to pass any value in $p (meaning that you can't differentiate calling method by looking at $p), then declare a second variable in fct() and pass there a value for switching between wanted behaviors. john_2019 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
john_2019 Posted April 1, 2020 Author Share Posted April 1, 2020 (edited) This is what I'm trying to create: Global $g_PseudoClipboard = "" Func _CopyToPseudoClipboard() Local $OriginalClipboard = ClipGet() Send("^c") $g_PseudoClipboard = ClipGet() ClipPut($OriginalClipboard) EndFunc Func _ShowPseudoClipboard() _CopyToPseudoClipboard() MsgBox(4096, "", $g_PseudoClipboard) EndFunc HotKeySet("+{Enter}", "_ShowPseudoClipboard") While 1 Sleep(100) WEnd Launch it and then open Windows file manager (that is, Windows Explorer), and press Shift+Enter. The currently selected file/directory will be copied to the variable and then the corresponding message box will show it. What I don't understand is why it works in the following way: Works → Then doesn't work → Then works → Then doesn't work → And so on. ? Edited April 1, 2020 by john_2019 Link to comment Share on other sites More sharing options...
mikell Posted April 1, 2020 Share Posted April 1, 2020 (edited) You might avoid a global var by using Return And also include some Sleep() so your system can breathe a little HotKeySet("+{Enter}", "_ShowPseudoClipboard") While 1 Sleep(100) WEnd Func _ShowPseudoClipboard() $PseudoClipboard = _CopyToPseudoClipboard() ;MsgBox(4096, "", $PseudoClipboard) consolewrite("result = " & $PseudoClipboard & @crlf & @crlf) EndFunc Func _CopyToPseudoClipboard() $OriginalClipboard = ClipGet() Sleep(10) Send("^c") Sleep(10) Local $NewClipboard = ClipGet() ; check it consolewrite("$NewClipboard = " & $NewClipboard & @crlf) consolewrite("$OriginalClipboard = " & $OriginalClipboard & @crlf) ClipPut($OriginalClipboard) Return $NewClipboard EndFunc Edited April 1, 2020 by mikell john_2019 1 Link to comment Share on other sites More sharing options...
john_2019 Posted April 1, 2020 Author Share Posted April 1, 2020 @mikell Works like a charm @jchd Thanks, I will save this thread with all the useful posts in my bookmarks. Link to comment Share on other sites More sharing options...
jchd Posted April 1, 2020 Share Posted April 1, 2020 This is one of the rare uses of IsDeclared(). This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now