Difference between revisions of "Commands.execAsync"

From ComputerCraft Wiki
Jump to: navigation, search
(Removed misinformation. commands.exec/execAsync only allows to pass one string: the command. Only commands.<command>() where <command> is an actual MC command allows multiple parameters, including numbers and tables)
m
 
(2 intermediate revisions by one other user not shown)
Line 6: Line 6:
 
|api=commands
 
|api=commands
 
|addon=ComputerCraft
 
|addon=ComputerCraft
|desc=Available only to [[Command Computer]]s, orders the specified [http://minecraft.gamepedia.com/Commands MineCraft command] to be executed, but doesn't [[os.pullEvent|yield]]. A [[task_complete_(event)|"task_complete"]] event will be queued once the command has actually been executed. You can use the <var>taskID</var> returned by this function to identify the "task_complete" event, should you wish to pull it. Compare [[commands.exec]], which automatically yields until this event is thrown, then catches it and returns the results.
+
|desc=Available only to [[Command Computer]]s, orders the specified [https://minecraft.gamepedia.com/Commands MineCraft command] to be executed, but doesn't [[os.pullEvent|yield]]. A [[task_complete_(event)|"task_complete"]] event will be queued once the command has actually been executed. You can use the <var>taskID</var> returned by this function to identify the "task_complete" event, should you wish to pull it. Compare [[commands.exec]], which automatically yields until this event is thrown, then catches it and returns the results.<br><br>
  
As this function allows huge amounts of commands to be requested in a very short time, no more than 1000 orders will be processed per server tick (the rest being queued until the next tick, and so on).
+
This function allows huge amounts of commands to be requested in a very short time - much faster than Minecraft can actually perform them. Generating an excessive queue will cause your script to error out; to avoid problems, either perform periodic sleeps or measure incoming task_complete events (up to 256 events may exist within the event queue at a time; see [[os.pullEvent]]() for more info on the event system).
 
|examples=
 
|examples=
 
{{Example
 
{{Example

Latest revision as of 07:07, 4 August 2020


Grid Redstone.png  Function commands.execAsync
Available only to Command Computers, orders the specified MineCraft command to be executed, but doesn't yield. A "task_complete" event will be queued once the command has actually been executed. You can use the taskID returned by this function to identify the "task_complete" event, should you wish to pull it. Compare commands.exec, which automatically yields until this event is thrown, then catches it and returns the results.

This function allows huge amounts of commands to be requested in a very short time - much faster than Minecraft can actually perform them. Generating an excessive queue will cause your script to error out; to avoid problems, either perform periodic sleeps or measure incoming task_complete events (up to 256 events may exist within the event queue at a time; see os.pullEvent() for more info on the event system).
Syntax commands.execAsync(string command)
Returns number taskID
Part of ComputerCraft
API commands

Examples

Grid paper.png  Example
Says 'Hello' to the player, closest to the Command Computer.
Code
commands.execAsync( "say @p Hello" )



Grid paper.png  Example
Checks if there are any players around the Command Computer in 2 block radius.
Code
local radius = 2
local taskID = commands.execAsync( "testfor @a[r=" .. radius .. "]")

local arePlayersAround
repeat
  local event, id, success, executed, error = os.pullEvent( "task_complete" )
  
  arePlayersAround = success and executed
until id == taskID

if arePlayersAround then
  print( "There is one or more players around me." )
else
  print( "There are no players around me." )
end


Grid disk.png Commands API Functions
commands.exec - commands.execAsync - commands.list - commands.getBlockPosition - commands.getBlockInfo - commands.getBlockInfos