提问人:James Hood 提问时间:6/12/2023 最后编辑:OkaJames Hood 更新时间:6/13/2023 访问量:85
如何在 Lua 上通过函数传递变量和表格?
How do I pass variables and tables through functions on Lua?
问:
我试图通过将所有内容都放在函数中来简化我的代码,但我不认为变量和表正在传递我的代码中的参数,或者 nodeNumber 无法正常工作,因为每次我运行代码时,我都会收到错误尝试对零值执行算术运算。我正在使用LOVE来创建图形。我正在尝试创建一个图表来显示使用投注策略时的所有可能结果(即,每次您赢得一轮,您将赌注设置回 1,但每次输掉一轮,您都会将之前的赌注翻倍。层数表示到目前为止玩的回合数,v 代表价值(到目前为止你赚或输的钱的总和),b 代表赌注(你下一轮的下一个赌注,遵循我之前提到的策略),每个节点是到目前为止计算的每一美元金额和每一层。
layer0v = {0}
layer0b = {1}
layer1v = {}
layer1b = {}
xValues = {0}
nodeNumber = 1
curLayer = 1
canvWidth = 800
canvHeight = 512
function love.load()
love.window.setMode(canvWidth, canvHeight)
end
repeat
table.insert(xValues, (#xValues + 1), xValues[#xValues] + 5)
until (xValues[#xValues] == canvWidth)
function calculateX(curLayer, xValues)
value = xValues[curLayer]
return value
end
function calculateWV(prevLayerV, prevLayerB, nodeNumber)
value = prevLayerV[nodeNumber] + prevLayerB[nodeNumber]
return value
end
function calculateLV(prevLayerV, prevLayerB, nodeNumber)
value = prevLayerV[nodeNumber] - prevLayerB[nodeNumber]
return value
end
function calculateWB()
value = 1
return value
end
function calculateLB(prevLayerB, nodeNumber)
value = prevLayerB[nodeNumber] * 2
return value
end
function addToTable(curLayerV, curLayerB, prevLayerV, prevLayerB, nodeNumber)
table.insert(curLayerV, (#curLayerV + 1), calculateWV(prevLayerV, prevLayerB, nodeNumber))
table.insert(curLayerV, (#curLayerV + 1), calculateLV(prevLayerV, prevLayerB, nodeNumber))
table.insert(curLayerB, (#curLayerB + 1), calculateWB())
table.insert(curLayerB, (#curLayerB + 1), calculateLB(prevLayerB, nodeNumber))
end
function drawLine(curLayerV, curLayerB, curLayerX, prevLayerV, prevLayerB, prevLayerX, nodeNumber)
love.graphics.line(prevLayerX, (prevLayerV[nodeNumber] + 128), curLayerX, (calculateWV(prevLayerV, prevLayerB, nodeNumber) + 128))
love.graphics.line(prevLayerX, (prevLayerV[nodeNumber] + 128), curLayerX, (calculateLV(prevLayerV, prevLayerB, nodeNumber) + 128))
end
function createLayer(curLayerV, curLayerB, prevLayerV, prevLayerB, nodeNumber, curLayer, xValues)
nodeNumber = 1
repeat
addToTable(curLayerV, curLayerB, prevLayerV, prevLayerB, nodeNumber)
drawLine(curLayerV, curLayerB, calculateX(curLayer, xValues), prevLayerV, prevLayerB, calculateX((curLayer - 1), xValues))
if (nodeNumber ~= #prevLayerV) then
nodeNumber = nodeNumber + 1
end
until (nodeNumber == #prevLayerV)
end
function love.load()
createLayer(layer1v, layer1b, layer0v, layer0b, nodeNumber, curLayer, xValues)
end
我尝试在 nodeNumber 等于 prevLayerV 后使用 break 退出该行。我尝试将 nil 转换为 0,以便计算机可以进行算术运算(即使这不会达到预期的结果。
答:
0赞
koyaanisqatsi
6/13/2023
#1
例
-- main.lua -- Easy Screen Change by Function Change (Endless Loop)
local txt1, txt2, start, stop, fnc1, fnc2
local love, os = require("love"), require("os")
function love.load() -- Only at Start
txt1 = {{0,1,0}, "GO", {1,1,0}, "GO", {1,0,0}, "GO"} -- Colored Text
txt2 = {{love.math.random(),love.math.random(),love.math.random()}, "GAME OVER"} -- Random Color
start = os.time()
stop = 10
fnc1 = function() love.graphics.print(txt1, 10, 10) end -- Green Yellow Red
fnc2 = function() love.graphics.print(txt2, 10, 10) end -- Random Color
love.draw = fnc1 -- Construct love.draw() and Start with fnc1
end
function love.update()
if os.time() > start + stop then
love.draw = fnc2 -- Change to Game Over Screen
elseif os.time() > start + (stop * 2) then
love.event.quit("restart") -- Restart
end
end
-- love.draw() will be constructed by love.load() at every start/restart
-- After start/restart love.draw() will be changed from love.update() by condition
评论
love.draw
回调之外绘制内容是一个坏主意。drawLine
nodeNumber
table.insert
已经默认为在表末尾插入 ()。#t + 1
createLayer
calculateX((curLayer - 1), xValues)
nil
curLayer
1
xValues[0]
table.insert
drawLine
xValues[0]
local
love.update()
love.draw()
conf.lua