how to make my lua script do not lag

  • gs_115
    19th Jul 2020 Member 0 Permalink
    i recently discovered that you can reprogram your own materials on tpt, but when i created a material that used element_func and put too much of it, the game started to lag.
    how can i fix this problem? 
  • INFINITY-BOI
    19th Jul 2020 Banned 0 Permalink
    This post is hidden because the user is banned
  • gs_115
    19th Jul 2020 Member 0 Permalink

    :( if you really want to help me , here the script:

     

    local success , err = pcall(function()
    local DST2 = elements.allocate("NUCULAR","DST2")
    elements.element(elements.NUCULAR_PT_DST2, elements.element(elements.DEFAULT_PT_DEST))
    elements.property(elements.NUCULAR_PT_DST2, "Name", "DST2")
    elements.property(elements.NUCULAR_PT_DST2, "Description", "BOOOM")

    function dst2_update(i,x,y,s,n)
    local succes , error = pcall(function()
    tpt.set_property("type","dest",x-13,y-26,26,26)
    tpt.set_property("type","dmg",x-2.5,y-5,5,5)
    tpt.set_property("type","fire",x,y+8,10,10)
    tpt.set_property("type","sing",x,y-20,10,10)
    tpt.set_property("type","thdr",x-10,y-20,10,10)
    --tpt.set_property("temp",99999,x-20,y-20,40,40)
    sim.partCreate(3, x+1, y, "caus" , 10 , 10)

    tpt.set_property("temp",10000,x-50,y,100,100)
    end)
    if succes then

    else

    end
    end


    tpt.element_func(dst2_update, elements.NUCULAR_PT_DST2)
    end)
    if succes then
    else
    print(err)
    end

    Edited 2 times by gs_115. Last: 19th Jul 2020
  • INFINITY-BOI
    19th Jul 2020 Banned 0 Permalink
    This post is hidden because the user is banned
  • gs_115
    19th Jul 2020 Member 0 Permalink

    The script add an element called 'DST2' which create "DEST" , "DMG" , "FIRE" , "SING"  and heat

    The pcall function make the script can't have an error.

    But why you help me if you are not a scripter?

  • ReallyJustDont
    19th Jul 2020 Member 0 Permalink

    i use lua to mod tpt quite a bit (i'm making a mod), but i still can't understand this code. however, something seems fishy about it. lua update functions can cause lag when there is too much of the element on the screen (i found that out with my chocolate element which uses sim.neighbors to detect a stkm).

    what is local succes , error = pcall(function() supposed to do? because i haven't seen local used that way before

  • jacob1
    20th Jul 2020 Developer 0 Permalink
    pcall calls a function that may error, and returns the error string for you (allowing you to handle it). It's sometimes used in cases where you know it may error and don't care. That way it doesn't stop execution of your script. Also "local" just means the return values won't be put into global variables. Always put local on as much as you can.

    But in this case I don't think it should error, it implies there's a flaw in the script. The pcall shouldn't be needed here.

    tpt.set_property isn't the right way to create elements. You should use sim.partCreate instead. You're already doing that on one line.

    Removing the pcall may help with lag. Fix errors by validating coordinates before calling sim.partCreate. Besides that, try adding a call like "if math.random(1, 10) == 1" for a 10% chance of creating those elements. That way it will only make Lua api calls 10% of the time, slowing down the rate of API calls will make the element less laggy.
  • gs_115
    20th Jul 2020 Member 0 Permalink

    thank you ! :D 


    the fact of usingif math.random(1,10) works, my games no longer lag (well yes, it lags because the explosion produced in very powerful but) its no longer lagging due to the number of particles

     

    I used pcall (function () because there were errors which said that the coordinates were invalid when the dst2 was on the edge of the screen. I knew the pcall because on another games where I code we often have to use for actions which can have many ways to produce an error

  • gs_115
    7th Aug 2020 Member 0 Permalink

    oh... the games lag more when 1000 particles are placed but 10,000, I would like my games not to lag even if the screen is filled with the particle

  • jacob1
    7th Aug 2020 Developer 0 Permalink
    @gs_115 (View Post)
    That's unlikely. Lua elements are inherently laggy, it is hard to make them not laggy. The math.random was the best idea I had to stop that.

    I still think pcall might be laggy, but I don't know the real performance cost of it. You should check the coordinates are within bounds (0 to sim.XRES, 0 to sim.YRES) instead of the pcall.