Difference between revisions of "Commands.exec"
From ComputerCraft Wiki
MKlegoman357 (Talk | contribs) (Fixed return parameters, improved description) |
MKlegoman357 (Talk | contribs) (Almost forgot to mention that it exists as _G.exec in CC 1.71+) |
||
Line 9: | Line 9: | ||
<br/> | <br/> | ||
In ComputerCraft 1.71+ instead of just returning a {{type|boolean}} and an optional error message ({{type|string}}) this function also returns the output of the command in a numerically-indexed table, where each line of the output is put in a separate index. This output is the same as if you would run the command in chat. See example below for an example of this table.<br/> | In ComputerCraft 1.71+ instead of just returning a {{type|boolean}} and an optional error message ({{type|string}}) this function also returns the output of the command in a numerically-indexed table, where each line of the output is put in a separate index. This output is the same as if you would run the command in chat. See example below for an example of this table.<br/> | ||
+ | <br/> | ||
+ | As of ComputerCraft 1.71 this function is also available in the global environment ( <var>_G</var> ) as just <var>exec</var>.<br/> | ||
<br/> | <br/> | ||
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!")''. | 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!")''. | ||
Line 28: | Line 30: | ||
}} | }} | ||
{{Example | {{Example | ||
− | |desc=Prints the output after running the command "help". | + | |desc=Prints the output after running the command "help". ''Requires ComputerCraft 1.71+.'' |
|code=local success, output = '''commands.exec( "help" )''' | |code=local success, output = '''commands.exec( "help" )''' | ||
Revision as of 16:24, 23 February 2015
Function commands.exec | |
Available only to Command Computers, executes the specified MineCraft command, yields until the result is determined, then returns it. If command executes successfully then it returns true + the output of the command as a numerically-indexed table, otherwise returns false + an error message as as a numerically-indexed table. Compare commands.execAsync, which ignores the result and returns immediately, without yielding.
| |
Syntax | commands.exec(string command) |
Returns | boolean success, table output |
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 |
Example | |
Prints the output after running the command "help". Requires ComputerCraft 1.71+. | |
Code |
local success, output = commands.exec( "help" ) if success then print( textutils.serialize( output ) ) end |
Output | Example output:
{ "--- Showing help page 1 of 5 (/help <page>) ---", "/achievement give <stat_name> [player]", "/clear <player> [item] [data]", "/defaultgamemode <mode>", "/difficulty <new difficulty>", "/effect <player> <effect> [seconds] [amplifier]", "/enchant <player> <enchantment ID> [level]", "Use /forge <subcommand>. Subcommands are tps, track", } |
Commands API Functions |
---|
commands.exec - commands.execAsync - commands.list - commands.getBlockPosition - commands.getBlockInfo - commands.getBlockInfos |