Difference between revisions of "Task complete (event)"

From ComputerCraft Wiki
Jump to: navigation, search
m (Fixed spelling mistake)
(Updated to ComputerCraft 1.71)
 
Line 27: Line 27:
 
|return3=[<nowiki/>{{type|string}} error] - if <var>success</var> is <var>false</var> then this is the error message saying why the task failed. Otherwise returned object 4 takes it's place, fallowed by returned object 5
 
|return3=[<nowiki/>{{type|string}} error] - if <var>success</var> is <var>false</var> then this is the error message saying why the task failed. Otherwise returned object 4 takes it's place, fallowed by returned object 5
 
|return4={{type|boolean}} commandResult - <var>true</var> if the command was executed successfully and returned positive value (a /testfor found a player), <var>false</var> if the command was not executed (invalid command or wrong arguments passed to the command) or it returned negative value (a /testfor didn't find a player)
 
|return4={{type|boolean}} commandResult - <var>true</var> if the command was executed successfully and returned positive value (a /testfor found a player), <var>false</var> if the command was not executed (invalid command or wrong arguments passed to the command) or it returned negative value (a /testfor didn't find a player)
|return5=[<nowiki/>{{type|string}} error] - if <var>commandResult</var> is <var>false</var> then this is the error message saying why the command failed
+
|return5={{type|table}} output - the output of the command as a numerically-indexed table
 
}}
 
}}
 
{{Example
 
{{Example
Line 53: Line 53:
 
   
 
   
 
  if errorMessage then
 
  if errorMessage then
   [[print]]( "An error has occurred: ", errorMessage )
+
   [[print]]( "An error has occurred: ", [[textutils.serialize]]( errorMessage ) )
 
  elseif arePlayersAround then
 
  elseif arePlayersAround then
 
   [[print]]( "There is one or more players around me." )
 
   [[print]]( "There is one or more players around me." )

Latest revision as of 16:15, 23 February 2015

Grid Modem.png  Event task_complete
Fired when an asynchronous task completes. Used by every commands API method which yields, including commands.execAsync(), but excluding commands.getBlockPosition(). Also used internally by every command block's methods.
Returned Object 1 number taskID - the ID number of the task
Returned Object 2 boolean success - true if the task completed without error, false otherwise
Returned Object 3 [string error] - if success is false then this is the error message saying why the task failed. Otherwise returned object 4 takes it's place, fallowed by returned object 5, 6, etc..
Returned Object 4 any param1, param2, ... - parameters, returned by the task


Grid paper.png  Example
Waits for any task to complete and prints it's ID, if it succeeded and if not succeeded the error message.
Code
local event, taskID, success, error = os.pullEvent( "task_complete" )

print( "Task ID: ", taskID )
print( "Task succeeded: ", success )

if not success then
  print( "Error: ", error )
end



commands.exec()/commands.execAsync()

Grid Modem.png  Event task_complete
Fired when commands.exec()/commands.execAsync() is executed. This is used internally by commands.exec() and it is not needed to be used by the user.
Returned Object 1 number taskID - the ID number of the task
Returned Object 2 boolean success - true if the task completed without error, false otherwise
Returned Object 3 [string error] - if success is false then this is the error message saying why the task failed. Otherwise returned object 4 takes it's place, fallowed by returned object 5
Returned Object 4 boolean commandResult - true if the command was executed successfully and returned positive value (a /testfor found a player), false if the command was not executed (invalid command or wrong arguments passed to the command) or it returned negative value (a /testfor didn't find a player)
Returned Object 5 table output - the output of the command as a numerically-indexed table


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
local errorMessage
while true do
  local event, id, success, executed, error = os.pullEvent( "task_complete" )
  
  if id == taskID then
    arePlayersAround = success and executed
    
    if not success then
      errorMessage = executed
    elseif not executed then
      errorMessage = error
    end
    
    break
  end
end

if errorMessage then
  print( "An error has occurred: ", textutils.serialize( errorMessage ) )
elseif arePlayersAround then
  print( "There is one or more players around me." )
else
  print( "There are no players around me." )
end



commands.list()

Grid Modem.png  Event task_complete
Fired when commands.list() completes. This is used internally by the function itself and it is not needed to be used by the user.
Returned Object 1 number taskID - the ID number of the task
Returned Object 2 boolean success - true if the task completed without error, false otherwise
Returned Object 3 [string error] - if success is false then this is the error message saying why the task failed. Otherwise returned object 4 takes it's place
Returned Object 4 table commands - a numerically indexed table filled with strings representing acceptable commands for commands.exec() / commands.execAsync()


commands.getBlockInfo()

Grid Modem.png  Event task_complete
Fired when commands.getBlockInfo() completes. This is used internally by the function itself and it is not needed to be used by the user.
Returned Object 1 number taskID - the ID number of the task
Returned Object 2 boolean success - true if the task completed without error, false otherwise
Returned Object 3 [string error] - if success is false then this is the error message saying why the task failed. Otherwise returned object 4 takes it's place
Returned Object 4 table blockInfo - a table containing information on the block at the specified world co-ordinate


commandBlock.getCommand()

Grid Modem.png  Event task_complete
Fired when commandBlock.getCommand() completes. This is used internally by the function itself and it is not needed to be used by the user.
Returned Object 1 number taskID - the ID number of the task
Returned Object 2 boolean success - true if the task completed without error, false otherwise
Returned Object 3 [string error] - if success is false then this is the error message saying why the task failed. Otherwise returned object 4 takes it's place
Returned Object 4 string command - the command in the command block


commandBlock.setCommand()


Grid Modem.png  Event task_complete
Fired when commandBlock.setCommand() completes. This is used internally by the function itself and it is not needed to be used by the user.
Returned Object 1 number taskID - the ID number of the task
Returned Object 2 boolean success - true if the task completed without error, false otherwise
Returned Object 3 [string error] - if success is false then this is the error message saying why the task failed.


commandBlock.runCommand()

Grid Modem.png  Event task_complete
Fired when commandBlock.runCommand() completes. This is used internally by the function itself and it is not needed to be used by the user.
Returned Object 1 number taskID - the ID number of the task
Returned Object 2 boolean success - true if the task completed without error, false otherwise
Returned Object 3 [string error] - if success is false then this is the error message saying why the task failed. Otherwise returned object 4 takes it's place, fallowed by returned object 5
Returned Object 4 boolean commandResult - true if the command was executed successfully and returned positive value (a /testfor found a player), false if the command was not executed (invalid command or wrong arguments passed to the command) or it returned negative value (a /testfor didn't find a player)
Returned Object 5 [string error] - if commandResult is false then this is the error message saying why the command failed