For more explanation, please read the comments I made in the scripts as well;
I am sorry, but I don't really understand how should I change my code. Could you please provide me a solution for this?
So I have a Highlight table, each time this table gets called, a new Highlight is being created for the given element variable (I would like to make it function like the default MTA OOP stuff, like Vehicle(model, position) instead of Vehicle.new(model, position), so I have this setup here:
local CREATED_HIGHLIGHTS = {}
local Highlight = {}
Highlight.__index = Highlight
setmetatable(Highlight, {__call = function(_,...) return Highlight.new(...) end})
function Highlight.new(element, style, color, flashSpeed, intensityOrThickness)
if not isElement(element) then return false end
if CREATED_HIGHLIGHTS[element] then return false end
local data = setmetatable({}, Highlight) -- this has something else to do with it I guess
-- set variables
-- data.element = element
-- and so on
return data
end
And I have methods for it like Highlight:update() and Highlight:destroy(), the way I did it for example:
function Highlight:destroy()
if not CREATED_HIGHLIGHTS[self.element] then return false end
if self.shader then self.shader:destroy() end
CREATED_HIGHLIGHTS[self.element] = nil
return true
end
When I create a new highlight for the localPlayer, like:
local playerHighlight = Highlight(localPlayer, "outline", {255,0,0,255}, 0, 1)
Now everything works fine above, highlight gets created, I can access the data with playerHighlight.thickness and such, BUT
I can then use the playerHighlight table (which should contain only data, like style, thickness etc) to create a new highlight on a totally different element like:
local dumpster = Object(1337, localPlayer.position + Vector3(0,3,1))
print(playerHighlight.new(dumpster, "fresnel", {100,200,255,255}, 2, 0.5)) -- returns table, I don't want to use .new here
-- how can I make it return nil, or avoid calling of .new, but keep the :update() and :destroy() methods
-- I would like playerHighlight to contain only the data given before
Now I understand that the data table inherits from Highlight table at the Highlight.new function, so to the data table the methods get passed as well, but how should I change it so it won't inherit all the functions, I would like to access actual data and only methods with colon (:update, :destroy), not with dot (.new) . So my guess is that this line should be changed to something else instead of Highlight, I imagine
local data = setmetatable({}, Highlight) -- change Highlight to what? Am I missing the creation of another table?
Sorry if it looks like I am asking the same question twice, I am not ignoring your reply but I don't understand your explanation @IIYAMA, I mean I don't understand how should I implement it in this case.