Where To Declare & Initialize Global Variables?

Hi,

I’ve started learning Corona SDK today.

I have no experience with this SDK or Lua.

I have a very simple demo which is not working as intended:

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local TextSize = 0 local TextSizeDir = 1 if TextSizeDir == 1 then if TextSize \<= 50 then TextSize = TextSize + 1 else TextSizeDir = 0 end elseif TextSizeDir == 0 then if TextSize \>= 0 then TextSize = TextSize - 1 else TextSizeDir = 1 end end local displayText = display.newText( "Hello World", display.contentCenterX, display.contentCenterY, "Font01.ttf", TextSize )

The above code *should* animate the “Hello World” text in the center of the screen (larger, smaller & repeat).

It does not do anything currently because at beginning of each main.lua loop the following is reset:

local TextSize = 0 local TextSizeDir = 1

I am coming from C++ programming language and want to make the above two variable global like in C++.

How would I declare the above two variables globally and then initialize their values only once at app start?

Also, if there is anything else wrong with above then please let me know…

(“Font01.ttf” is a valid TTF font in the project’s main directory)

Thanks!

JeZxLee

Main.lua is not a loop. It runs once, and that’s it. To do a repeated task, you’d need to set up a function to run repeatedly on a timer or enterFrame listener.

There are a few ways of using globals.

  1. Omit the ‘local’

[lua]

myGlobal = 5

[/lua]

  1. Add the _G. prefix (amounts to the same thing, but makes it more clear that it’s a global…)

[lua]

_G.myGlobal = 5

[/lua]

  1. Declare in a separate module (preferred method)

[lua]

---- globals.lua ----

local m = {}

m.myGlobal = 5

return m

---- main.lua and anywhere else you need to access the globals ----

local glo = require (“globals”)

print (glo.myGlobal)

[/lua]

Hi,

Thanks for the reply!

Is this OK for global variable usage in Lua:

-- "main.lua" if TextSize == nil then TextSize = 0 end if TextSizeDir == nil then TextSizeDir = 1 end -- How to run below once each frame? if TextSizeDir == 1 then if TextSize \<= 50 then TextSize = TextSize + 1 else TextSizeDir = 0 end elseif TextSizeDir == 0 then if TextSize \>= 0 then TextSize = TextSize - 1 else TextSizeDir = 1 end end local displayText = display.newText( "Hello World", display.contentCenterX, display.contentCenterY, "Font01.ttf", TextSize ) -- How to run above once each frame?

If it’s OK now then how can I run the bottom section of code once per frame?

Thanks!

JeZxLee

You don’t need to check if it’s nil first. To set up an enterFrame listener:

local loopThis = function () &nbsp;-- DO STUFF end Runtime:addEventListener("enterFrame", loopThis)

However, I wouldn’t recommend putting your code as it is inside there - it will just create thousands of text objects on top of each other and slow to a crawl. 

You’ll either need to destroy the text object each frame and create a new one (look up display.remove in the docs), or use .xScale and .yScale to change the size of the text object.

You really need to read the docs to get the Lua basics down and learn all about scope, functions, listeners etc.

This is a great resource to help explain scope:  https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Rob

Main.lua is not a loop. It runs once, and that’s it. To do a repeated task, you’d need to set up a function to run repeatedly on a timer or enterFrame listener.

There are a few ways of using globals.

  1. Omit the ‘local’

[lua]

myGlobal = 5

[/lua]

  1. Add the _G. prefix (amounts to the same thing, but makes it more clear that it’s a global…)

[lua]

_G.myGlobal = 5

[/lua]

  1. Declare in a separate module (preferred method)

[lua]

---- globals.lua ----

local m = {}

m.myGlobal = 5

return m

---- main.lua and anywhere else you need to access the globals ----

local glo = require (“globals”)

print (glo.myGlobal)

[/lua]

Hi,

Thanks for the reply!

Is this OK for global variable usage in Lua:

-- "main.lua" if TextSize == nil then TextSize = 0 end if TextSizeDir == nil then TextSizeDir = 1 end -- How to run below once each frame? if TextSizeDir == 1 then if TextSize \<= 50 then TextSize = TextSize + 1 else TextSizeDir = 0 end elseif TextSizeDir == 0 then if TextSize \>= 0 then TextSize = TextSize - 1 else TextSizeDir = 1 end end local displayText = display.newText( "Hello World", display.contentCenterX, display.contentCenterY, "Font01.ttf", TextSize ) -- How to run above once each frame?

If it’s OK now then how can I run the bottom section of code once per frame?

Thanks!

JeZxLee

You don’t need to check if it’s nil first. To set up an enterFrame listener:

local loopThis = function () &nbsp;-- DO STUFF end Runtime:addEventListener("enterFrame", loopThis)

However, I wouldn’t recommend putting your code as it is inside there - it will just create thousands of text objects on top of each other and slow to a crawl. 

You’ll either need to destroy the text object each frame and create a new one (look up display.remove in the docs), or use .xScale and .yScale to change the size of the text object.

You really need to read the docs to get the Lua basics down and learn all about scope, functions, listeners etc.

This is a great resource to help explain scope:  https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Rob