- 521
- 2
- 8
Vector3 = {};
function Vector3.__index:New(x,y,z)
local obj = Vector3;{}; -- Setcreate a new table (object)
-- self already exists as a local because of the index: syntactic sugar it points to the Vector3table BaseVector3 Classhere, sobut missingwould indexespoint onto thea child objectstable lookupif backVector3 tois Vector3inherited.
function Vector3:New setmetatable(x,yobj,z self); -- Set localthe metatable of the new object to self
self.__index = {};
self -- setmetatable(set the __index metamethod to self, so missing indexes on objects lookup back to self(Vector3 or inherited); instead of returning nil.
selfobj.x = x or 0;
selfobj.y = y or 0;
selfobj.z = z or 0;
return self;obj;
end
function Vector3:ToString() -- SampleMethod
return "(X: " .. self.x .. " Y: " .. self.y .. " Z: " .. self.z ..")";
end
function Vector3:__add(other) -- Sample Overload
local x = self.x + other.x;
local y = self.y + other.y;
local z = self.z + other.z;
return Vector3:New(x,y,z);
end
function Vector3:__div(other)
local distance = math.sqrt((other.x - self.x)^2 + (other.y - self.y)^2 + (other.z - self.z)^2);
return distance;
end
local Origin = Vector3:New(25,25,25); -- Object1
local Destin = Vector3:New(50,50,50); -- Object2
print("Vector Addition: " .. Origin:ToString() .. " + " .. Destin:ToString());
local Sum = Origin + Destin;
print(Sum:ToString());
print("Distance Between: " .. Origin:ToString() .. " and: " .. Destin:ToString());
local Distance = (Destin / Origin);
print(Distance);
Vector3 = {};
Vector3.__index = Vector3; -- Set the index to the Vector3 Base Class so missing indexes on the child objects lookup back to Vector3
function Vector3:New(x,y,z) local self = {};
setmetatable(self, Vector3);
self.x = x or 0;
self.y = y or 0;
self.z = z or 0;
return self;
end
function Vector3:ToString() -- SampleMethod
return "(X: " .. self.x .. " Y: " .. self.y .. " Z: " .. self.z ..")";
end
function Vector3:__add(other) -- Sample Overload
local x = self.x + other.x;
local y = self.y + other.y;
local z = self.z + other.z;
return Vector3:New(x,y,z);
end
function Vector3:__div(other)
local distance = math.sqrt((other.x - self.x)^2 + (other.y - self.y)^2 + (other.z - self.z)^2);
return distance;
end
local Origin = Vector3:New(25,25,25); -- Object1
local Destin = Vector3:New(50,50,50); -- Object2
print("Vector Addition: " .. Origin:ToString() .. " + " .. Destin:ToString());
local Sum = Origin + Destin;
print(Sum:ToString());
print("Distance Between: " .. Origin:ToString() .. " and: " .. Destin:ToString());
local Distance = (Destin / Origin);
print(Distance);
Vector3 = {};
function Vector3:New(x,y,z)
local obj = {}; -- create a new table (object)
-- self already exists as a local because of the : syntactic sugar it points to the table Vector3 here, but would point to a child table if Vector3 is inherited.
setmetatable(obj, self); -- Set the metatable of the new object to self
self.__index = self -- set the __index metamethod to self, so missing indexes on objects lookup back to self(Vector3 or inherited) instead of returning nil.
obj.x = x or 0;
obj.y = y or 0;
obj.z = z or 0;
return obj;
end
function Vector3:ToString() -- SampleMethod
return "(X: " .. self.x .. " Y: " .. self.y .. " Z: " .. self.z ..")";
end
function Vector3:__add(other) -- Sample Overload
local x = self.x + other.x;
local y = self.y + other.y;
local z = self.z + other.z;
return Vector3:New(x,y,z);
end
function Vector3:__div(other)
local distance = math.sqrt((other.x - self.x)^2 + (other.y - self.y)^2 + (other.z - self.z)^2);
return distance;
end
local Origin = Vector3:New(25,25,25); -- Object1
local Destin = Vector3:New(50,50,50); -- Object2
print("Vector Addition: " .. Origin:ToString() .. " + " .. Destin:ToString());
local Sum = Origin + Destin;
print(Sum:ToString());
print("Distance Between: " .. Origin:ToString() .. " and: " .. Destin:ToString());
local Distance = (Destin / Origin);
print(Distance);
As far as your code goes if its just a one time run in an isolated environment its fine for its purpose, but otherwise you should encapsulate all your vars/functions into one namespace or make sure they are all local to prevent pollution as in Lua all scripts share the environment and the default is global unless local is specified. The other reason is garbage collection. "any object stored in a global variable is not garbage for Lua, even if your program will never use it again
"-https://www.lua.org/pil/17.html
any object stored in a global variable is not garbage for Lua, even if your program will never use it again — https://www.lua.org/pil/17.html
As far as your code goes if its just a one time run in an isolated environment its fine for its purpose, but otherwise you should encapsulate all your vars/functions into one namespace or make sure they are all local to prevent pollution as in Lua all scripts share the environment and the default is global unless local is specified. The other reason is garbage collection. "any object stored in a global variable is not garbage for Lua, even if your program will never use it again
"-https://www.lua.org/pil/17.html
As far as your code goes if its just a one time run in an isolated environment its fine for its purpose, but otherwise you should encapsulate all your vars/functions into one namespace or make sure they are all local to prevent pollution as in Lua all scripts share the environment and the default is global unless local is specified. The other reason is garbage collection.
any object stored in a global variable is not garbage for Lua, even if your program will never use it again — https://www.lua.org/pil/17.html
You could organize it by using Lua's object oriented tricks to make a class, methods etc. I need to learn more math to cleanup the code you posted but here is a sample Vector3 class I wrote with operator overloading and a sample method. Additionally all the methods get packed into one "namespace" in the Vector3 table so there is less global pollution.
As far as your code goes if its just a one time run in an isolated environment its fine for its purpose, but otherwise you should encapsulate all your vars/functions into one namespace or make sure they are all local to prevent pollution as in Lua all scripts share the environment and the default is global unless local is specified. The other reason is garbage collection. "any object stored in a global variable is not garbage for Lua, even if your program will never use it again
"-https://www.lua.org/pil/17.html
Vector3 = {};
Vector3.__index = Vector3; -- Set the index to the Vector3 Base Class so missing indexes on the child objects lookup back to Vector3
function Vector3:New(x,y,z)
local self = {};
setmetatable(self, Vector3);
self.x = x or 0;
self.y = y or 0;
self.z = z or 0;
return self;
end
function Vector3:ToString() -- SampleMethod
return "(X: " .. self.x .. " Y: " .. self.y .. " Z: " .. self.z ..")";
end
function Vector3:__add(other) -- Sample Overload
local x = self.x + other.x;
local y = self.y + other.y;
local z = self.z + other.z;
return Vector3:New(x,y,z);
end
function Vector3:__div(other)
local distance = math.sqrt((other.x - self.x)^2 + (other.y - self.y)^2 + (other.z - self.z)^2);
return distance;
end
local Origin = Vector3:New(25,25,25); -- Object1
local Destin = Vector3:New(50,50,50); -- Object2
print("Vector Addition: " .. Origin:ToString() .. " + " .. Destin:ToString());
local Sum = Origin + Destin;
print(Sum:ToString());
print("Distance Between: " .. Origin:ToString() .. " and: " .. Destin:ToString());
local Distance = (Destin / Origin);
print(Distance);
You could organize it by using Lua's object oriented tricks to make a class, methods etc. I need to learn more math to cleanup the code you posted but here is a sample Vector3 class I wrote with operator overloading and a sample method. Additionally all the methods get packed into one "namespace" in the Vector3 table so there is less global pollution.
As far as your code goes if its just a one time run in an isolated environment its fine for its purpose, but otherwise you should encapsulate all your vars/functions into one namespace or make sure they are all local to prevent pollution as in Lua all scripts share the environment and the default is global unless local is specified. The other reason is garbage collection. any object stored in a global variable is not garbage for Lua, even if your program will never use it again
Vector3 = {};
Vector3.__index = Vector3; -- Set the index to the Vector3 Base Class so missing indexes on the child objects lookup back to Vector3
function Vector3:New(x,y,z)
local self = {};
setmetatable(self, Vector3);
self.x = x or 0;
self.y = y or 0;
self.z = z or 0;
return self;
end
function Vector3:ToString() -- SampleMethod
return "(X: " .. self.x .. " Y: " .. self.y .. " Z: " .. self.z ..")";
end
function Vector3:__add(other) -- Sample Overload
local x = self.x + other.x;
local y = self.y + other.y;
local z = self.z + other.z;
return Vector3:New(x,y,z);
end
function Vector3:__div(other)
local distance = math.sqrt((other.x - self.x)^2 + (other.y - self.y)^2 + (other.z - self.z)^2);
return distance;
end
local Origin = Vector3:New(25,25,25); -- Object1
local Destin = Vector3:New(50,50,50); -- Object2
print("Vector Addition: " .. Origin:ToString() .. " + " .. Destin:ToString());
local Sum = Origin + Destin;
print(Sum:ToString());
print("Distance Between: " .. Origin:ToString() .. " and: " .. Destin:ToString());
local Distance = (Destin / Origin);
print(Distance);
You could organize it by using Lua's object oriented tricks to make a class, methods etc. I need to learn more math to cleanup the code you posted but here is a sample Vector3 class I wrote with operator overloading and a sample method. Additionally all the methods get packed into one "namespace" in the Vector3 table so there is less global pollution.
As far as your code goes if its just a one time run in an isolated environment its fine for its purpose, but otherwise you should encapsulate all your vars/functions into one namespace or make sure they are all local to prevent pollution as in Lua all scripts share the environment and the default is global unless local is specified. The other reason is garbage collection. "any object stored in a global variable is not garbage for Lua, even if your program will never use it again
"-https://www.lua.org/pil/17.html
Vector3 = {};
Vector3.__index = Vector3; -- Set the index to the Vector3 Base Class so missing indexes on the child objects lookup back to Vector3
function Vector3:New(x,y,z)
local self = {};
setmetatable(self, Vector3);
self.x = x or 0;
self.y = y or 0;
self.z = z or 0;
return self;
end
function Vector3:ToString() -- SampleMethod
return "(X: " .. self.x .. " Y: " .. self.y .. " Z: " .. self.z ..")";
end
function Vector3:__add(other) -- Sample Overload
local x = self.x + other.x;
local y = self.y + other.y;
local z = self.z + other.z;
return Vector3:New(x,y,z);
end
function Vector3:__div(other)
local distance = math.sqrt((other.x - self.x)^2 + (other.y - self.y)^2 + (other.z - self.z)^2);
return distance;
end
local Origin = Vector3:New(25,25,25); -- Object1
local Destin = Vector3:New(50,50,50); -- Object2
print("Vector Addition: " .. Origin:ToString() .. " + " .. Destin:ToString());
local Sum = Origin + Destin;
print(Sum:ToString());
print("Distance Between: " .. Origin:ToString() .. " and: " .. Destin:ToString());
local Distance = (Destin / Origin);
print(Distance);