Global Variables

Hello

This has most probably been asked many times but i need a bit of assistance with variables.

I am making a simple app, noughts and crosses.

It uses composer and creates a new scene for each stage, easy, medium, hard. These then go to  scenes for win lose or draw and then onto show replay. The easy, medium, hard and show replay use a set of global variables to record the moves, the turn, tables for win moves etc. These variables are reset after each game.

What is the best way to setup variables that are used throughout the program either for reading or writing without using global variables?

Any advice would be appreciated.

Cheers

Gareth

The easiest was I do this is by using a module. 

I call mine ‘common.lua’ (I copied Roaming Gamers technique.

local common={} common.totalTreasures = 4 -- This is the treasures needed to trigger the end game find key common.findKey = false -- This controls the end game 'find Gold Key' common.atLadder = false -- Are we at a ladder - triggered by a collision common.climbing = false -- Are we partyly climbing a ladder common.climbAnim = 1 common.keydown = false -- If a key currently down? common.map = nil -- a 'global' to the map object if I need it. common.keyHeld = nil -- when we pic up a door key, this is the key #. -- Need to replace the key if we leave teh room. common.doorToEmerge = 0 -- This holds the door # when we load the next map, where do we start common.exitDoor = false -- Are we at an exit door? common.mapToLoad = "" -- Parameter passing which map to load next common.hero = nil -- A pointer ot the hero object common.self = nil -- A pointer to a self object common.atExitDoor = false -- Are we as and exit door? common.gameOver = false return common

Then all I have to do is 

local common = require("common")

in any module that I want to reference.

Then I can access any of it as 

 if common.gameOver == true then common.gameOver = false end

a global variable can be easily created. I like to do it in main.lua, as it is the first file loaded. Just don’t put ‘local’ in it, then it naturally becomes global.

I like to differentiate global variables in 2 ways :

  1. Global variables within a file (e.g. storyBoard1.lua)

  2. Super global variables within the whole app. It can be used by any file.

main.lua :

super\_global\_variable1 = "My variable" super\_global\_variable2 = { }

Before going the route of using global variables, please read this tutorial:  https://docs.coronalabs.com/tutorial/basics/globals/index.html

While global variables exist and can do good things, for most new developers the trouble they lead to are not worth the “convenience” of them.  Corona provides multiple ways to avoid global variables and still pass data between scenes and modules.

We recommend avoiding globals in most cases.

Rob

The easiest was I do this is by using a module. 

I call mine ‘common.lua’ (I copied Roaming Gamers technique.

local common={} common.totalTreasures = 4 -- This is the treasures needed to trigger the end game find key common.findKey = false -- This controls the end game 'find Gold Key' common.atLadder = false -- Are we at a ladder - triggered by a collision common.climbing = false -- Are we partyly climbing a ladder common.climbAnim = 1 common.keydown = false -- If a key currently down? common.map = nil -- a 'global' to the map object if I need it. common.keyHeld = nil -- when we pic up a door key, this is the key #. -- Need to replace the key if we leave teh room. common.doorToEmerge = 0 -- This holds the door # when we load the next map, where do we start common.exitDoor = false -- Are we at an exit door? common.mapToLoad = "" -- Parameter passing which map to load next common.hero = nil -- A pointer ot the hero object common.self = nil -- A pointer to a self object common.atExitDoor = false -- Are we as and exit door? common.gameOver = false return common

Then all I have to do is 

local common = require("common")

in any module that I want to reference.

Then I can access any of it as 

 if common.gameOver == true then common.gameOver = false end

a global variable can be easily created. I like to do it in main.lua, as it is the first file loaded. Just don’t put ‘local’ in it, then it naturally becomes global.

I like to differentiate global variables in 2 ways :

  1. Global variables within a file (e.g. storyBoard1.lua)

  2. Super global variables within the whole app. It can be used by any file.

main.lua :

super\_global\_variable1 = "My variable" super\_global\_variable2 = { }

Before going the route of using global variables, please read this tutorial:  https://docs.coronalabs.com/tutorial/basics/globals/index.html

While global variables exist and can do good things, for most new developers the trouble they lead to are not worth the “convenience” of them.  Corona provides multiple ways to avoid global variables and still pass data between scenes and modules.

We recommend avoiding globals in most cases.

Rob