A hand with a lua script

  • plypencil
    11th May 2012 Member 0 Permalink
    Okay so I have started to get into lua scripting, I have written this however I cannot seem to get it to work. I have a simulation which has oil containers which have a thin coal lining and a bmtl case, when the coal lining is broken and the oil touches the metal I want the oil to turn into coal.


    tpt.el.oil.color=0x333300
    tpt.el.oil.description="Modified Oil"
    math.randomseed(os.time())
    function oil_clog()
    if tpt.get_property("type",partx+1,party)=="BMTL" then
    tpt.set_property("type",partx,party, "COAL")
    end
    if tpt.get_property("type",partx-1,y)=="BMTL" then
    tpt.set_property("type",partx,party, "COAL")
    end
    if tpt.get_property("type",partx,party+1)=="BMTL" then
    tpt.set_property("type",partx,party, "COAL")
    end
    if tpt.get_property("type",partx,party-1)=="BMTL" then
    tpt.set_property("type",partx,party, "COAL")
    end
    end
    tpt.element_func(oil_clog,tpt.el.oil.id,1)


    It executes successfully because the colour of the oil changes, but it doesn't work. So I think I have misunderstood how the passing of variables works somewhere.

    Thanks for any help
  • jacob1
    11th May 2012 Developer 1 Permalink
    @plypencil (View Post)
    There are just a few minor problems with it. The function oil_clog gets passed these variables: i,x,y,surround_space,nt, so you should use that x and y instead of partx and party. Also, "BMTL" should be tpt.el.bmtl.id, and the "COAL" argument is second, not fourth. I fixed these for you:



    tpt.el.oil.color=0x333300
    tpt.el.oil.description="Modified Oil"
    math.randomseed(os.time())

    function oil_clog(i,x,y,surround_space,nt)
    local found_bmtl = false
    if tpt.get_property("type",x+1,y)==tpt.el.bmtl.id then
    found_bmtl = true
    end
    if tpt.get_property("type",x-1,y)==tpt.el.bmtl.id then
    found_bmtl = true
    end
    if tpt.get_property("type",x,y+1)==tpt.el.bmtl.id then
    found_bmtl = true
    end
    if tpt.get_property("type",x,y-1)==tpt.el.bmtl.id then
    found_bmtl = true
    end
    if found_bmtl then
    tpt.set_property("type","COAL",x,y)
    tpt.set_property("life",110,x,y)
    tpt.set_property("tmp",50,x,y)
    end
    end

    tpt.element_func(oil_clog,tpt.el.oil.id,1)


    The COAL burns instantly when created, i'll check how to fix it now.
    Edit: When created, COAL needs to have a life of 110 and a tmp of 50 to work right, I updated the code and changed it to make it smaller so I wouldn't have to write that 4 times.

    One thing that I do when I have problems with lua coding is putting this line: tpt.drawtext(100,100,"asdf") in places to see which lines of code are being run and to find which line has the problem. I also replace asdf with a variable if I want to see if it has the value it should.

    @mniip (View Post)
    Oops, I always forget to do that. When I wrote that massive lua script/mod, I didn't even know about local, so I had to go back and fix most of it.
  • mniip
    11th May 2012 Developer 1 Permalink
    found_bmtl = false

    local found_bmtl = false

    dont put garbage in globals
  • plypencil
    11th May 2012 Member 0 Permalink
    @jacob1 (View Post)
    Thanks, the code works great. And the text output will be very valuable for debugging in the future thank you.

    @mniip (View Post)
    I assume that is to stop the variable being accessible outside the function?