gpt4 book ai didi

lua - 尝试索引本地( bool 值)

转载 作者:行者123 更新时间:2023-12-04 16:34:06 26 4
gpt4 key购买 nike

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

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



为什么我会收到此错误,如何解决?

这是我的 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

最佳答案

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

关于lua - 尝试索引本地( bool 值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27458453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com