Issue with module

So I’ve got some issues with my app since recent updates that I’m trying to resolve, but besides that, I am trying to change something that I currently have setup as a storyboard scene, into just a module.

The lua file doesn’t have any visual elements, it just runs a few checks, advances the turn count and the player turn, and then reloads the mainGame.lua file.

It clearly should be a module and not a scene.

So I removed the storyboard stuff from the file, and put the rest of the file inside a local function called turnControl

I required the module at the top of mainGame with
local overlordMod = require ( “overlordModule” )

and when I call the module I used
overlordMod.turnControl()

I get an error that says "attempt to index upvalue ‘overlordMod’ "

any ideas whats going wrong with that?
the module itself doesn’t return anything. It loads a data file, makes checks and changes, saves the data file, and ends.

the next function called by the mainGame.lua file then loads the data file and uses whatever info is in it. [import]uid: 170004 topic_id: 36257 reply_id: 336257[/import]

Not having seen the rest of the code it looks like an error with “scoping”. Are you sure that overlordMod.turnControl() is located in a lua chunk that is inside the chunk containing local overlordMod = require("…") ? [import]uid: 159908 topic_id: 36257 reply_id: 144031[/import]

yes, but i’ll post code

--  
-- overlordModule.lua  
--  
---------------------------------------------------------------------------------  
local json = require "json"  
local loadsave = require("loadsave")  
  
local turnControl = function ()  
  
-- all the code for advances game turn and player turn number  
  
return true  
end  

the mainGame.lua file looks like this

[code]


– mainGame.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local json = require “json”
local widget = require “widget”

local loadsave = require( “loadsave” )
local loader = require( “loader” )

local overlordMod = require( “overlordModule” ) – The attempt at making a module

– followed by many variables and lua tables

– Called when the scene’s view does not exist:
function scene:createScene( event )
local screenGroup = self.view

– completely remove mainmenu and creditsScreen
storyboard.removeScene( “overlord” )
print( “\nmaingame: createScene event” )
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local screenGroup = self.view
print( “\nmainGame: enterScene event” )

– create display groups
local mainDisplay = display.newGroup()
local editMenu = display.newGroup()
local contextMenu = display.newGroup()
local panel = display.newGroup()
local research = display.newGroup()
local leader = display.newGroup()
local transfer = display.newGroup()
local market = display.newGroup()
local victory = display.newGroup()
local timecounter = display.newGroup()

– followed by a lot of various code

– Initializes all functions created
local turnStart = function()
collection()
print(“Collection Completed”)
mapDraw()
print(“mapDraw Completed”)
panelDraw()
print(“panelDraw Completed”)
location()
print(“location Completed”)

end

– First call
print( “maingame: enterScene event” )
overlordMod.turnControl() – This is me trying to call the function from inside overlordMod Module
turnStart()
end

– Called when scene is about to move offscreen:
function scene:exitScene()

print( “maingame: exitScene event” )

end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

print( “((destroying maingame view))” )
end
[/code] [import]uid: 170004 topic_id: 36257 reply_id: 144033[/import]

the function turnControl() is local to overlordModule.lua and you cannot call it from outside that file (a file is a chunk). Try this:

[code]


– overlordModule.lua


local M = {} – new table containing all elements of the module to be passed outside the module

local json = require “json”
local loadsave = require(“loadsave”)

– local turnControl = function () delete this and substitute with…
function M.turnControl()
– YOUR CODE
end
– all the code for advances game turn and player turn number

return true
end

return M

[/code] [import]uid: 159908 topic_id: 36257 reply_id: 144034[/import]

Hi paolo.longato

I don’t really understand it, but it does work now.
:wink:
Of course, what ever the issue that has forced my app to crash the simulator since the widget update has not been fixed so it still crashes.

But I can see that it is now correctly using the module.

Thank you [import]uid: 170004 topic_id: 36257 reply_id: 144036[/import]

When you call “require” what happens is that your module content is imported into the file making the call and executed at that point as a separate chunk (just like a function). If you call local moduleA = require “module A”, local moduleA should be a nil and but LUA diligently executes its content. For “module A” not to be nil, you have to pass something to it (“return M” in our example). M is a LUA table containing all the stuff that you want to pass outside the chunk. Modules are explained in PIL (Programming in Lua – excellent book, if you want to go even deeper). [import]uid: 159908 topic_id: 36257 reply_id: 144037[/import]

