global variable change in substack not available to main stack

Ian McKnight iangmcknight at gmail.com
Sun Jul 10 04:03:29 EDT 2011


Hi Slava

I understand what you mean about portability - I've been caught out myself
many times reusing a handler and forgetting to copy the global declarations
at the top of the script but you may find a post by Richard Gaskin to this
list a few days ago useful for future projects. I paste his post here for
convenience (the subject of the thread is -
"defining and using globals in an application"

>-- snip
As Jacque pointed out, LC and related dialects are very forgiving with the
scope of variable names, so it's possible to have one token used as a
global, a local, and an argument.  To avoid confusing the compiler, we need
to declare globals in any script that uses them.

If that one line of code at the top of a script is cumbersome (and in some
complex code bases it may well be), try accessors instead:

-- In a mainstack, library, backscript, or other globally-available
-- script:
global gDefaultWidgetHeight

command SetDefaultWidgetHeight pVal
 put pVal into gDefaultWidgetHeight
end SetDefaultWidgetHeight

function GetDefaultWidgetHeight
  return gDefaultWidgetHeight
end GetDefaultWidgetHeight
---

With those in place you can get and set the value stored in the global from
any other script without a declaration, e.g.:

 set the height of grp 1 to GetDefaultWidgetHeight()
 SetDefaultWidgetHeight 200

Another benefit to using accessors is that they help factor code; that is,
you can change how you store that value at any time without having to modify
any code that relies on it.  Today it might be a global, but two versions
from now it might be a database record; with accessors, you only change two
handlers and everything else comes along for the ride.

True, using accessors will impair performance relative to calling the global
directly.  But in practical terms you're only looking at a fraction of a
*micro*second, so if you need to liberate your code base from direct storage
references chances are no one will ever notice the performance difference.

>--snip

On 10 July 2011 08:39, Slava Paperno <slava at lexiconbridge.com> wrote:

> Thanks Stephen and Ian. It is true that I declare each global variables
> inside every handler where it is used. I have a sizable application now
> where this strategy is employed everywhere. In doing so I followed this
> statement in the User Guide to 4.6.2:
>
> 5.5.4 Global Variables (page 143, below the pink Important box)
> "You can use the global command either inside a handler, or outside any
> handler at the top of a script (like a script local)."
>
> In choosing between these two recommendations I decided that putting the
> global declaration inside the handler makes the handler more portable.
>
> But I'll follow your advice and mess with this some more after a good
> night's sleep.
>
> Gratefully,
>
> Slava
>
> > -----Original Message-----
> > From: use-livecode-bounces at lists.runrev.com [mailto:use-livecode-
> > bounces at lists.runrev.com] On Behalf Of Ian McKnight
> > Sent: Sunday, July 10, 2011 2:23 AM
> > To: How to use LiveCode
> > Subject: Re: global variable change in substack not available to main
> > stack
> >
> > Hi
> >
> > I just recreated you scripts in a stack.
> >
> > Stephen Barncard is correct the more usual place to put the global
> > statement
> > is outside the handlers at the top of the script.
> >
> > However. Your original card script is incomplete since the 'put'
> > statement
> > needs to be put into a handler.
> >
> > eg
> > global gBgColor
> >
> > on openstack -- or preOpenStack or whatever
> >    put 0, 0, 0 into gBgColor
> > end openstack
> >
> > When I made this change the gBgColor reflects changes made in the
> > mousedown
> > script.
> >
> >
> > BTW I know that it works the way you have it but when using colour info
> > like
> > 0,0,0 I would recommend putting it into quotes "0,0,0"
> >
> > HTH
> >
> > On 10 July 2011 07:02, Slava Paperno <slava at lexiconbridge.com> wrote:
> >
> > > In openCard script of the main stack:
> > >
> > > global gBgColor
> > > put 0, 0, 0 into gBgColor
> > >
> > >
> > > In a button script in a substack:
> > >
> > > on mouseUp
> > >        global gBgColor
> > >
> > >        answer color with gBgColor --white is selected
> > >        put it into gBgColor
> > >
> > >        put gBgColor & cr after msg
> > >        --255, 255, 255
> > > end mouseUp
> > >
> > > In the Variables panel below script editor (after it is refreshed),
> > > gBgColor
> > > is shown to be 0, 0, 0.
> > >
> > > In the Message box:
> > >
> > > put gBgColor
> > > --0, 0, 0
> > >
> > >
> > > In other words, the change to a global variable that is made in a
> > substack
> > > is not available to the global variable of the same name in a script
> > in the
> > > main stack.
> > >
> > > Just last week I carefully followed the discussion of the use of
> > global
> > > variables (and even participated a little by quoting the User Guide
> > on the
> > > availability of globals to all substacks), but now I see the same
> > problem
> > > that was reported by the original poster.
> > >
> > > Is it just me now? Or is it the late hour?
> > >
> > > Slava
> > >
> > >
> > >
> > > _______________________________________________
> > > 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
> > >
> >
> >
> >
> > --
> > Regards
> >
> >
> > Ian McKnight
> >
> > iangmcknight at gmail.com
> > =======================
> > _______________________________________________
> > 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
>



-- 
Regards


Ian McKnight

iangmcknight at gmail.com
=======================



More information about the use-livecode mailing list