图形绘制器 - 我的 Rbx.Lua 脚本出了什么问题?

我有一个用于 Roblox 版本的 Lua 脚本。Roblox 的语法检查系统说我的脚本没有问题。这个脚本应该使用“部件”或“砖块”创建一个圆的图形。下面是我从维基页面中得到的图形函数。

我认为 bounds.from 是砖块的当前位置;bounds.to 是下一个砖块的下一个计算位置;bounds.step 是正在进行的步骤计数器 - 这意味着您可以更改图形的分辨率(如 1,000 个点或 10,000 个点)。

图形函数的维基页面。'制作图形器'是我所使用的内容。

local p = Instance.new("Part")
p.Anchored      = true
p.BottomSurface = "Smooth"
p.TopSurface    = "Smooth"
p.BrickColor    = BrickColor.Black()
p.FormFactor    = "Custom"
p.Size          = Vector3.new(1, 1, 1)

function graph(bounds, f)
    bounds.step = bounds.step or (bounds.max - bounds.min) / bounds.n
    for t = bounds.from, bounds.to, bounds.step do
        local xyz = f(t)
        local p = p.clone()
        p.CFrame = CFrame.new(xyz)
        p.Parent = game.Workspace
    end
end

graph({from = 0, to = math.pi * 12, n = 1000}, function(t)
    return Vector3.new(
        5 * math.cos(t),
        5 * math.sin(t),
        0
    )
end)

PS:我在代数 1 阶段,所以我不知道正弦、余弦和正切,也不知道参数方程。

点赞
用户4669648
用户4669648

在最初查看代码时,我注意到有相当多的语法错误,所以我在 Roblox Wiki 上查看了代码,惊讶地发现你提供的代码与他们的代码相同。

暂时不考虑 Roblox Wiki 维护的破损代码,让我们看看为什么你的代码无法运行,以及你可以做什么来修复它。

我将从你遇到的问题开始,这个问题源于语句 bounds.step = bounds.step or (bounds.max - bounds.min) / bounds.n

(由于 Lua 的工作原理,解释器将其作为 (bounds[max] - bounds[min]) / bounds.n 读取,因为 table.valuetable[value] 的语法糖,而我们从未定义 bounds.maxbounds.min,这就是你收到错误的原因。)

评估该表达式的正确方式是使用显式参数调用 math.max()math.min(),如下所示:(math.max(bounds.from, bounds.to) - math.min(bounds.from, bounds.to)) / bounds.n,因为两者都未编写为方法使用,任何情况下都只能使用整数/浮点数作为参数;表不允许,会 返回错误

我注意到的第二个错误是,在修复上述问题后,用于克隆模板部件(在全局范围中定义为 local p)的原始代码的编写者使用了 p.clone(),但这实际上会返回此错误:01:49:37.933 - Expected ':' not '.' calling member function clone

这个错误的返回响应相当自描述,但我还是会说:为了修复这个错误,只需在图函数中将 local p = p.clone() 替换为 local p = p:clone()

如果上述解释过于混乱,或者你不想读它,我在下面提供了修复后的代码(完整版)。

愉快的编程。


TL;DR 或 修复后的代码:

local p = Instance.new("Part")
p.Anchored      = true
p.BottomSurface = "Smooth"
p.TopSurface    = "Smooth"
p.BrickColor    = BrickColor.Black()
p.FormFactor    = "Custom"
p.Size          = Vector3.new(1, 1, 1)

function graph(bounds, f)
    bounds.step = bounds.step or (math.max(bounds.from, bounds.to) - math.min(bounds.from, bounds.to)) / bounds.n
    for t = bounds.from, bounds.to, bounds.step do
        local xyz = f(t)
        local p = p:clone()
        p.CFrame = CFrame.new(xyz)
        p.Parent = game.Workspace
    end
end

graph({from = 0, to = math.pi * 12, n = 1000}, function(t)
    return Vector3.new(
        5 * math.cos(t),
        5 * math.sin(t),
        0
    )
end)
2015-03-14 06:04:12