Access Global Variable Outside Function

Hi

I am trying to acces a variable outside a function

 the code simply reads a database

how can i access the variable “email” outside the function at the line of code.

the last line should basically read “bbb@hotmail.com” in the console

so print (data)

would be “bbb@hotmail.com” in console

Hi @akhtarx5. Just a reminder, if you want the community to help with your questions, code must be properly formatted. There is a blue <> button in the row with Bold, Italics, etc. that if you click on it, then you will get a popup window that you can paste your code into and it will be formatted so people can understand what you’re writing better.

Next, there are very few reasons to use global variables. You should spend time trying to understand Scope.  Please review this tutorial:

https://docs.coronalabs.com/tutorial/basics/scope/index.html

You should be defining “email” as a local variable at the top of your module:

local email

Rob

then in your function, make sure you’re not redefining it local to that function (which you are not), then it will be set when the function is finished at the bottom of your code.

just want to point out that the reason Rob says not to assign the variable local further down in a function is that whenever you call a variable (be it value or function), code will look from point of call and upwards in the tab hierarchy, which i find kinda useful.

For example:

local function callback(event) print(event.response) end network.request(url2,"GET",callback) local function callback(event) print(event.response) end network.request(url1,"GET",callback)

here i will get 2 different replies, as intended, even though i use the same function name twice.

recently i’ve been working with a lot of network calls, and it helps me keep things more organized, if i do it like that.

all my return functions are named callback, and exist above the requesting function itself.

dont know if this makes much sense, or if it is how it is intended to work in lua, but it is how it works :slight_smile:

I have never used a global variable yet.

network.request is an async call, you probably just print the value of data before network.request had a chance to call your networkListener.

Ahhhhhhh, thanks so much guys.

i was accessing the variable before giving the data a chance to be recieved

Hi @akhtarx5. Just a reminder, if you want the community to help with your questions, code must be properly formatted. There is a blue <> button in the row with Bold, Italics, etc. that if you click on it, then you will get a popup window that you can paste your code into and it will be formatted so people can understand what you’re writing better.

Next, there are very few reasons to use global variables. You should spend time trying to understand Scope.  Please review this tutorial:

https://docs.coronalabs.com/tutorial/basics/scope/index.html

You should be defining “email” as a local variable at the top of your module:

local email

Rob

then in your function, make sure you’re not redefining it local to that function (which you are not), then it will be set when the function is finished at the bottom of your code.

just want to point out that the reason Rob says not to assign the variable local further down in a function is that whenever you call a variable (be it value or function), code will look from point of call and upwards in the tab hierarchy, which i find kinda useful.

For example:

local function callback(event) print(event.response) end network.request(url2,"GET",callback) local function callback(event) print(event.response) end network.request(url1,"GET",callback)

here i will get 2 different replies, as intended, even though i use the same function name twice.

recently i’ve been working with a lot of network calls, and it helps me keep things more organized, if i do it like that.

all my return functions are named callback, and exist above the requesting function itself.

dont know if this makes much sense, or if it is how it is intended to work in lua, but it is how it works :slight_smile:

I have never used a global variable yet.

network.request is an async call, you probably just print the value of data before network.request had a chance to call your networkListener.

Ahhhhhhh, thanks so much guys.

i was accessing the variable before giving the data a chance to be recieved