Last active
December 21, 2023 05:14
-
-
Save alfredbaudisch/90598e71283cdfc4a8fdb8fdb8432e28 to your computer and use it in GitHub Desktop.
Lua operator overloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Helpers | |
function v2_add() | |
-- foo.. | |
end | |
-- Vector | |
local vector = {} | |
local vector_meta = {} | |
function vector:new(x, y) | |
local _ = {x=x or 0,y=y or 0} | |
return setmetatable(_, vector_meta) | |
end | |
vector_meta.__add = function(a, b) return v2_add(a,b, vector:new(a,b)) end | |
-- more: | |
-- vector_meta.__sub | |
-- vector_meta.__tostring | |
-- etc... | |
local vectorPlayer = vector:new(10,10) | |
local vectorEnemy = vector:new(2,2) | |
local added = vectorPlayer + vectorEnemy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment