How Global is global?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

How Global is global?

Post by bjb007 » Fri Feb 15, 2008 1:05 pm

Trying to set up a variable which will be available
to a number of button handlers but no matter where
I put "global theString" it isn't available when I want
it to be.

All suggestions considered.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Post by Klaus » Fri Feb 15, 2008 3:20 pm

You have to declare/initialize that variable the first time you use/fill it.

e.g.

Code: Select all

on openstack
  global gVar1
  put 33 into gVar1
  ...
end openstack
And then you have to declare it everytime you use it like in a button:

Code: Select all

on mouseup
  global gVar1
  add 66 to gVar1
  ## or whatever...
  answer gVar1
end mouseup
Best

Klaus

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1236
Joined: Sat Apr 08, 2006 1:10 pm
Location: Zurich
Contact:

Post by BvG » Fri Feb 15, 2008 5:28 pm

Globals are globals inasmuch as they can be used in every part of a rev application. But to use them, you have to declare them in the respective context. If a variable is declared within a handler, like Klaus explained, it's only available as such in that specific handler. But you can also declare variables outside of handlers, making them available to every handler of the same object. So this would be a valid and working script:

Code: Select all

global someVarName

on mouseUp
  put someVarName
end mouseUp

on mousedown
  add one to someVarName
end mousedown
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”