Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to index local (a boolean value)

Tags:

lua

coronasdk

I have 2 different Lua files, main.lua and game_model.lua. I'm trying to save some details in a JSON file (I googled that using a JSON file would be the best way to save a user's settings and score), but I'm getting the following error:

Error: File: main.lua Line: 11 Attempt to index local 'game' (a boolean value)

Why is am I getting this error and how can fix it?

Here is the code in my main.lua:

--Main.lua

display.setStatusBar( display.HiddenStatusBar )

local composer = require( "composer" )
local game = require("data.game_model")

myGameSettings = {}
myGameSettings.highScore = 1000
myGameSettings.soundOn = true
myGameSettings.musicOff = true
myGameSettings.playerName = "Andrian Gungon"
game.saveTable(myGameSettings, "mygamesettings.json")

composer.gotoScene("scripts.menu")

game_model.lua (in the data subdirectory) contains this code:

--game_model.lua (located at data/game_model.lua)

local json = require("json")

function saveTable(t, filename)
    local path = system.pathForFile( filename, system.DocumentsDirectory)
    local file = io.open(path, "w")
    if (file) then
        local contents = json.encode(t)
        file:write( contents )
        io.close( file )
        return true
    else
        print( "Error!" )
        return false
    end
end

function loadTable(filename)
    local path = system.pathForFile( filename, system.DocumentsDirectory)
    local contents = ""
    local myTable = {}
    local file = io.open( path, "r" )
    if (file) then         
         local contents = file:read( "*a" )
         myTable = json.decode(contents);
         io.close( file )
         return myTable 
    end
    return nil
end
like image 483
Andrian Gungon Avatar asked Dec 13 '14 11:12

Andrian Gungon


2 Answers

It means that the module data.game_model did not return anything when it was loaded.
In this case, require returns true.

like image 134
lhf Avatar answered Sep 28 '22 07:09

lhf


To fix the problem identified in lhf's answer, you can put your table saving and loading functions in a table that is returned by data.game_model, like this:

-- Filename: data/game_model.lua

local model = {}

local json = require("json")

function model.saveTable( t, filename )
    -- code for saving
end

function model.loadTable( filename )
    -- code for loading
end

return model

Note also that a common mistake would be to declare the functions as model:saveTable( t, fn ) instead of model.saveTable( t, fn ). Remember, the former is syntactic sugar for model.saveTable( model, t, fn ).

Now the variable game in local game = require( "data.game_model" ) should be initialized to a table containing your functions. You can easily check this:

local game = require("data.game_model")

print( type( game ) )
for k,v in pairs(game) do
    print(k,v)
end 

Produces output like:

table
loadTable   function: 0x7f87925afa50
saveTable   function: 0x7f8794d73cf0
like image 23
GoojajiGreg Avatar answered Sep 28 '22 08:09

GoojajiGreg