global variable

Hi

How to create global variable in corona sdk?.I want create global variable and initialize value in that variable
and i want access that variable in other function or other screen. [import]uid: 209400 topic_id: 34767 reply_id: 334767[/import]

There are 2 ways.

  1. Don’t label it as local (see line 1)
  2. Prefix the variable name with _G to add it to the global table (see line 2)

while both 1 & 2 act exactly the same in terms of function, 2 is a lot cleaner and better practice (and some say offer better performance?), as when you read over your code you can tell you intend it to be global and its not a local you made global in error.

[lua]a = 5 – global variable
_G.b = 10 – global variable
local c = 15 – local variable[/lua] [import]uid: 62706 topic_id: 34767 reply_id: 138145[/import]

Thanks for your response.How to read that variable in other function or other screen ?.I am using like

assign dynamic value here
_G.str1 = (string.sub(data1, 0,str-3))
in different function in am reading that variable.
print(_G.str1)
or (str1)

its showing nil value. [import]uid: 209400 topic_id: 34767 reply_id: 138148[/import]

Only use _G when declaring the variable.

\_G.str1 = "cool" print(str1) -- "cool"

Keep in mind that you can always override a global variable with scope

local str1 = "not cool" -- Lua looks at the local first!!

It also depends on where you put the global. Generally you want to declare _G. in main.lua so that it’s there from the start. If you define it elsewhere you may have code looking for it that executes before the global is made. [import]uid: 41884 topic_id: 34767 reply_id: 138149[/import]

Hi

I am doing like this.I have declare
in main.lua
_G.str1=""
in next screen i am using like this for assign value
local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
else
local data1 = (event.response)
local str=(string.find(data1, "<?xml "))
_G.str1 = (string.sub(data1, 0,str-3))
end
end

and in function i am getting value but showing null

function scene:createScene( event )
local group = self.view
print(“aaaa” … str1)
end
[import]uid: 209400 topic_id: 34767 reply_id: 138158[/import]

Well it depends on when you poll the value. At createScene() str1 should probably still be empty. You’d need to check sometime *after* doing the networkListen, so probably at least enterScene(). But it depends how often your networkListener is active. [import]uid: 41884 topic_id: 34767 reply_id: 138165[/import]

Thanks its working fine. [import]uid: 209400 topic_id: 34767 reply_id: 138168[/import]

There are 2 ways.

  1. Don’t label it as local (see line 1)
  2. Prefix the variable name with _G to add it to the global table (see line 2)

while both 1 & 2 act exactly the same in terms of function, 2 is a lot cleaner and better practice (and some say offer better performance?), as when you read over your code you can tell you intend it to be global and its not a local you made global in error.

[lua]a = 5 – global variable
_G.b = 10 – global variable
local c = 15 – local variable[/lua] [import]uid: 62706 topic_id: 34767 reply_id: 138145[/import]

Thanks for your response.How to read that variable in other function or other screen ?.I am using like

assign dynamic value here
_G.str1 = (string.sub(data1, 0,str-3))
in different function in am reading that variable.
print(_G.str1)
or (str1)

its showing nil value. [import]uid: 209400 topic_id: 34767 reply_id: 138148[/import]

Only use _G when declaring the variable.

\_G.str1 = "cool" print(str1) -- "cool"

Keep in mind that you can always override a global variable with scope

local str1 = "not cool" -- Lua looks at the local first!!

It also depends on where you put the global. Generally you want to declare _G. in main.lua so that it’s there from the start. If you define it elsewhere you may have code looking for it that executes before the global is made. [import]uid: 41884 topic_id: 34767 reply_id: 138149[/import]

Hi

I am doing like this.I have declare
in main.lua
_G.str1=""
in next screen i am using like this for assign value
local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
else
local data1 = (event.response)
local str=(string.find(data1, "<?xml "))
_G.str1 = (string.sub(data1, 0,str-3))
end
end

and in function i am getting value but showing null

function scene:createScene( event )
local group = self.view
print(“aaaa” … str1)
end
[import]uid: 209400 topic_id: 34767 reply_id: 138158[/import]

Well it depends on when you poll the value. At createScene() str1 should probably still be empty. You’d need to check sometime *after* doing the networkListen, so probably at least enterScene(). But it depends how often your networkListener is active. [import]uid: 41884 topic_id: 34767 reply_id: 138165[/import]

Thanks its working fine. [import]uid: 209400 topic_id: 34767 reply_id: 138168[/import]