Mods/LuaCmd

From Minetest Wiki
< Mods(Redirected from LuaCmd)
Jump to: navigation, search
LuaCmd
Blank.png
A mod
Type Misc
Author Prestidigitator
Forum topic 9671
Source code GitHub
Technical name luacmd

This mod adds the useful debugging command /lua which executes its parameter string as a Lua chunk, as if it were being run in the body of a function inside some mod file. Errors are reported to the player via in-game chat, as is anything sent to the print() function.

Any player executing this command must have the new "lua" permission, as it is about the most dangerous thing you could allow a player to do. Also not recommended for public servers. Use at your own risk!

Contents

Special Variables

The following symbols are available to Lua statements executing with the /lua command. They do not pollute the global namespace seen by modules.

  • me — reference to current player object
  • myname — current player's name
  • here — current player's position as a printable vector
  • print(...) — function redirected to print all arguments to the player's chat window.

This allows commands such as the following:

/lua me:set_properties({visual_size={x=100, y=100}})
/lua print(myname)
/lua print(here)

to make you big, print the player's name, and print the player's location, respectively.

User-Local Context

"Global variables" (those not used in a "for" loop or specified via a "local" declaration) are kept in a special context local to the current user, until the user logs out or explicitly clears the context using the /luaclear command. To set true globals visible to mods and other players, use _G.var=... (of course, this can pollute the global namespace of mods, too, so use with caution). The _G table can also be used to get any globals hidden by specials and player-local variables.

Examples

Examples of Basic Command Execution

Entering the command:

/lua print("Hello self");

will result in the following output in the in-game chat console:

issued command: /lua print("Hello self");
Hello self

whereas entering the (erroneous) command:

/lua ^

will result in the following output:

issued command: /lua ^
Server -!- ERROR: [string "/lua command"]:1: unexpected symbol near '^'

A slightly more useful example might be the command:

/lua for _, player in ipairs(minetest.get_connected_players()) do print(player:get_player_name()); end

Examples Using User-Local Context

This shows how logic can be split between multiple command lines for simplicity:

/lua players = minetest.get_connected_players();
/lua print(type(players));
   -> table
/lua for _, p in ipairs(players) do print(p:get_player_name()); end
   -> prestidigitator
/luaclear
/lua print(type(players));
   -> nil

And here are two players John and Fred issuing commands with results showing their user-local variables are independent, but they share the true global (mod) environment:

John: /lua message = "Hello self";
John: /lua print(message);
   -> Hello self
Fred: /lua print(message);
Fred: /lua print(type(message));
   -> nil
John: /lua message = nil;
John /lua _G.message = "Hello anyone";
John /lua print(message);
   -> Hello anyone
Fred: /lua print(message);
   -> Hello anyone

Mod Details

Required Minetest Version: >=0.4.9 (likely to work in earlier versions, but not tested)

Dependencies: (none)

Commands:

  • /lua <luaStatement>
  • /luaclear

Privileges: lua

Change History

Version 1.0

  • Released 2014-07-04
  • First working version

Version 1.1

  • Release 2014-07-04
  • Variable 'me': reference to current player object
  • Variable 'myname': current player's name
  • Variable 'here': current player's position as a printable vector
  • Made the print(...) function a little more useful: converts args to strings

Version 1.2

  • Released 2014-07-04
  • Keeps "global" variables set by commands in a player-local context.
  • Prevents setting of special variables (e.g. me, myname, here, print).
  • Added /luaclear command to clear the player-local context.

Copyright and Licensing

All contents are the original creations of the mod author (prestidigitator).

Author: Prestidigitator

License: WTFPL (all content)

External Links