I should and will read PIL.

Thanks again [import]uid: 170004 topic_id: 36257 reply_id: 144038[/import]

To try and simply this, think of it this way… require is a function that loads a module and it returns whatever that module returns. So:
somevar = require(“somemodule”)
will cause somevar to be nil if somemodule.lua doesn’t return anything. Your module doesn’t return anything.

An upvalue is basically a variable defined in a block of code higher than you’re currently executing at. Make your module return something. Typically a table or display object in many cases.

[import]uid: 199310 topic_id: 36257 reply_id: 144068[/import]

Not having seen the rest of the code it looks like an error with “scoping”. Are you sure that overlordMod.turnControl() is located in a lua chunk that is inside the chunk containing local overlordMod = require("…") ? [import]uid: 159908 topic_id: 36257 reply_id: 144031[/import]

yes, but i’ll post code

--  
-- overlordModule.lua  
--  
---------------------------------------------------------------------------------  
local json = require "json"  
local loadsave = require("loadsave")  
  
local turnControl = function ()  
  
-- all the code for advances game turn and player turn number  
  
return true  
end  

the mainGame.lua file looks like this

[code]


– mainGame.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local json = require “json”
local widget = require “widget”

local loadsave = require( “loadsave” )
local loader = require( “loader” )

local overlordMod = require( “overlordModule” ) – The attempt at making a module

– followed by many variables and lua tables

– Called when the scene’s view does not exist:
function scene:createScene( event )
local screenGroup = self.view

– completely remove mainmenu and creditsScreen
storyboard.removeScene( “overlord” )
print( “\nmaingame: createScene event” )
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local screenGroup = self.view
print( “\nmainGame: enterScene event” )

– create display groups
local mainDisplay = display.newGroup()
local editMenu = display.newGroup()
local contextMenu = display.newGroup()
local panel = display.newGroup()
local research = display.newGroup()
local leader = display.newGroup()
local transfer = display.newGroup()
local market = display.newGroup()
local victory = display.newGroup()
local timecounter = display.newGroup()

– followed by a lot of various code

– Initializes all functions created
local turnStart = function()
collection()
print(“Collection Completed”)
mapDraw()
print(“mapDraw Completed”)
panelDraw()
print(“panelDraw Completed”)
location()
print(“location Completed”)

end

– First call
print( “maingame: enterScene event” )
overlordMod.turnControl() – This is me trying to call the function from inside overlordMod Module
turnStart()
end

– Called when scene is about to move offscreen:
function scene:exitScene()

print( “maingame: exitScene event” )

end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

print( “((destroying maingame view))” )
end
[/code] [import]uid: 170004 topic_id: 36257 reply_id: 144033[/import]

the function turnControl() is local to overlordModule.lua and you cannot call it from outside that file (a file is a chunk). Try this:

[code]


– overlordModule.lua


local M = {} – new table containing all elements of the module to be passed outside the module

local json = require “json”
local loadsave = require(“loadsave”)

– local turnControl = function () delete this and substitute with…
function M.turnControl()
– YOUR CODE
end
– all the code for advances game turn and player turn number

return true
end

return M

[/code] [import]uid: 159908 topic_id: 36257 reply_id: 144034[/import]

Hi paolo.longato

I don’t really understand it, but it does work now.
:wink:
Of course, what ever the issue that has forced my app to crash the simulator since the widget update has not been fixed so it still crashes.

But I can see that it is now correctly using the module.

Thank you [import]uid: 170004 topic_id: 36257 reply_id: 144036[/import]

When you call “require” what happens is that your module content is imported into the file making the call and executed at that point as a separate chunk (just like a function). If you call local moduleA = require “module A”, local moduleA should be a nil and but LUA diligently executes its content. For “module A” not to be nil, you have to pass something to it (“return M” in our example). M is a LUA table containing all the stuff that you want to pass outside the chunk. Modules are explained in PIL (Programming in Lua – excellent book, if you want to go even deeper). [import]uid: 159908 topic_id: 36257 reply_id: 144037[/import]

