[Solved] How do you run a lua script without a trigger?

User avatar
ratmix
Member
 
Posts: 148
Joined: Mon May 01, 2017 19:45
In-game: ratmix

[Solved] How do you run a lua script without a trigger?

by ratmix » Sat May 20, 2017 08:18

I am slowly learning how to write mods. One thing I cannot figure out is how to constantly scan for objects using minetest.get_objects_inside_radius. I want to perform an action if there is a player inside an area. Is there a way to persistently scan the area? I have used minetest.register_on_... to trigger other mods, but I have no idea how repeatedly trigger a script without an event. I tried in vain to figure out how the mesecon player detector does it.
Last edited by ratmix on Mon May 22, 2017 00:00, edited 1 time in total.
King of the Hill :: Modern Warfare Server RATMIX.COM:30000 | King of the Hill Community Site
 

User avatar
kaeza
Moderator
 
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
Location: Montevideo, Uruguay
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: How do you run a lua script without a trigger?

by kaeza » Sat May 20, 2017 11:55

Use `register_globalstep` or `after`. To get the players currently online, use `get_connected_players`, then do whatever you need:

Code: Select all
minetest.register_globalstep(function(dtime)
  for _, p in ipairs(minetest.get_connected_players()) do
    -- Do something with `p`.
  end
end)

`dtime` is the time since last step, in seconds. It's usually around the same value as `dedicated_server_step` (0.05 seconds or 50 milliseconds, IIRC). If you don't need to do the processing every step, you should consider using the `after` function, or tracking elapsed time yourself.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal
 

User avatar
ratmix
Member
 
Posts: 148
Joined: Mon May 01, 2017 19:45
In-game: ratmix

Re: How do you run a lua script without a trigger?

by ratmix » Sat May 20, 2017 18:39

Thanks kaeza. Do I use minetest.after together with register_globalstep to delay the script more than 0.05 seconds? Or can I use after by itself with some sort of loop? I think I can use a timer with globalstep as shown in the wiki to delay the script to have it run evey 5 seconds. Cheers
King of the Hill :: Modern Warfare Server RATMIX.COM:30000 | King of the Hill Community Site
 

Byakuren
Member
 
Posts: 816
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: How do you run a lua script without a trigger?

by Byakuren » Sun May 21, 2017 03:46

ratmix wrote:Thanks kaeza. Do I use minetest.after together with register_globalstep to delay the script more than 0.05 seconds? Or can I use after by itself with some sort of loop? I think I can use a timer with globalstep as shown in the wiki to delay the script to have it run evey 5 seconds. Cheers

You could use a counter in a globalstep callback to keep track of how much time has gone by, and only do something when the counter has gone high enough (then subtract the interval from the counter to reset it).

EDIT: Example:
Code: Select all
local interval = 5
local counter = 0
minetest.register_on_globalstep(function(dtime)
  counter = counter + dtime
  if counter >= interval then
    -- do stuff
    counter = counter - interval
  end
end)
Every time a mod API is left undocumented, a koala dies.
 

User avatar
ratmix
Member
 
Posts: 148
Joined: Mon May 01, 2017 19:45
In-game: ratmix

Re: How do you run a lua script without a trigger?

by ratmix » Sun May 21, 2017 06:01

Thank you Byakuren, that should work fine.
King of the Hill :: Modern Warfare Server RATMIX.COM:30000 | King of the Hill Community Site
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 2 guests