【问题标题】:Attempt to index local (a boolean value)尝试索引本地(布尔值)
【发布时间】:2014-12-13 11:48:34
【问题描述】:

我有 2 个不同的 Lua 文件,main.luagame_model.lua。我正在尝试将一些详细信息保存在 JSON 文件中(我在 Google 上搜索到使用 JSON 文件是保存用户设置和分数的最佳方式),但出现以下错误:

错误:文件:main.lua 线路:11 尝试索引本地“游戏”(布尔值)

为什么会出现此错误,如何解决?

这是我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(在data 子目录中)包含以下代码:

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

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    这意味着模块data.game_model在加载时没有返回任何内容。
    在这种情况下,require 返回true

    【讨论】:

      【解决方案2】:

      要解决lhf's answer 中发现的问题,您可以将表保存和加载函数放在data.game_model 返回的表中,如下所示:

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

      另请注意,一个常见的错误是将函数声明为model:saveTable( t, fn ) 而不是model.saveTable( t, fn )。请记住,前者是model.saveTable( model, t, fn ) 的语法糖。

      现在应该将local game = require( "data.game_model" ) 中的变量game 初始化为包含您的函数的表。您可以轻松检查:

      local game = require("data.game_model")
      
      print( type( game ) )
      for k,v in pairs(game) do
          print(k,v)
      end 
      

      产生如下输出:

      table
      loadTable   function: 0x7f87925afa50
      saveTable   function: 0x7f8794d73cf0
      

      【讨论】:

        【解决方案3】:

        使用下面的代码保存/加载。所有代码均来自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 
        

        用法

        local loadsave = require("loadsave")
        
        myTable = {}
        myTable.musicOn = false
        myTable.soundOn = true
        
        loadsave.saveTable(myTable, "myTable.json")
        

        【讨论】:

        • 感谢您的回答... :)
        猜你喜欢
        • 2022-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-27
        • 2017-03-12
        • 2020-08-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多