Page 1 of 1

function return after(some time)

PostPosted: Tue Mar 14, 2017 05:25
by juli
Is it possible to make a function return a value after some seconds?
I mean if i write something like this:
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
local str = "some string"
minetest.after(3.5, function(str)
    print(str)
end, str)
return str

Then the str is first returned and then printed, but is it possible to print it first and then return?

Re: function return after(some time)

PostPosted: Tue Mar 14, 2017 08:19
by rubenwardy
Why?

Re: function return after(some time)

PostPosted: Tue Mar 14, 2017 14:58
by juli
To make a function return something for my new mod (radio controll) but to put it also in a stack so that no lag arrives. Do u understand?

Re: function return after(some time)

PostPosted: Tue Mar 14, 2017 16:31
by Byakuren
No, and the cost of minetest.after is not large enough to matter unless you were calling it thousands of times per tick. I also don't see how allowing it to pause and return somehow would improve performance.

Re: function return after(some time)

PostPosted: Tue Mar 14, 2017 18:54
by juli
Look mesecons and digilines use after already to prevent lag, for example the function digiline_send in the mesecons-luacontroller, if u would remove it, then the controller would send in the right order his messages, but there would be more lag. If u don't then the second sended message is often the first which apperas at for example an other luacontroller.

Re: function return after(some time)

PostPosted: Tue Mar 14, 2017 20:32
by sofar
juli wrote:Is it possible to make a function return a value after some seconds?
I mean if i write something like this:
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
local str = "some string"
minetest.after(3.5, function(str)
    print(str)
end, str)
return str

Then the str is first returned and then printed, but is it possible to print it first and then return?


You're asking the wrong question, and so you're going to get the wrong answer.

Lua can't ever wait in minetest. If you would wait for 3.5 seconds in Lua, minetest would be frozen and nothing in the minetest world would change in that 3.5 seconds. And then after that 3.5 seconds, all of a sudden everything would "jump around" and be all weird.

What you should be asking is, "I have event A, and 3.5 seconds later I need to do something with that work in a certain way".

The problem is, I can't know what "something" is. And until you tell anyone, we're all left in the dark *guessing* what you're trying to do.

What you should try to do is learn about asynchronous execution and event-based programming. Minetest is a very event-driven API, and if you use it right, you don't need to wait (ever) and so your minetest server won't ever be halted. It's actually really hard to "halt" minetest for 3.5 seconds, for that very reason.

So, please take some time to elaborate and what it is that you want, instead of giving only a tiny bit of code that doesn't work.

Re: function return after(some time)

PostPosted: Tue Mar 14, 2017 20:59
by juli
ok u are right,
I wanted something like this: (tehere is no code yet ...)
1.A luacontroller executes a function with arg pos
2. at pos is an other block, and the function should return a value, based on
for example which node is under pos.

here an example:
luacontroller->invokes function get_data(pos)
at pos is a camera with nodedef.get_data(pos), and this returns a image
now the get_data of the luacontroller calls the get_data function of the camera_nodedef and returns also its return value.

I could do this without minetest.after(time,function) but a bad player could run this function 1000x so
that the server lags a lot.
I thinked that minetest.after would prevent this, right or why uses the luacontroller in real minetest.after?

Maybe it would be more simple to block the function if its invoked more then 5 times in a second or so?

The reason why i wanted o create such a function is, that a luacontroller-code become often very muddled, if there are much events of the same type, especially with digilines etc.

Re: function return after(some time)

PostPosted: Wed Mar 15, 2017 10:36
by bell07
You need to do the "steps to do after delay" that needs the result inside the after function.
Example from trash in smart_inventory (delete item after 1 second delay):
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
         on_put = function(inv, listname, index, stack, player)
            minetest.after(1, function(stack)
               inv:set_stack(listname, index, nil)
            end)
         end,

I could do this without minetest.after(time,function) but a bad player could run this function 1000x so
that the server lags a lot.
I thinked that minetest.after would prevent this, right or why uses the luacontroller in real minetest.after?

Maybe something like (untested)
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
local started = {}
function yourfunction(playername)
   if started[playername] then
      return
   end
   started[playername] = true
   minetest.after(3.5, function()
      do_your_delayed_things()
      started[playername] = nil
   end)
end

That will allow 1 call at the same time per user..

Re: function return after(some time)

PostPosted: Wed Mar 15, 2017 15:53
by sofar
If you want to `throttle` something, write a queue and throttle throughput through a queue mechanism.

local queue = {}

... table.insert(queue, some_data)

... -- process up to N items from the queue each server interval
minetest.register_globalstep(dtime)
for i = 1, max_per_step do
-- take one off the queue if you can, and do something with it.
end

Re: function return after(some time)

PostPosted: Wed Mar 15, 2017 16:13
by juli
Thank u i think i got it :)
I'll try to implement it