Function a nil value?

I’m working between two files at the moment. The first being:

module = {} function module:load() module.currentMenu = "main" local menu = nil local gui = display.newGroup() local rect = display.newRect(0, 0, display.contentWidth, 80) rect.alpha = 100/255 local line = display.newLine(0, 80, 568, 80) line.width = 3 line.alpha = 150/255 gui:insert(rect) gui:insert(line) module.update = function() if (module.currentMenu == "main") then rect.x = 0 rect.y = 0 rect.width = display.contentWidth\*2 rect.height = 160 line.x1 = 0 line.x2 = display.contentWidth line.y1 = 80 line.y2 = 80 menu = require("scripts.gui.gui\_menu\_main") menu.load(module) elseif (module.currentMenu == "material") then rect.x = 0 rect.y = 0 rect.width = display.contentWidth rect.height = display.contentHeight line.x1 = 0 line.x2 = display.contentWidth line.y1 = display.contentHeight line.y2 = display.contentHeight menu = require("scripts.gui.gui\_menu\_material") menu.load(module) end end module.update() end return module

and the second being: 

module = {} local button = require("scripts.gui.gui\_button") function module.load(editor) local menu = display.newGroup() print(editor.currentMenu) local material = button.new(-15 + display.contentWidth/7.5, 40, "material", function() editor.currentMenu = "material" editor.update() return true end) local up = button.new(-15 + display.contentWidth/7.5\*2, 40, "up", function() print("up clicked") return true end) local down = button.new(-15 + display.contentWidth/7.5\*3, 40, "down", function() print("down clicked") return true end) local object = button.new(-15 + display.contentWidth/7.5\*4, 40, "rock", function() print("object clicked") return true end) local entity = button.new(-15 + display.contentWidth/7.5\*5, 40, "entity", function() print("entity clicked") return true end) local size = button.new(-15 + display.contentWidth/7.5\*6, 40, "scale", function() print("size clicked") return true end) local save = button.new(-15 + display.contentWidth/7.5\*7, 40, "save", function() print("save clicked") return true end) end return module

My issue is that when I click the ‘material’ button, it’s saying “attempt to call field ‘update’ (a nil value)”

Can somebody explain to me why this is happening? I’m somewhat new to lua, but have plenty of programming experience.

Also, the module from the first class is not getting properly put through to the second class

print(editor.currentMenu) is printing ‘nil’

How can I properly pass the module of the first class to the second class?

Edit: If needed, I can provide the entire project

You must have
local module = {}. The whole point is to make it local and return it at the end of file

alright, thank you! Any chance you could try to explain why that only works if it’s local? I normally don’t have any trouble understanding scope…

What are the names of this files?

map_editor.lua and menu_main.lua

The map editor controls all of the functionality of the map editor GUI, and menu_main is the default menu when the editor opens up. There will be other menus created to appear when certain buttons are touched.

map_editor.lua being the first

and menu_main.lua being the second in the OP

Reason is simple - because you generaly shouldn’t use globals. Lua is scriptic language - it’s different from C or C++ or other similar language because it’s executed dynamically. If you create local variable then it’s easier and faster to use. If you write without local keyword then variable is inserted into global namespace. Always when accessing global namespace there is overhead in finding each time address of this variable in memory. Locals make you know exact position of variable in memory and access is instant. What’s more it gives you encapsulation and module independance.
In your code you have ‘module’ in both files and these are declared declared without local keyword. That means both are inserted into global namespace. Which means one overwrites another one!

ahh okay thanks! I appreciate all your help! I’m starting to understand a bit better how the LUA is being compiled.

I wouldn’t say it’s compiled as such. It’s rather like running script from file. When you build application for mobile device then it’s compiled with other assets (or rather included and compiled with everything)

You must have
local module = {}. The whole point is to make it local and return it at the end of file

alright, thank you! Any chance you could try to explain why that only works if it’s local? I normally don’t have any trouble understanding scope…

What are the names of this files?

map_editor.lua and menu_main.lua

The map editor controls all of the functionality of the map editor GUI, and menu_main is the default menu when the editor opens up. There will be other menus created to appear when certain buttons are touched.

map_editor.lua being the first

and menu_main.lua being the second in the OP

Reason is simple - because you generaly shouldn’t use globals. Lua is scriptic language - it’s different from C or C++ or other similar language because it’s executed dynamically. If you create local variable then it’s easier and faster to use. If you write without local keyword then variable is inserted into global namespace. Always when accessing global namespace there is overhead in finding each time address of this variable in memory. Locals make you know exact position of variable in memory and access is instant. What’s more it gives you encapsulation and module independance.
In your code you have ‘module’ in both files and these are declared declared without local keyword. That means both are inserted into global namespace. Which means one overwrites another one!

ahh okay thanks! I appreciate all your help! I’m starting to understand a bit better how the LUA is being compiled.

I wouldn’t say it’s compiled as such. It’s rather like running script from file. When you build application for mobile device then it’s compiled with other assets (or rather included and compiled with everything)