Jump to content

setElementData + tables + functions


Bonsai

Recommended Posts

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
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...