File loads and reads in separate lua file

I have all of my file operations, game save, load, defaults, etc. in a separate lua file from the main. Since this all new for me I am puzzled by the problem that is happening.

When the file save/load functions are in the main code there is no problems with their execution however when removed and having the main call these functions I get the following error:

main.lua:710 attempt to call global function ‘setNewGameDefaults’ (a nil value).

This is reading the set defaults for a new game requested by the user, if the file does not exist for some reason or if there are new defaults added it builds the file and then loads it to the game.

See attached:

When you import the gameFileOperations file like that, its functions don’t get automatically dumped into the main file’s namespace. What you probably want to do is this:

In gameFileOperations.lua create a table, and then create all your functions as fields of that table. That way they’re all nicely scoped. Return the table at the end of the module.

local gameFileOperations = {} gameFileOperations.buildGameDefaults = function(params)   yada yada yada end return gameFileOperations

In main.lua when you import the module, that table you returned at the end of gameFileOperations.lua will get assigned to the local gameFileOperations variable. Now you can easily call the functions within that table.

local gameFileOperations = require("gameFileOperations") gameFileOperations.buildGameDefaults(params)

Yes.  That is exactly right.  Turn your separate lua ‘code’ into a module and use it the way aaronkarp shows.

Tip: Lua’s require is not like C’s include.

And the simple way to create a module is in your Lua file to be loaded declare a table:

local module = {}

Add anything to that module you like:

function module.doSomething()

Set some properties:

module.myValue = "Hello World"

Then return the module:

return module

Now most people name their module “M”, i.e.

local M = {} function M.doSomething()     -- end M.someValue = "Hello World" return M

Now when you want to use the doSomething() function or access the value someValue, simply require it in the Lua file where you need it:

local myModule = require("theLuaFileWtihTheModule") -- leave off the .lua

And now you can do:

myModule.doSomething()

Rob

Ok good info, now my next question, and I am probably missing something, my functions are file loads and reads. the reads return sorted tables. Such as:

-----module

gameFileOperationsData.loadExistingGame = function ()

local statSavedBar = {}

local statSavedBar = processGame(processType, processAction, statSavedBar)    
     if statSavedBar ~= nil then
         local sort_func = function( a,b ) return a.statName < b.statName end
         table.sort( statSavedBar, sort_func )  

     end

return statsSavedBar

end

return gameFileOperationsData

-----MAIN

other code----

if mode == loadGame    then
            print(‘mode is load’)
            gameFileOperationsData.loadExistingGame ()
            print(‘load existing error’)

end

ERROR:

Runtime error
c:\users\lee\documents\vectorsys\corona projects\scratchgame\main.lua:671: attempt to index upvalue ‘gameFileOperationsLogical’ (a boolean value)
stack traceback:
    c:\users\lee\documents\vectorsys\corona projects\scratchgame\main.lua:671: in function <c:\users\lee\documents\vectorsys\corona projects\scratchgame\main.lua:661>
    ?: in function <?:221>

  1.  Please format future code posts.  It is hard to read code posted inline, which takes more time… i.e. less like to get an answer:

formatyourcode.jpg

  1. Your code is wrong.  You didn’t follow Robs advice.  Oh, and avoid globals.  This is closer to what your module should look like:

    – assume I saved this in a file called ‘gfod.lua’ – local gameFileOperationsData = {} – Define the table as a local FIRST. gameFileOperationsData.loadExistingGame = function( ) – Now assign methods and fields local statSavedBar = {} local statSavedBar = processGame(processType, processAction, statSavedBar) if statSavedBar ~= nil then local sort_func = function( a,b ) return a.statName < b.statName end table.sort( statSavedBar, sort_func ) end return statsSavedBar end return gameFileOperationsData

Now in main:

local gfod = require "gfod" if mode == loadGame then print('mode is load') gfod.loadExistingGame () print('load existing error') end

PS - Notice that not only did I use code blocks to format, but I went back and re-edited this post till it was legible and the code was formatted nicely.  Please do the same when formatting and posting if you want folks to help.  I am always more inclined to help those who spend time making the question easy for me to read and understand.  (Not an admonition, only friendly advice.)

