Page 1 of 1

Global variable, what am I doing wrong?

Posted: Thu Jul 11, 2013 7:22 pm
by trenatos
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

Re: Global variable, what am I doing wrong?

Posted: Thu Jul 11, 2013 7:51 pm
by Newbie4
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.

Re: Global variable, what am I doing wrong?

Posted: Thu Jul 11, 2013 8:07 pm
by trenatos
Thanks Cyril, I owe you more beer ;)

Re: Global variable, what am I doing wrong?

Posted: Fri Jul 12, 2013 8:02 am
by makeshyft
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.

Re: Global variable, what am I doing wrong?

Posted: Fri Jul 12, 2013 8:56 am
by Dixie
Hi Tom...

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