Skip to content

Examples

Joachim Stolberg edited this page Jul 1, 2018 · 35 revisions

THIS IS WORK IN PROGRESS

  • Simple Counter
  • Hello World
  • Monitoring Chest & Furnace
  • Stopwatch

Simple Counter

Very simple example with output on the Controller menu.

init() code:

a = 1

loop() code:

a = a + 1
$print("a = ", a)

end

Hello World

"Hello world" example with output on the Display. Hello world

init() code:

a = Array("Hello", "world", "of", "Minetest")

$clear_screen("0669")

for i,text in a.next() do
    $display("0669", i+2, text)
end

Monitoring Chest & Furnace

More realistic example to output Pusher states on the Display

init() code:

DISPLAY = "1234"
min = 0

loop() code:

-- call code every 60 sec
if ticks % 60 == 0 then
  -- output time in minutes
  min = min + 1
  $display(DISPLAY, 1, min, " min")

  -- Cactus chest overrun
  sts = $get_status("1034") -- read pusher status
  if sts == "blocked" then $display(DISPLAY, 2, "Cactus full") end

  -- Tree chest overrun
  sts = $get_status("1089")  -- read pusher status
  if sts == "blocked" then $display(DISPLAY, 3, "Tree full") end

  -- Furnace fuel empty
  sts = $get_status("2895")  -- read pusher status
  if sts == "standby" then $display(DISPLAY, 4, "Furnace fuel") end
end

Stopwatch

Stopwatch with highscore list, stored on the Server. You need the following nodes:

  • SaferLua Controller
  • SaferLua Battery
  • SaferLua Server
  • SmartLine Display
  • SmartLine Player Detector
  • SmartLine Button/Switch

You have to configure the Button/Switch as "switch" and store the number of the SaferLua Controller.

Stopwatch

init() code:

-- node numbers
DISPLAY = "0669"
SWITCH = "0670"
DETECTOR = "0671"
SERVER = "0674"

$events(true)
$loopcycle(0)
$clear_screen(DISPLAY)

-- read highscore data from Server, if available
-- otherwise generate a new 'Store'
highscore = $server_read(SERVER, "highscore") or Store()

loop() code:

if event then
    if $get_input(SWITCH) == "on" then
        time = $get_ms_time()
        name = $playerdetector(DETECTOR)
        $display(DISPLAY, 1, "running")
    elseif $get_input(SWITCH) == "off" then
        $display(DISPLAY, 1, "stopped")
        time = $get_ms_time()  - time
        $display(DISPLAY, 2, "Time: ", time)
        -- store the best (smallest) time only
        time = math.min(highscore.get(name) or 999999, time)
        highscore.set(name, time)
        keys = highscore.keys("up")
        -- output the top three with name and time
        for i, name in keys.next() do
            if i == 4 then break end
            $display(DISPLAY, i+3, name, ": ", highscore.get(name) / 1000.0)
        end
        -- store results on the Server
        $server_write(SERVER, "highscore", highscore)
    end
end