尝试对本地变量(一个布尔值)进行索引
2017-3-23 17:51:1
收藏:0
阅读:114
评论:3
我有两个不同的 Lua 文件,main.lua 和 game_model.lua。我试图将一些细节保存在 JSON 文件中(我谷歌了一下,使用 JSON 文件是保存用户设置和分数的最佳方式),但是我得到了以下错误:
错误:文件:main.lua 行:11 尝试索引本地变量“game”(一个布尔值)
为什么会出现这个错误,我该如何解决?
这是我的 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(位于 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
点赞
用户3455883
为了解决lhf的答案中指出的问题,您可以将表格保存和加载功能放置在由data.game_model返回的表格中,就像这样:
-- 存放路径: data/game_model.lua
local model = {}
local json = require("json")
function model.saveTable( t, filename )
-- 存储代码
end
function model.loadTable( filename )
-- 加载代码
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
2017-03-23 13:14:52
用户7026995
使用以下代码进行保存/加载。所有代码都来自 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("尝试将表保存到无效位置", 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("尝试从无效位置加载表", 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("尝试将默认位置更改为无效位置", 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")
2017-03-23 18:25:46
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
这意味着当模块
data.game_model被加载时,它没有返回任何东西。在这种情况下,
require返回true。