Detect when part is killed Lua

  • creator_of_fail
    13th May Member 0 Permalink

    So I was trying to make a test element that has an index in a lua list, but the problem is that when the element is destroyed it is supposed to remove its entry from the list. However, I could find no way of letting me do this on the wiki(or just didn't look closely enough) so I have come to ask for a function that does this or to request a new function.

  • Simon
    13th May Administrator 0 Permalink

    Use the "ChangeType" hook when registering your element. When the particle is killed, your ChangeType function will be called with a new_type of PT_NONE (0)

     

    https://powdertoy.co.uk/Wiki/W/Element_Properties.html#ChangeType

     

  • creator_of_fail
    14th May Member 0 Permalink

    NEVERMIND- I fixed it.

     

    For some reason it becomes very laggy when using ChangeType and used to even crash TPT(doesn't do that anymore)

    Here's my code:

    local listi = {}
    local listx = {}
    local listy = {}
    elem.property(test,"Create", function(i,x,y,s,n)
    table.insert(listi,i)
    table.insert(listx,x)
    table.insert(listy,y)
    end
    elem.property(test,"ChangeType", function(i,x,y,t,n)
    if n ~= test then
    print("works?")
    table.remove(listi,i);
    table.remove(listx,i);
    table.remove(listy,i);
    listi[i] = nil;
    listx[i] = nil;
    listy[i] = nil;
    print(listi[i])
    end
    end)

     

    in theory it should remove the index from the table but for whatever reason it does not and lags the game while creating BHOL(the update function that does this is here:

    if listi[i] then

     local xog = listx[i]
    local yog = listy[i]
    sim.partProperty(i,sim.FIELD_X,xog)
     sim.partProperty(i,sim.FIELD_Y,yog)
    end

    Edited 4 times by creator_of_fail. Last: 18th May