How to use a global or static variable in a function?

Advanced Macro Recorder/Editor.

Moderator: Pulover

mons3fa
Posts: 16
Joined: 20 May 2017, 19:33

How to use a global or static variable in a function?

Post by mons3fa » 26 Oct 2020, 22:31

I am trying to evaluate DailyMax and DailyMin which are set outside of the function, but it seems that the variables are reset on every call of the function how can I get the function to evaluate the global variable and not the local variable?

Code: Select all

; Start
evaluateTrendQRS()
{
	global DailyMax, DailyMin
	If (DailyMax == "0" AND DailyMin == "0")
	{
		MsgBox, 262148, Trend Strip, Is this a Maximum Daily?
		IfMsgBox, Yes
		{
			DailyMax := 1
			clickHRField()
			CurrentKeyDelay := A_KeyDelay
			SetKeyDelay, 1
			SendEvent, DAILY MAXIMUM AVERAGE HR - %getHR% bpm
			SetKeyDelay, %CurrentKeyDelay%
			Sleep, 100
			HRValidation()
			ControlClick, Button14, QRS Measurements ahk_class #32770,, Left, 1,  x28 y8 NA  ; Close QRS Window
			Sleep, 10
		}
	}
	Else IfMsgBox, No
	{
		MsgBox, 262148, Trend Strip, Is this a Minimum Daily?
		IfMsgBox, Yes
		{
			DailyMin := 1
			clickHRField()
			CurrentKeyDelay := A_KeyDelay
			SetKeyDelay, 1
			SendEvent, DAILY MINIMUM AVERAGE HR - %getHR% bpm
			SetKeyDelay, %CurrentKeyDelay%
			Sleep, 100
			HRValidation()
			ControlClick, Button14, QRS Measurements ahk_class #32770,, Left, 1,  x28 y8 NA  ; Close QRS Window
			Sleep, 10
		}
		Else IfMsgBox, No
		{
			Sleep, 100
			CurrentKeyDelay := A_KeyDelay
			SetKeyDelay, 2
			SendEvent, AVERAGE HR - %getHR% bpm
			SetKeyDelay, %CurrentKeyDelay%
			HRValidation()
			ControlClick, Button14, QRS Measurements ahk_class #32770,, Left, 1,  x28 y8 NA  ; Close QRS Window
			Sleep, 10
		}
	}
	Else If (DailyMax == "1" AND DailyMin == "0")
	{
		MsgBox, 262148, Trend Strip, Is this a Minimum Daily?
		IfMsgBox, Yes
		{
			DailyMin := 1
			clickHRField()
			CurrentKeyDelay := A_KeyDelay
			SetKeyDelay, 1
			SendEvent, DAILY MINIMUM AVERAGE HR - %getHR% bpm
			SetKeyDelay, %CurrentKeyDelay%
			Sleep, 100
			HRValidation()
			ControlClick, Button14, QRS Measurements ahk_class #32770,, Left, 1,  x28 y8 NA  ; Close QRS Window
			Sleep, 10
		}
	}
	Else If (DailyMin == "1" AND DailyMax =="0")
	{
		MsgBox, 262148, Trend Strip, Is this a Maximum Daily?
		IfMsgBox, Yes
		{
			DailyMax := 1
			clickHRField()
			CurrentKeyDelay := A_KeyDelay
			SetKeyDelay, 1
			SendEvent, DAILY MAXIMUM AVERAGE HR - %getHR% bpm
			SetKeyDelay, %CurrentKeyDelay%
			Sleep, 100
			HRValidation()
			ControlClick, Button14, QRS Measurements ahk_class #32770,, Left, 1,  x28 y8 NA  ; Close QRS Window
			Sleep, 10
		}
	}
	Else
	{
		Sleep, 100
		CurrentKeyDelay := A_KeyDelay
		SetKeyDelay, 2
		SendEvent, AVERAGE HR - %getHR% bpm
		SetKeyDelay, %CurrentKeyDelay%
		HRValidation()
		ControlClick, Button14, QRS Measurements ahk_class #32770,, Left, 1,  x28 y8 NA  ; Close QRS Window
		Sleep, 10
	}
}

User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: How to use a global or static variable in a function?

Post by Pulover » 27 Oct 2020, 07:08

I've tested global variables and they seem to be working. Your code only shows the function and you do set the variables inside them, so there must be some problem with the logic. I've spotted one at least: You have an Else IfMsgBox, No after a normal If. You probably meant to put it inside the if, after the IfMsgBox, Yes.

Here's my test code:

Code: Select all

[PMC Code v5.3.0]|F4||1|Window,2,Fast,0,1,Input,-1,-1,1|1|AskVars
Context=None||None|
Groups=Start:1
1|InputBox|DailyMax, DailyMax, Set DailyMax|1|0|InputBox|||||1|
2|InputBox|DailyMin, DailyMin, Set DailyMin|1|0|InputBox|||||2|
3|MyFunc1|_null := |1|0|Function|||||3|

[PMC Code v5.3.0]|F5||1|Window,2,Fast,0,1,Input,-1,-1,1|1|SameVars
Context=None||None|
Groups=Start:1
1|MyFunc1|_null := |1|0|Function|||||3|

[PMC Code v5.3.0]|||1|Window,2,Fast,0,1,Input,-1,-1,1|1|MyFunc1()
Context=None||None|
Groups=Start:1
1|[FunctionStart]|MyFunc1|1|0|UserFunction|Local|DailyMax, DailyMin / |||1|
2|Evaluate Expression|DailyMax == "0" AND DailyMin == "0"|1|0|If_Statement|Local||||4|
3|[MsgBox]|Question|1|0|MsgBox|32||||6|
4|[ElseIf] Evaluate Expression|DailyMax == "1" AND DailyMin == "0"|1|0|If_Statement|32||||7|
5|[MsgBox]|DailyMax|1|0|MsgBox|0||||10|
6|[ElseIf] Evaluate Expression|DailyMin == "1" AND DailyMax =="0"|1|0|If_Statement|0||||11|
7|[MsgBox]|DailyMin|1|0|MsgBox|0||||14|
8|[Else]|Else|1|0|If_Statement|||||15|
9|[MsgBox]|Something else...|1|0|MsgBox|0|||||
10|[End If]|EndIf|1|0|If_Statement|||||18|

Code: Select all

F4::
AskVars:
InputBox, DailyMax, DailyMax, Set DailyMax
InputBox, DailyMin, DailyMin, Set DailyMin
MyFunc1()
Return

F5::
SameVars:
MyFunc1()
Return

MyFunc1()
{
    global DailyMax, DailyMin
    If (DailyMax == "0" AND DailyMin == "0")
    {
        MsgBox, 32, , Question
    }
    Else If (DailyMax == "1" AND DailyMin == "0")
    {
        MsgBox, 0, , DailyMax
    }
    Else If (DailyMin == "1" AND DailyMax =="0")
    {
        MsgBox, 0, , DailyMin
    }
    Else
    {
        MsgBox, 0, , Something else...
    }
}
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)

Post Reply

Return to “Pulovers Macro Creator”