I should and will read PIL.

Thanks again [import]uid: 170004 topic_id: 36257 reply_id: 144038[/import]

To try and simply this, think of it this way… require is a function that loads a module and it returns whatever that module returns. So:
somevar = require(“somemodule”)
will cause somevar to be nil if somemodule.lua doesn’t return anything. Your module doesn’t return anything.

An upvalue is basically a variable defined in a block of code higher than you’re currently executing at. Make your module return something. Typically a table or display object in many cases.

[import]uid: 199310 topic_id: 36257 reply_id: 144068[/import]

Not having seen the rest of the code it looks like an error with “scoping”. Are you sure that overlordMod.turnControl() is located in a lua chunk that is inside the chunk containing local overlordMod = require("…") ? [import]uid: 159908 topic_id: 36257 reply_id: 144031[/import]

yes, but i’ll post code

--  
-- overlordModule.lua  
--  
---------------------------------------------------------------------------------  
local json = require "json"  
local loadsave = require("loadsave")  
  
local turnControl = function ()  
  
-- all the code for advances game turn and player turn number  
  
return true  
end  

the mainGame.lua file looks like this

[code]


– mainGame.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local json = require “json”
local widget = require “widget”

local loadsave = require( “loadsave” )
local loader = require( “loader” )

local overlordMod = require( “overlordModule” ) – The attempt at making a module

– followed by many variables and lua tables

– Called when the scene’s view does not exist:
function scene:createScene( event )
local screenGroup = self.view

– completely remove mainmenu and creditsScreen
storyboard.removeScene( “overlord” )
print( “\nmaingame: createScene event” )
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local screenGroup = self.view
print( “\nmainGame: enterScene event” )

– create display groups
local mainDisplay = display.newGroup()
local editMenu = display.newGroup()
local contextMenu = display.newGroup()
local panel = display.newGroup()
local research = display.newGroup()
local leader = display.newGroup()
local transfer = display.newGroup()
local market = display.newGroup()
local victory = display.newGroup()
local timecounter = display.newGroup()

– followed by a lot of various code

– Initializes all functions created
local turnStart = function()
collection()
print(“Collection Completed”)
mapDraw()
print(“mapDraw Completed”)
panelDraw()
print(“panelDraw Completed”)
location()
print(“location Completed”)

end

– First call
print( “maingame: enterScene event” )
overlordMod.turnControl() – This is me trying to call the function from inside overlordMod Module
turnStart()
end

– Called when scene is about to move offscreen:
function scene:exitScene()

print( “maingame: exitScene event” )

end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

print( “((destroying maingame view))” )
end
[/code] [import]uid: 170004 topic_id: 36257 reply_id: 144033[/import]

the function turnControl() is local to overlordModule.lua and you cannot call it from outside that file (a file is a chunk). Try this:

[code]


– overlordModule.lua


local M = {} – new table containing all elements of the module to be passed outside the module

local json = require “json”
local loadsave = require(“loadsave”)

– local turnControl = function () delete this and substitute with…
function M.turnControl()
– YOUR CODE
end
– all the code for advances game turn and player turn number

return true
end

return M

[/code] [import]uid: 159908 topic_id: 36257 reply_id: 144034[/import]

Hi paolo.longato

I don’t really understand it, but it does work now.
:wink:
Of course, what ever the issue that has forced my app to crash the simulator since the widget update has not been fixed so it still crashes.

But I can see that it is now correctly using the module.

Thank you [import]uid: 170004 topic_id: 36257 reply_id: 144036[/import]

When you call “require” what happens is that your module content is imported into the file making the call and executed at that point as a separate chunk (just like a function). If you call local moduleA = require “module A”, local moduleA should be a nil and but LUA diligently executes its content. For “module A” not to be nil, you have to pass something to it (“return M” in our example). M is a LUA table containing all the stuff that you want to pass outside the chunk. Modules are explained in PIL (Programming in Lua – excellent book, if you want to go even deeper). [import]uid: 159908 topic_id: 36257 reply_id: 144037[/import]