From cea99400370473f3e801ed2666689f31e2ba80f2 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 14 Nov 2018 03:31:37 +0000 Subject: [PATCH] Node Timers and ABMs: Create from ABMs chapter --- _en/basics/lua.md | 15 -------- _en/map/{abms.md => timers.md} | 64 ++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 29 deletions(-) rename _en/map/{abms.md => timers.md} (55%) diff --git a/_en/basics/lua.md b/_en/basics/lua.md index a833bee..00cd035 100644 --- a/_en/basics/lua.md +++ b/_en/basics/lua.md @@ -56,21 +56,6 @@ Other editors are available, of course. * Linux: Kate, Gedit, [Atom](http://atom.io/), [VS Code](https://code.visualstudio.com/) * OSX: [Atom](http://atom.io/), [VS Code](https://code.visualstudio.com/) -### Integrated Programming Environments - -IDEs allow you to debug code like a native application. -These are harder to set up than just a text editor. - -One such IDE is Eclipse with the Koneki Lua plugin: - -* Install Eclipse + Koneki. -* Create a new Lua project from existing source (specify Minetest's base directory). -* Follow instructions from Koneki wiki on how to do "Attach to remote Application" debugging (just a few steps). -* It is suggested to add those lines from wiki at beginning of builtin.lua. -* Start the debugger (set "Break on first line" in debugger configuration to see if it is working). -* Start Minetest. -* Enter the game to startup Lua. - ## Coding in Lua ### Program Flow diff --git a/_en/map/abms.md b/_en/map/timers.md similarity index 55% rename from _en/map/abms.md rename to _en/map/timers.md index 33bf554..2b2a079 100644 --- a/_en/map/abms.md +++ b/_en/map/timers.md @@ -1,34 +1,70 @@ --- -title: Active Block Modifiers +title: Node Timers and ABMs layout: default root: ../.. idx: 3.2 description: Learn how to make ABMs to change blocks. -redirect_from: /en/chapters/abms.html +redirect_from: +- /en/chapters/abms.html +- /en/map/abms.html --- ## Introduction -An Active Block Modifier (ABM) is a method of periodically running a -function on nodes matching specific criteria. -As the name implies, this only works on loaded MapBlocks. +Periodically running a function on certain nodes is a common task. +Minetest provides two methods of doing this: Active Block Modifiers (ABMs) and node timers. -ABMs are best suited for nodes which are frequently found in the world, +ABMs scan all loaded MapBlocks looking for nodes that match a criteria. +They are best suited for nodes which are frequently found in the world, such as grass. -ABMs have a high CPU overhead, as Minetest needs to scan all Active Blocks -to find matching nodes, but they have a low memory and storage overhead. +They have a high CPU overhead, but a low memory and storage overhead. -For nodes which are uncommon or already use metadata, such as furnaces +For nodes that are uncommon or already use metadata, such as furnaces and machines, node timers should be used instead. -Node timers don't involve searching all loaded nodes to find matches, +Node timers work by keeping track of pending timers in each MapBlock, and then +running them when they expire. +This means that timers don't need to search all loaded nodes to find matches, but instead require slightly more memory and storage for the tracking -of running timers. +of pending timers. - -* [Registering an ABM](#registering-an-abm) +* [Node Timers](#node-timers) +* [Active Block Modifiers](#active-block-modifiers) * [Your Turn](#your-turn) -## Registering an ABM +## Node Timers + +Node timers are directly tied to a single node. +When a node timer is up, the `on_timer` method in the node's definition table will +be called. +The method only takes a single parameter, the position of the node. + +```lua +minetest.register_node("autodoors:door_open", { + on_timer = function(pos) + minetest.set_node(pos, { name = "autodoors:door" }) + end +}) +``` + +You can manage node timers by obtaining a NodeTimerRef object. + +```lua +local timer = minetest.get_node_timer(pos) +timer:start(10.5) -- in seconds +``` + +You can also check the status or stop the timer: + +```lua +if timer:is_started() then + print("The timer is running, and has " .. timer:get_timeout() .. "s remaining!") + print(timer:get_elapsed() .. "s has elapsed.") +end + +timer:stop() +``` + +## Active Block Modifiers Alien grass, for the purposes of this chapter, is a type of grass which has a chance to appear near water.