local newVector4d
do
--[[/* Not working code | rewritten below | still 'fixed' for fun
local vector4d = setmetatable({}, {
__newindex = function(self, key, value) --# renamed variable '_' to 'self'
if type(value) == "function" then
local func = setmetatable({}, {
__call = function(_, ...)
if self == ({...})[1] then --# added parenthesis around {...}
return value(...)
end
return value(self, ...)
end;
})
rawset(self, key, func)
else
rawset(self, key, value) --# reordered arguments (value, key) => (key, value)
end
end;
})
--*/]]
local vector4d = {} --# added a class table declaration
function vector4d:moveX(offset) --# changed to colon notation
self.x = self.x + offset
return self
end
function vector4d:getX()
return self.x
end
function vector4d:moveY(offset)
self.y = self.y + offset --# added the correct variable reassignment
return self --# returning self to allow method chaining, as in the above function originally
end
function vector4d:getY()
return self.y --# returning property 'y' instead of property 'x'
end
function vector4d:moveZ(offset)
self.z = self.z + offset --# there is no '+=' in Lua you silly :P/>
return self
end
function vector4d:getZ()
return self.z --# colon notation only works with functions!
end
--# set the Turtle's facing direction
function vector4d:setFacing(face) --# changed to colon notation
self.facing = (self.facing + face) % 3 --# reorganized assigning and returning 'self' variable to allow chaining of functions
return self
end
function vector4d:getFacing()
return self.facing
end
function vector4d:tostring() --# added missing parenthesis
return self.x..","..self.y..","..self.z..","..self.facing --# too many dots at the end, only two ( .. ) are needed for concatenation
end
newVector4d = function(x, y, z, facing)
local v = { --# use the right bracket for table constructors! Changed '[' to '{'
x = x or 0, --# probably should be 0 instead of 1 as others are
y = y or 0,
z = z or 0, --# should be using an 'or' here, or the z will (mostly) always stay 0 after initialization
facing = facing or 0;
direction = "Calculating...", --# forgot a comma or a semicolon
--# removed '_tostring = vector4d.tostring'
}
--[ My own code for fixing purposes | see the top commented out line ]
local vmt = {}
for key, val in pairs(vector4d) do
if type(val) == "function" then
vmt[key] = function (self, ...)
if self == v then
return vector4d[key](self, ...) --# 'vector4d[key]' instead of 'val' in case the function is changed in 'vector4d' table after creating an object instance
else
return vector4d[key](v, self, ...)
end
end
else
vmt[key] = val
end
end
--[ End of my own code ]
return setmetatable(v, {__index = vmt, __tostring = vector4d.tostring}) --# switched 'v' with 'vector3d' and renamed 'vector3d' to 'vmt'. It's '=' for assigning, not '==' :P/>
end
end
--[[ Testing code ]]--
local v = newVector4d()
print(v) --> 0,0,0,0
print(v:getX()) --> 0
v.moveX(2)
.moveY(5)
.moveZ(-10)
.setFacing(5)
print(v.getX()) --> 2
print(v) --> 2,5,-10,2