return multiple vars from an #include script ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
James9491
Posts: 34
Joined: 18 Feb 2016, 12:10

return multiple vars from an #include script ?

Post by James9491 » 29 Jun 2022, 09:01

From a function that runs in a separate script (as #include)
I should return about 6 VAR values to the main script.

How do I do that?

James

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: return multiple vars from an #include script ?

Post by mikeyww » 29 Jun 2022, 09:07

It requires no special handling because the included code acts as if it existed in the main script itself. Thus, the scripts do not behave as separate scripts, but as one.

#Include:
Causes the script to behave as though the specified file's contents are present at this exact position.
How to confirm: run your script.

James9491
Posts: 34
Joined: 18 Feb 2016, 12:10

Doesn't work..

Post by James9491 » 29 Jun 2022, 12:37

This is (part of) the main script.
It calls a routine : get_yValues()
That routine changes thouse testVar[1-6]
but in the main script, these changes doesn't show.

The Main script:

Code: Select all

Yellow_chk:
testVar1:=999
testVar2:=888
testVar3:=777
testVar4:=666
testVar5:=555
testVar6:=444
get_yValues()

msgbox, 0,, Results from the the function: %testVar1%`, %testVar2%`, %testVar3%`, %testVar4%`, %testVar5%`, %testVar6%, 90 
return
The included script (with the get_yValues() function)

Code: Select all

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
get_yValues()
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
{
xyFound:=-1
while xyFound<1
	{
;	send, !+W	;Call the yWatcher window
	soundPlay, %A_WorkingDir%\Sounds\cycle.wav, 1
	testVar1:=1.11
	testVar2:=2.12
	testVar3:=3.13
	testVar4:=4.14
	testVar5:=5.15
	testVar6:=6.16

	xyFound:=1
	return (testVar1, testVar2, testVar3, testVar4, testVar5, testVar6) 
	}

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: return multiple vars from an #include script ?

Post by boiler » 29 Jun 2022, 12:41

Without checking anything else about your script. You have a variable scope problem. The function doesn’t see the variables outside the function, and the code outside the function can’t see the ones inside it. The quickest fix is to declare them as global inside your function or super-global (can be seen by any function) by declaring them as global outside your function.

James9491
Posts: 34
Joined: 18 Feb 2016, 12:10

Re: 'variable scope' ??!

Post by James9491 » 30 Jun 2022, 18:03

I started reading about 'variable scope' and it's quite a lot to read..
I do not have that much time for it and I am looking for a quick fix. Is there any definition I should apply for the system to detect variables that should be visible in both scripts?

I learned to use ByRef and it works for me.
I'm wondering whether if there's any limitation using this ByRef definition?!

maybe it's affecting speed or has some other downside that effect the scripts?

I'd be happy to get more info about the issue.

Thanks

James

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: 'variable scope' ??!

Post by boiler » 30 Jun 2022, 19:41

James9491 wrote: I started reading about 'variable scope' and it's quite a lot to read..
I do not have that much time for it and I am looking for a quick fix. Is there any definition I should apply for the system to detect variables that should be visible in both scripts?
Hmm...maybe you found my four-sentence post to be quite a lot to read as well and didn't make it to this part of it:
boiler wrote: The quickest fix is to declare them as global inside your function or super-global (can be seen by any function) by declaring them as global outside your function.

James9491 wrote: I learned to use ByRef and it works for me.
I'm wondering whether if there's any limitation using this ByRef definition?!
No real limitations. To help you learn more, I would just be pointing you to the documentation. It would require some more reading, and since you say you don't have that much time for it, I won't take my own time to cite the locations in the documentation.

James9491
Posts: 34
Joined: 18 Feb 2016, 12:10

Dear Sir, I apologize.

Post by James9491 » 01 Jul 2022, 19:09

By saying "I do not have time to read" I did not meant to the four lines in your previous answer, but to the 232 lines explaining the value 'variable scope'.

I am sorry that you got offended from me.
I didn't meant to disrespect you nor your explanations.

I apologize to you.

In spite of my busy schedule, I found the time and read about the easy solution that you mentioned in your answer.

I still don't fully understand the other options that are available to handle the issue without the need for global variables, but for now, 'global' and 'ByRef' are enough for me at this point.

You helped me a lot,

Thank you,

James

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: return multiple vars from an #include script ?

Post by boiler » 01 Jul 2022, 19:49

I appreciate your comments. My view is that the help given in this forum should be seen as a supplement to the documentation, not a replacement for it. If someone says they've read the documentation but didn't understand something about it or couldn't apply it correctly, then I'll try to help. If someone says "I don't have time to read it so please take your time to tell me what it says," I won't do that.

Post Reply

Return to “Ask for Help (v1)”