How to detect a change in a global variable ?

Martin Koob mkoob at rogers.com
Tue Jul 29 08:49:56 EDT 2025


Hi Tom,

That is even easier  way to make a variable listener  for LiveCode!

Thanks for that recipie.

I do have a question about this.   I would think that ‘wait until [condition is true]’ must have a loop under the hood although probably much faster that running a loop written in LiveCode script would. Is that true?  I am curious as to what kind of performance hit from that. (Probably nothing we have to worry about for a single variable listener like your example. If you were monitoring many variables could it have a noticiable effect, or I am still just stuck in the 90s.

 💾👴🏻📠

Best regards,

Martin Koob

> On Jul 26, 2025, at 8:59 PM, Tom Glod via use-livecode <use-livecode at lists.runrev.com> wrote:
> 
> When I need to detect a change in another variable I use "wait until
> variable_name is not "x" with messages" ... obviously your condition has to
> match what you are trying to detect.
> no loop needed
> 
> 
> 
> On Sat, Jul 26, 2025 at 8:14 AM Martin Koob via use-livecode <
> use-livecode at lists.runrev.com> wrote:
> 
>> 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
>> 
>> _______________________________________________
>> 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