我在main.lua中有这个文件:
local Vector3 = require "vector3"
local A = {
v = Vector3:new(16,16,16)
}
b = Vector3:new(A.v.x + 2, A.v.y + 3, A.v.z + 4)
print(A.v.x)
这在Vector3.lua中
local Vector3 = {
x,
y,
z
}
function Vector3:new(x,y,z)
o = {}
setmetatable(o,self)
self.__index = self
self.x = x
self.y = y
self.z = z
return o
end
return Vector3
为什么打印18而不是16?我想这与所引用的变量有关。如何获得16个结果?
答案 0 :(得分:4)
Vector3:new
在Vector3
中设置字段,而不是在创建的对象中设置字段。试试这个:
function Vector3:new(x,y,z)
local o = {}
setmetatable(o,self)
self.__index = self
o.x = x
o.y = y
o.z = z
return o
end