How to detect a change in a global variable ?

Martin Koob mkoob at rogers.com
Sat Jul 26 08:12:25 EDT 2025


Hi

Another way to do this is to use a setProp handler to set the global variable and create in essence a listener for changes to the global variable.  In the setProp handler you set the global but then you can include other commands to be issued or sent or dispatched when the global variable is set,  Of course you have to make sure that you only this setProp handler to set the value of global gMyVar variable. 

To see how this would work 

1. Create a new stack with 1 button and 1 field.

2. Name the Name the button “Easy” and Name the field “Global VariableDisplay”.

3. Add the following script to the field “Global VariableDisplay”.
on displayMyVar pMyStatus

	set the text of me to pMyStatus

end displayMyVar

4. Add the following script to the button.
on mouseUp

	global gMyVar

	if gMyVar is "easy" then

		set the cMyGlobalVar of this card to ""

	else

		set the cMyGlobalVar of this card to "Easy"

	end if

end mouseUp

5. Add the following script to Card 1.

setProp cMyGlobalVar pNewVar

	global gMyVar

	put pNewVar into gMyVar

	## If you don't need to include a parameter with the message use 'send'

	-- send "displayMyVar" to field "GlobalVariable Display"

	## If you need to include a parameter with the message use 'dispatch'

	dispatch "displayMyVar" to field "GlobalVariable Display" of me with gMyVar

	end cMyGlobalVar



	## you can also access the property using a getProp handler but

	## that does not make much sense since since gMyVar is a global which you can use directly.

	--getProp cMyGlobalVar

		-- global gMyVar

		-- return gMyVar

	--end cMyGlobalVar

This would avoid any blocking or load on the machine using the loop.	


Best regards,

Martin Koob

> On Jul 26, 2025, at 5:16 AM, scott--- via use-livecode <use-livecode at lists.runrev.com> wrote:
> 
> Oops… just for thoroughness:  In the last handler I forgot to add the line “put empty into sFlag” after the repeat loop
> 
> --
> Scott Morrow
> 
> Elementary Software
> (Now with 20% less chalk dust!)
> web       https://elementarysoftware.com/
> email     scott at elementarysoftware.com
> booth    1-360-734-4701
> ------------------------------------------------------
> 
> 
>> On Jul 26, 2025, at 1:59 AM, scott--- via use-livecode <use-livecode at lists.runrev.com> wrote:
>> 
>> The way that the loop is written would make it blocking. If you wanted the loop to start running and then stop after the mouseUp happens I think you would need to add a "wait x millisec with messages” 
>> 
>> Fo example:
>> 
>> global myVar
>> 
>> on elsoTester
>>   put 0 into myVar
>>   -- some code
>>   repeat until myVar > 0
>>        --  set the thumbPosition of scrollBar "ProgressBar" to (the thumbPosition of scrollBar "ProgressBar" + 5)  --   testing
>>       wait 500 millisec with messages
>>   end repeat
>>   -- some code
>>   answer "Finished"
>> end elsoTester
>> 
>> 
>> Depending on what you are doing, another method would be to use a "send it time" mechanism where the handler keeps calling itself until myVar > 0 
>> For example:
>> 
>> 
>> local sFlag
>> global myVar
>> 
>> command elsoTester
>>   if sFlag is empty then
>>       put 0 into myVar
>>      -- some code
>>   end if
>> 
>>   repeat until myVar > 0
>>         --  set the thumbPosition of scrollBar "ProgressBar" to (the thumbPosition of scrollBar "ProgressBar" + 5)   --   testing
>>        put “waiting” into sFlag
>>        send elsoTester to me in 500 millisec
>>        exit elsoTester
>>   end repeat
>> 
>>   answer “Finished"
>> end elsoTester
>> 
>> To be thorough  you might also might want to empty the message queue of any other elsoTester messages before sending in time or add a timer which will eventually time out if myVar doesn’t exceed 0 after a set time.
>> 
>> --
>> Scott Morrow
>> 
>> Elementary Software
>> (Now with 20% less chalk dust!)
>> web       https://elementarysoftware.com/
>> email     scott at elementarysoftware.com
>> booth    1-360-734-4701
>> ------------------------------------------------------
>> 
>>> On Jul 26, 2025, at 12:44 AM, jbv via use-livecode <use-livecode at lists.runrev.com> wrote:
>>> 
>>> Hi list,
>>> 
>>> Let's say I have a portion of a script like this :
>>> 
>>> global myVar
>>> put 0 into myVar
>>> -- some code
>>> repeat until myVar > 0
>>> end repeat
>>> -- some code
>>> 
>>> and the content of global myVar being changed
>>> in a handler of another control :
>>> 
>>> on mouseUp
>>>  global myVar
>>>  put 1 into myVar
>>> end mouseUp
>>> 
>>> I know this is not very elegant, but my question is :
>>> can the change in myVar be detected in a loop like
>>> above ?
>>> I made some tests and it doesn't seem to work...
>>> 
>>> Thank you in advance.
>>> jbv
>>> 
>>> _______________________________________________
>>> use-livecode mailing list
>>> use-livecode at lists.runrev.com
>>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


More information about the use-livecode mailing list