Weird behavior with require function, global variables

Hello Corona community,

I have a question about the communication of global variables between modules. Sorry in advance if this question sounds elementary, but could not find an answer with a google search…anyway here it goes!

Suppose with have the following simple code:

-------------- main.lua --------------

globalValue = 1

function increment( )

    globalValue = globalValue + 1

    print(globalValue)

end

function test( )

    local test = require(“test”)

    print(test)

end

-------------- test.lua --------------

return globalValue

When I call test( ) for the first time, it prints 1, as expected.

Then we call increment( ). It prints 2.

Now when we call test( ) again, one would assume it’d print 2, but it prints 1 instead!!

Shouldn’t it print 2, since we are creating a fresh copy of the test module every time we call the test( ) function?

This is driving me crazy! I come from a Java background, and emulating OOP in lua has been proving very difficult (and frustrating)…
Please advise me how I can have test( ) print the current value of globalValue.

Thanks in advance!

main.lua

local testNum = require("test") globalValue = 1 function increment( ) globalValue = globalValue + 1 print(globalValue) end function test( ) local num = testNum.getGlobal() print(num) end test() increment() test()

test.lua

local Class = {} function Class.getGlobal() return globalValue end return Class

One method

@kc,

  1. That is how require works.  
  • When you call it, it loads the script at the location once.  
  • Then it stores the value (not a reference) returned by the module.
  • Then it returns that value to you.
  • Subsequent calls to require for that file return the stored ‘value’.
  1. You’re not using it the way it is intended.  While you can use ‘require’ to run a script in a file once, it is generally used to load a module.

yo.lua

-- A module local public = {} function public.doit() print("Yo!") end return public

Using yo.lua

local yo = require( "yo" ) yo.doit()
  1. Why would you want to wrap a global in a module?  Its global.  Just access it directly.  Doing anything else is just slow.

  2. Please format code in future posts:

formatyourcode.jpg

Thanks JonPM!

Your answer works great!  :smiley:

Thank you for your detailed answer, roaminggamer.

As you may have guessed, my understanding of lua is rather rudimentary.

Also, please excuse me as this is only the second time using these forums, so I am unaware of many features. I will format my code in the future.

Sure thing and welcome to the community.  I only mention the formatting bit because it helps us to help you if we can more easily read the code and get the gist of what you’re trying to do.

Cheers,

Ed

main.lua

local testNum = require("test") globalValue = 1 function increment( ) globalValue = globalValue + 1 print(globalValue) end function test( ) local num = testNum.getGlobal() print(num) end test() increment() test()

test.lua

local Class = {} function Class.getGlobal() return globalValue end return Class

One method

@kc,

  1. That is how require works.  
  • When you call it, it loads the script at the location once.  
  • Then it stores the value (not a reference) returned by the module.
  • Then it returns that value to you.
  • Subsequent calls to require for that file return the stored ‘value’.
  1. You’re not using it the way it is intended.  While you can use ‘require’ to run a script in a file once, it is generally used to load a module.

yo.lua

-- A module local public = {} function public.doit() print("Yo!") end return public

Using yo.lua

local yo = require( "yo" ) yo.doit()
  1. Why would you want to wrap a global in a module?  Its global.  Just access it directly.  Doing anything else is just slow.

  2. Please format code in future posts:

formatyourcode.jpg

Thanks JonPM!

Your answer works great!  :smiley:

Thank you for your detailed answer, roaminggamer.

As you may have guessed, my understanding of lua is rather rudimentary.

Also, please excuse me as this is only the second time using these forums, so I am unaware of many features. I will format my code in the future.

Sure thing and welcome to the community.  I only mention the formatting bit because it helps us to help you if we can more easily read the code and get the gist of what you’re trying to do.

Cheers,

Ed