Difference between revisions of "Commands.exec"
From ComputerCraft Wiki
MKlegoman357 (Talk | contribs) (Created page with "{{lowercase}} {{Function |name=commands.exec |args={{type|string}} command |returns={{type|boolean}} success [<nowiki/>, {{type|string}} error] |api=commands |addon=ComputerCr...") |
Bomb Bloke (Talk | contribs) (More info.) |
||
Line 7: | Line 7: | ||
|addon=ComputerCraft | |addon=ComputerCraft | ||
|desc=Available only to [[Command Computer]]s, executes the command <var>command</var>, [[os.pullEvent|yields]] until the result is determined, then returns it. If command executes successfully then it returns <var>true</var>, otherwise returns <var>false</var> + an error message. | |desc=Available only to [[Command Computer]]s, executes the command <var>command</var>, [[os.pullEvent|yields]] until the result is determined, then returns it. If command executes successfully then it returns <var>true</var>, otherwise returns <var>false</var> + an error message. | ||
+ | |||
+ | Accepts multiple parameters, in much the same manner as [[print]], so long as they can all be serialised into one string. For example, ''commands.exec("setblock 0 70 0 minecraft:stone 0")'' is treated the same as ''commands.exec("setblock", 0, 70, 0, "minecraft:stone", 0)''. If one of the parameters is a table, it will be automatically [[textutils.serializeJSON|textutils.serializeJSON()'d]] (handy for dynamically generating [http://minecraft.gamepedia.com/Tutorials/Command_NBT_Tags NBT representations]). | ||
+ | |||
+ | Most commands returned by [[commands.list]]() are also available as functions, such that ''commands.exec(command)'' could also be written as ''commands.command(par1, par2, ...)'' - eg, ''commands.exec("msg @p Hello world!")'' becomes ''commands.msg("@p Hello world!")''. | ||
|examples= | |examples= | ||
{{Example | {{Example |
Revision as of 23:23, 16 February 2015
Function commands.exec | |
Available only to Command Computers, executes the command command, yields until the result is determined, then returns it. If command executes successfully then it returns true, otherwise returns false + an error message.
Accepts multiple parameters, in much the same manner as print, so long as they can all be serialised into one string. For example, commands.exec("setblock 0 70 0 minecraft:stone 0") is treated the same as commands.exec("setblock", 0, 70, 0, "minecraft:stone", 0). If one of the parameters is a table, it will be automatically textutils.serializeJSON()'d (handy for dynamically generating NBT representations). Most commands returned by commands.list() are also available as functions, such that commands.exec(command) could also be written as commands.command(par1, par2, ...) - eg, commands.exec("msg @p Hello world!") becomes commands.msg("@p Hello world!"). | |
Syntax | commands.exec(string command) |
Returns | boolean success [, string error] |
Part of | ComputerCraft |
API | commands |
Examples
Example | |
Says 'Hello' to the player, closest to the Command Computer. | |
Code |
commands.exec( "say @p Hello" ) |
Example | |
Checks if there are any players around the Command Computer in 2 block radius. | |
Code |
local radius = 2 local arePlayersAround = commands.exec( "testfor @a[r=" .. radius .. "]") if arePlayersAround then print( "There is one or more players around me." ) else print( "There are no players around me." ) end |
Commands API Functions |
---|
commands.exec - commands.execAsync - commands.list - commands.getBlockPosition - commands.getBlockInfo - commands.getBlockInfos |