Page 1 of 1

return multiple vars from an #include script ?

Posted: 29 Jun 2022, 09:01
by James9491
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

Re: return multiple vars from an #include script ?

Posted: 29 Jun 2022, 09:07
by mikeyww
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.

Doesn't work..

Posted: 29 Jun 2022, 12:37
by James9491
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) 
	}

Re: return multiple vars from an #include script ?

Posted: 29 Jun 2022, 12:41
by boiler
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.

Re: 'variable scope' ??!

Posted: 30 Jun 2022, 18:03
by James9491
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

Re: 'variable scope' ??!

Posted: 30 Jun 2022, 19:41
by boiler
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.

Dear Sir, I apologize.

Posted: 01 Jul 2022, 19:09
by James9491
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

Re: return multiple vars from an #include script ?

Posted: 01 Jul 2022, 19:49
by boiler
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.