it works thanks. The explanation was much easier for me to follow (you pretty much handed it to me), but being new to this you saved me a lot of frustration.

When you import the gameFileOperations file like that, its functions don’t get automatically dumped into the main file’s namespace. What you probably want to do is this:

In gameFileOperations.lua create a table, and then create all your functions as fields of that table. That way they’re all nicely scoped. Return the table at the end of the module.

local&nbsp;gameFileOperations = {} gameFileOperations.buildGameDefaults = function(params) &nbsp; yada yada yada end return&nbsp;gameFileOperations

In main.lua when you import the module, that table you returned at the end of gameFileOperations.lua will get assigned to the local gameFileOperations variable. Now you can easily call the functions within that table.

local&nbsp;gameFileOperations = require("gameFileOperations") gameFileOperations.buildGameDefaults(params)

Yes.  That is exactly right.  Turn your separate lua ‘code’ into a module and use it the way aaronkarp shows.

Tip: Lua’s require is not like C’s include.

And the simple way to create a module is in your Lua file to be loaded declare a table:

local module = {}

Add anything to that module you like:

function module.doSomething()

Set some properties:

module.myValue = "Hello World"

Then return the module:

return module

Now most people name their module “M”, i.e.

local M = {} function M.doSomething() &nbsp;&nbsp;&nbsp; -- end M.someValue = "Hello World" return M

Now when you want to use the doSomething() function or access the value someValue, simply require it in the Lua file where you need it:

local myModule = require("theLuaFileWtihTheModule") -- leave off the .lua

And now you can do:

myModule.doSomething()

Rob

Ok good info, now my next question, and I am probably missing something, my functions are file loads and reads. the reads return sorted tables. Such as:

-----module

gameFileOperationsData.loadExistingGame = function ()

local statSavedBar = {}

local statSavedBar = processGame(processType, processAction, statSavedBar)    
     if statSavedBar ~= nil then
         local sort_func = function( a,b ) return a.statName < b.statName end
         table.sort( statSavedBar, sort_func )  

     end

return statsSavedBar

end

return gameFileOperationsData

-----MAIN

other code----

if mode == loadGame    then
            print(‘mode is load’)
            gameFileOperationsData.loadExistingGame ()
            print(‘load existing error’)

end

ERROR:

Runtime error
c:\users\lee\documents\vectorsys\corona projects\scratchgame\main.lua:671: attempt to index upvalue ‘gameFileOperationsLogical’ (a boolean value)
stack traceback:
    c:\users\lee\documents\vectorsys\corona projects\scratchgame\main.lua:671: in function <c:\users\lee\documents\vectorsys\corona projects\scratchgame\main.lua:661>
    ?: in function <?:221>

  1.  Please format future code posts.  It is hard to read code posted inline, which takes more time… i.e. less like to get an answer:

formatyourcode.jpg

  1. Your code is wrong.  You didn’t follow Robs advice.  Oh, and avoid globals.  This is closer to what your module should look like:

    – assume I saved this in a file called ‘gfod.lua’ – local gameFileOperationsData = {} – Define the table as a local FIRST. gameFileOperationsData.loadExistingGame = function( ) – Now assign methods and fields local statSavedBar = {} local statSavedBar = processGame(processType, processAction, statSavedBar) if statSavedBar ~= nil then local sort_func = function( a,b ) return a.statName < b.statName end table.sort( statSavedBar, sort_func ) end return statsSavedBar end return gameFileOperationsData

Now in main:

local gfod = require "gfod" if mode == loadGame then print('mode is load') gfod.loadExistingGame () print('load existing error') end

PS - Notice that not only did I use code blocks to format, but I went back and re-edited this post till it was legible and the code was formatted nicely.  Please do the same when formatting and posting if you want folks to help.  I am always more inclined to help those who spend time making the question easy for me to read and understand.  (Not an admonition, only friendly advice.)

it works thanks. The explanation was much easier for me to follow (you pretty much handed it to me), but being new to this you saved me a lot of frustration.