Page 1 of 1

New function: minetest.after(time, func)

PostPosted: Mon Mar 26, 2012 22:48
by randomproof
Can I make a small suggestion?

Add a variable for passing params to the function

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.timers_to_add = {}
minetest.timers = {}
minetest.register_globalstep(function(dtime)
  for indes, timer in ipairs(minetest.timers_to_add) do
    table.insert(minetest.timers, timer)
  end
  minetest.timers_to_add = {}
  for index, timer in ipairs(minetest.timers) do
    timer.time = timer.time - dtime
    if timer.time <= 0 then
      timer.func(timer.params)
      minetest.timers[index] = nil
    end
  end
end)

function minetest.after(time, func, params)
  table.insert(minetest.timers_to_add, {time=time, func=func, params=params})
end

PostPosted: Mon Mar 26, 2012 23:33
by randomproof
I found another issue with this function. Had to change the line:
minetest.timers[index] = nil
to:
table.remove(minetest.timers,index)

and it worked.

here is the full code:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.timers_to_add = {}
minetest.timers = {}
minetest.register_globalstep(function(dtime)
  for _, timer in ipairs(minetest.timers_to_add) do
    table.insert(minetest.timers, timer)
  end
  minetest.timers_to_add = {}
  for index, timer in ipairs(minetest.timers) do
    timer.time_sec = timer.time_sec - dtime
    --print("Timer: "..index.."  "..timer.time_sec)
    if timer.time_sec <= 0 then
      timer.func(timer.params)
      --minetest.timers[index] = nil
      table.remove(minetest.timers,index)
    end
  end
  --print("Timer count: "..table.maxn(minetest.timers))
end)

function minetest.after(time_sec, func, params)
  table.insert(minetest.timers_to_add, {time_sec=time_sec, func=func, params=params})
end


By-the-way, here it the code I'm using this for now:
It fills the air holes you see in water. It needs to be run after the normal register_on_generated because of some issue with dungeons.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function CheckForWaterHoles(params)
    minp = params.minp
    maxp = params.maxp
    for x = minp.x,maxp.x do
    for z = minp.z,maxp.z do
    for y = minp.y,maxp.y do
        pos = {x=x, y=y, z=z}
        pos_up = {x=x, y=y+1, z=z}
        pos_down = {x=x, y=y-1, z=z}
        if minetest.env:get_node(pos).name == "air" and minetest.env:get_node(pos_up).name == "default:water_source" then
            minetest.env:add_node(pos, {name="default:water_source"})
            doing_next_check = true
        end
        if minetest.env:get_node(pos_down).name == "air" and minetest.env:get_node(pos).name == "default:water_source" then
            minetest.env:add_node(pos_down, {name="default:water_source"})
            doing_next_check = true
        end
    end
    end
    end
end

minetest.register_on_generated(
function(minp, maxp)
    minetest.after(10, CheckForWaterHoles, {minp=minp, maxp=maxp})
end)

PostPosted: Tue Mar 27, 2012 03:40
by jordan4ibanez
randomproof sometimes i think you're too smart for anyone else to understand

and that would be nice +1

PostPosted: Tue Mar 27, 2012 12:21
by sfan5
+1

PostPosted: Tue Mar 27, 2012 16:47
by kddekadenz
Thank you :)

PostPosted: Fri Apr 13, 2012 15:32
by LorenzoVulcan
It doesn't work to me...