2014-12-13 13 views
5

Mam 2 różne pliki Lua, main.lua i game_model.lua. Próbuję zapisać pewne szczegóły w pliku JSON (google, że przy użyciu pliku JSON byłby najlepszy sposób, aby zapisać ustawienia i ocena użytkownika), ale ja dostaję następujący błąd:Próba indeksu lokalnego (wartość boolowska)

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

Dlaczego otrzymuję ten błąd i jak mogę go naprawić?

Oto kod w moim 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 (w podkatalogu data) zawiera ten kod:

--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 

Odpowiedz

8

Oznacza to, że moduł data.game_model nic nie wrócić, kiedy to było załadowany.
W tym przypadku require zwraca true.

1

Aby rozwiązać ten problem zidentyfikowany w lhf's answer, można umieścić stole zapisywania i wczytywania funkcji w tabeli, który jest zwracany przez data.game_model coś takiego:

-- 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 

Należy również zauważyć, że częstym błędem byłoby zadeklarować funkcje jako model:saveTable(t, fn) zamiast . Pamiętaj, że ten pierwszy jest cukrem syntaktycznym dla model.saveTable(model, t, fn).

Teraz zmienna game w local game = require("data.game_model") powinna zostać zainicjalizowana do tabeli zawierającej twoje funkcje. Można to łatwo sprawdzić:

local game = require("data.game_model") 

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

Wytwarza wyjścia jak:

table 
loadTable function: 0x7f87925afa50 
saveTable function: 0x7f8794d73cf0 
2

Użyj kodu poniżej, aby zapisać/obciążenia. Cały kod pochodzi z github/robmiracle.

local M = {} 
local json = require("json") 
local _defaultLocation = system.DocumentsDirectory 
local _realDefaultLocation = _defaultLocation 
local _validLocations = { 
    [system.DocumentsDirectory] = true, 
    [system.CachesDirectory] = true, 
    [system.TemporaryDirectory] = true 
} 

function M.saveTable(t, filename, location) 
    if location and (not _validLocations[location]) then 
    error("Attempted to save a table to an invalid location", 2) 
    elseif not location then 
     location = _defaultLocation 
    end 

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

function M.loadTable(filename, location) 
    if location and (not _validLocations[location]) then 
    error("Attempted to load a table from an invalid location", 2) 
    elseif not location then 
     location = _defaultLocation 
    end 
    local path = system.pathForFile(filename, location) 
    local contents = "" 
    local myTable = {} 
    local file = io.open(path, "r") 
    if file then 
     -- read all contents of file into a string 
     local contents = file:read("*a") 
     myTable = json.decode(contents); 
     io.close(file) 
     return myTable 
    end 
    return nil 
end 

function M.changeDefault(location) 
    if location and (not location) then 
     error("Attempted to change the default location to an invalid location", 2) 
    elseif not location then 
     location = _realDefaultLocation 
    end 
    _defaultLocation = location 
    return true 
end 

function M.print_r (t) 
    local print_r_cache={} 
    local function sub_print_r(t,indent) 
     if (print_r_cache[tostring(t)]) then 
      print(indent.."*"..tostring(t)) 
     else 
      print_r_cache[tostring(t)]=true 
      if (type(t)=="table") then 
       for pos,val in pairs(t) do 
        if (type(val)=="table") then 
         print(indent.."["..pos.."] => "..tostring(t).." {") 
         sub_print_r(val,indent..string.rep(" ",string.len(pos)+8)) 
         print(indent..string.rep(" ",string.len(pos)+6).."}") 
        elseif (type(val)=="string") then 
         print(indent.."["..pos..'] => "'..val..'"') 
        else 
         print(indent.."["..pos.."] => "..tostring(val)) 
        end 
       end 
      else 
       print(indent..tostring(t)) 
      end 
     end 
    end 
    if (type(t)=="table") then 
     print(tostring(t).." {") 
     sub_print_r(t," ") 
     print("}") 
    else 
     sub_print_r(t," ") 
    end 
    print() 
end 

M.printTable = M.print_r 

return M 

Wykorzystanie

local loadsave = require("loadsave") 

myTable = {} 
myTable.musicOn = false 
myTable.soundOn = true 

loadsave.saveTable(myTable, "myTable.json") 
+0

Dzięki za odpowiedź ... :) –

Powiązane problemy