Access object in main with external function - possible?

Hi, 

I have a very simple problem but for some reason this seems complicated:

main.lua    <- here I have a display.newText    “Hello World”

some_module.lua   <-  here I have a function that needs to be able to change the text  in main into something else

No matter what I do inside some_module the text doesn’t change or there is an error. I need some way to make the object in main available to the external function. Is it at all possible to accomplish? 

[edit: code example]

-- Main.lua local somemodule = require("some\_module") labelText = display.newText({options}) labelText.text = "Hello World"

-- some\_module.lua labelText.text="Something" -- nope main.labelText.text="Something" -- nope? -- ?

It is possible. Try this:

-- Main.lua labelText = display.newText({options}) labelText.text = "Hello World" local somemodule = require("some\_module")

-- some\_module.lua labelText.text="Something" -- yep

The reason your first attempt did not work is because you are requiring the some_module.lua file before labelText is created. By moving the require statement to after the text object creation, the module can now find the global variable.  

On that note, global variables are generally best to avoid. If you need a text object which can be accessed from multiple files, I would suggest making the variable itself local, and add a global function which can access it. This way if you accidentally name an object in another file “labelText”, it won’t overwrite the existing one:

-- Main.lua local labelText = display.newText("", display.contentWidth \* 0.5, display.contentHeight \* 0.5, native.systemFont, 30) function updateMainLabel(str) --first check that string is not nil if str then labelText.text = str end end updateMainLabel("Hello World") local somemodule = require("some\_module")

-- some\_module.lua updateMainLabel("Something")

Although you potentially could end up naming a function in another file “updateMainLabel” by mistake, it’s much less likely.

Ahh good ol’ scoping issues. Thanks, I’ll test it out!

Btw I generally take care to avoid global variables but I was under the impression that global functions are discouraged as well. Also I thought that the whole “goodbye global variables” thing was more security and memory related rather than name conflicting related. Interesting.

I would recommend using a data module to story things in you need to access in multiple modules. You can read about the idea here:
 

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Rob

It is possible. Try this:

-- Main.lua labelText = display.newText({options}) labelText.text = "Hello World" local somemodule = require("some\_module")

-- some\_module.lua labelText.text="Something" -- yep

The reason your first attempt did not work is because you are requiring the some_module.lua file before labelText is created. By moving the require statement to after the text object creation, the module can now find the global variable.  

On that note, global variables are generally best to avoid. If you need a text object which can be accessed from multiple files, I would suggest making the variable itself local, and add a global function which can access it. This way if you accidentally name an object in another file “labelText”, it won’t overwrite the existing one:

-- Main.lua local labelText = display.newText("", display.contentWidth \* 0.5, display.contentHeight \* 0.5, native.systemFont, 30) function updateMainLabel(str) --first check that string is not nil if str then labelText.text = str end end updateMainLabel("Hello World") local somemodule = require("some\_module")

-- some\_module.lua updateMainLabel("Something")

Although you potentially could end up naming a function in another file “updateMainLabel” by mistake, it’s much less likely.

Ahh good ol’ scoping issues. Thanks, I’ll test it out!

Btw I generally take care to avoid global variables but I was under the impression that global functions are discouraged as well. Also I thought that the whole “goodbye global variables” thing was more security and memory related rather than name conflicting related. Interesting.

I would recommend using a data module to story things in you need to access in multiple modules. You can read about the idea here:
 

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Rob