Global variable, what am I doing wrong?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Global variable, what am I doing wrong?

Post by trenatos » Thu Jul 11, 2013 7:22 pm

When clicking a button, I create a global variable called connectedStatus and setting it to having the value "0", it's the status of a network connection.

There's a button that says log in, when you click on it, it should read the status of connectedStatus and either pop up the login window, or disconnect, depending on the connectedStatus.

But I can't get it to properly read the global variable, it *always* pops up the login stack.
I've done testing and it's setting the variable correctly, but the if/then doesn't work.

What am I doing wrong here?

This is the button:
on mouseUp
if connectedStatus = "1" then
close socket "127.0.0.1:2001"
put "Disconnected" into field "statusField"
put "0" into connectedStatus
set the title of button "loginButton" to "Log in"
else
set the visible of stack "loginStack" to true
end if
end mouseUp
Marcus

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 280
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Global variable, what am I doing wrong?

Post by Newbie4 » Thu Jul 11, 2013 7:51 pm

To access a global variable created elsewhere, you need to declare that variable as global.

I assume that somewhere you have the statement

Code: Select all

 global connectedStatus 
that declares "connectedStatus" as a global, before you put the status into it.

From there, anywhere else that you want to access that variable (as in your on mouseUp example), you have to declare that you want to use the global variable that you already created before. You need to add the statement

Code: Select all

 global connectedStatus 
also in your mouseUp handler or at the top of the script before that handler.

e.g.

Code: Select all

on mouseUp
  global connectedStatus
  if connectedStatus = "1" then
     close socket "127.0.0.1:2001"
     put "Disconnected" into field "statusField"
     put "0" into connectedStatus
     set the title of button "loginButton" to "Log in"
  else
     set the visible of stack "loginStack" to true
  end if
end mouseUp
If you put the "global connectedStatus" in the "on mouseUp" handler, then it is only good in that handler.
If you put the "global connectedStatus" at the top of that script page before the "on mouseUp" handler, then it is good for every handler on that script page.
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Global variable, what am I doing wrong?

Post by trenatos » Thu Jul 11, 2013 8:07 pm

Thanks Cyril, I owe you more beer ;)
Marcus

makeshyft
Posts: 187
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: Global variable, what am I doing wrong?

Post by makeshyft » Fri Jul 12, 2013 8:02 am

You might want to turn on the IDE feature thats going to require you to declare all variables explicitly before you use them anywhere. I have it on, it makes life easier in my own head to see a list of variables that I am using.

What I did to make declaring variables easier, is I created one main array. And in that array I made variables, with nice readable names, and clear and concise organizing of information. It makes it so much easier to keep track of variables in the debug window......

I read stuff about Global Variables not being good to use, but I don't really get it, I have done a lot using global variables, and I haven't run into a problem with using them yet. Creating an organized array with ,eaningful names works for me.

Like for example....

MyAppCentralArray["Internal Settings"]["FullscreenOnOff"]
MyAppCentralArray["User Data"][1]["User Name"]
etc

and you always only have to declare the first variable.

global MyAppCentralArray

When performing calculations and temporary functions, use local variables, they are erased once the value has been calculated.
Tom @
http://www.makeshyft.com
---------------------------------
Changing the Future, Today!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Global variable, what am I doing wrong?

Post by Dixie » Fri Jul 12, 2013 8:56 am

Hi Tom...

Instead of using globals, I have got into the habit of using custom properties instead... but there again, 'one mans meat...'

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”