function return after(some time)

User avatar
juli
Member
 
Posts: 109
Joined: Sat Jun 11, 2016 10:31
GitHub: cpdef
In-game: juli

function return after(some time)

by juli » Tue Mar 14, 2017 05:25

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?
Please tell me if i have written bad english.
Planets/Asteroids mod: https://forum.minetest.net/viewtopic.php?t=15933
servers where i'am:
mars-server
jungle-server
buildersworld
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: function return after(some time)

by rubenwardy » Tue Mar 14, 2017 08:19

Why?
 

User avatar
juli
Member
 
Posts: 109
Joined: Sat Jun 11, 2016 10:31
GitHub: cpdef
In-game: juli

Re: function return after(some time)

by juli » Tue Mar 14, 2017 14:58

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?
Please tell me if i have written bad english.
Planets/Asteroids mod: https://forum.minetest.net/viewtopic.php?t=15933
servers where i'am:
mars-server
jungle-server
buildersworld
 

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: function return after(some time)

by Byakuren » Tue Mar 14, 2017 16:31

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.
Every time a mod API is left undocumented, a koala dies.
 

User avatar
juli
Member
 
Posts: 109
Joined: Sat Jun 11, 2016 10:31
GitHub: cpdef
In-game: juli

Re: function return after(some time)

by juli » Tue Mar 14, 2017 18:54

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.
Please tell me if i have written bad english.
Planets/Asteroids mod: https://forum.minetest.net/viewtopic.php?t=15933
servers where i'am:
mars-server
jungle-server
buildersworld
 

sofar
Member
 
Posts: 781
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: function return after(some time)

by sofar » Tue Mar 14, 2017 20:32

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.
 

User avatar
juli
Member
 
Posts: 109
Joined: Sat Jun 11, 2016 10:31
GitHub: cpdef
In-game: juli

Re: function return after(some time)

by juli » Tue Mar 14, 2017 20:59

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.
Please tell me if i have written bad english.
Planets/Asteroids mod: https://forum.minetest.net/viewtopic.php?t=15933
servers where i'am:
mars-server
jungle-server
buildersworld
 

bell07
Member
 
Posts: 140
Joined: Sun Sep 04, 2016 15:15
GitHub: bell07

Re: function return after(some time)

by bell07 » Wed Mar 15, 2017 10:36

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..
 

sofar
Member
 
Posts: 781
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: function return after(some time)

by sofar » Wed Mar 15, 2017 15:53

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
 

User avatar
juli
Member
 
Posts: 109
Joined: Sat Jun 11, 2016 10:31
GitHub: cpdef
In-game: juli

Re: function return after(some time)

by juli » Wed Mar 15, 2017 16:13

Thank u i think i got it :)
I'll try to implement it
Please tell me if i have written bad english.
Planets/Asteroids mod: https://forum.minetest.net/viewtopic.php?t=15933
servers where i'am:
mars-server
jungle-server
buildersworld
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 8 guests

cron