Changing one of the methods in a LÖVE object
Posted: Wed Dec 12, 2018 1:50 pm
I made a deterministic maths library, and I want to make LÖVE's Transform objects use my sine and cosine algorithms for its rotate method. How can I do this?
I tried:
OK, so I can't index Transforms because Transforms are userdata values. Metatables?
I can't put metatables on userdata.
Help! What do I do? I thought maybe I could use FFI to replace the Transform class from LÖVE with a subclass that has its rotate method replaced (I could make my sine and cosine in C) but A: ffi.c.love doesn't exist and B: LÖVE is C++ and already compiled, so... yeah! Completely stumped.
Any help is appreciated! Thank you.
I tried:
Code: Select all
local rotate = function(self, angle)
-- TODO
print("It worked.")
end
local originalNewTransform = love.math.newTransform
function love.math.newTransform(...)
local new = originalNewTransform(...)
new.rotate = rotate
return new
end
local transform = love.math.newTransform()
transform:rotate(math.pi / 2)
Code: Select all
local rotate = function(self, angle)
-- TODO
print("It worked.")
end
local transformMetatable = {
__index = function(table, key)
return key == "rotate" and rotate or rawget(table, "key")
end
}
local originalNewTransform = love.math.newTransform
function love.math.newTransform(...)
return setmetatable(originalNewTransform(...), transformMetatable)
end
local transform = love.math.newTransform()
transform:rotate(math.pi / 2)
Help! What do I do? I thought maybe I could use FFI to replace the Transform class from LÖVE with a subclass that has its rotate method replaced (I could make my sine and cosine in C) but A: ffi.c.love doesn't exist and B: LÖVE is C++ and already compiled, so... yeah! Completely stumped.
Any help is appreciated! Thank you.