Problem with detecting nearby particles

  • Sussy_Tiago
    4th Dec 2024 Member 0 Permalink

    This script

    function dtc(x2,y2,rangex,rangey)
        local tab= {}
        for x1=-rangex, rangexdo
            for y1=-rangey, rangeydo
                table.insert(tab,sim.partID(x2+x1,y2-y1))
            end
        end
        return tab
    end
     
    elements.property(elemid4,"Update",function(i,x,y,ss,nt)
        local tab = dtc(x,y,1,1)
        local temp = sim.partProperty(i,"temp")
        local oxid = false
        local ign = false
        for b=1, #tab do
            local typ = sim.partProperty(b,"type")
            if typ == (4) then
                ign=true
            end
            if typ == (61) then
                oxid=true
            end
        end
        tpt.log(ign,oxid)
        if temp > (300+273.15) then
            ign=true
        end
        if oxid == true and ign == true then
            sim.partChangeType(i,4)
        end
    end)
     
    Doesnt detect particles very well it always detects oxygen but not fire
    All the other elements on my modpack work fine with the detecting function however this one doesnt
    Edited once by Sussy_Tiago. Last: 4th Dec 2024
  • Jerehmia
    6th Dec 2024 Member 0 Permalink

    The Lua API already has functions to detect neighboring particles and either return them in a table with simulation.partNeighbors() or as an iterator with simulation.neighbors().

     

    So either you could change the first line of your Update() function to:

    local tab = sim.partNeighbors(x, y, 1, 1)

    Or you could delete the first line completely and change the for loop to:

    for b in sim.neighbors(x, y, 1, 1) do

    Edited 2 times by Jerehmia. Last: 6th Dec 2024