Bonsai Posted January 9, 2016 Share Posted January 9, 2016 Hey there, I got a little problem using tables that contain functions as element data. According to the wiki you can't use functions for that. I have one file that contains something with this structure: function Test.new(name) local self = {}; self.element = createElement("Test", name) function self.destroy() destroyElement(self.element) outputDebugString("Destroyed") end setElementData(self.element, "object", self) return self end Then I have another file that that contains a function that is triggered at some point: function test() local testObject = getElementData(source, "object") testObject.destroy() end The problem is that I can't access the "destroy" function like this. Does anyone know a solution for this? The only thing I can think of is using a global table containing the object, which isn't that nice. Bonsai Link to comment
Revolt Posted January 9, 2016 Share Posted January 9, 2016 function newElement(name) local countID = 0 while getElementByID(name, countID) do countID = countID + 1 end local self = {} self.element = createElement("Test", name) self.destroy = "destroyElement(getElementByID('" .. name .. "', " .. tostring(countID) .."));outputChatBox('Destroyed it!')" setElementData(localPlayer, "object", self) return self end function testDestroy() local testObject = loadstring(getElementData(localPlayer, "object").destroy)() end newElement("test") testDestroy() Maybe something like this would suffice your needs? Link to comment
Bonsai Posted January 9, 2016 Author Share Posted January 9, 2016 That seems to be a working solution, but I'm not sure if its a good idea doing it like that. I also tried using string.dump to save the function and execute it later but MTA won't let me execute it: Bad usage @ 'loadstring' [argument 1 is invalid. Please re-compile at https://luac.multitheftauto.com/] Anyway, I might have to go with a global table then Bonsai Link to comment
Axel Posted January 11, 2016 Share Posted January 11, 2016 Why don't you use loadstring(str), and save the string with setElementData? Link to comment
Bonsai Posted January 11, 2016 Author Share Posted January 11, 2016 Meanwhile I found a much easier and better solution. I simply attached an event to that function and element. No idea why I didn't think about that earlier. Its working fine now. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now