Difference between revisions of "Task complete (event)"

From ComputerCraft Wiki
Jump to: navigation, search
(Created page with "{{Event |name=task_complete |desc=Fired when an asynchronous task completes. Used by every commands API method which yields, including [[commands.execAsync]...")
 
(Updated to ComputerCraft 1.71)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{Event
 
{{Event
 
|name=task_complete
 
|name=task_complete
|desc=Fired when an asynchronous task completes. Used by every [[commands_(API)|commands API]] method which yields, including [[commands.execAsync]](), but excluding [[commands.getBlockPosition]](). Also used internally by every [[Command_Block_(API)|command block's method]].
+
|desc=Fired when an asynchronous task completes. Used by every [[commands_(API)|commands API]] method which yields, including [[commands.execAsync]](), but excluding [[commands.getBlockPosition]](). Also used internally by every [[Command_Block_(API)|command block's methods]].
 
|return1={{type|number}} taskID - the ID number of the task
 
|return1={{type|number}} taskID - the ID number of the task
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
Line 25: Line 25:
 
|return1={{type|number}} taskID - the ID number of the task
 
|return1={{type|number}} taskID - the ID number of the task
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
|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, 6, etc..
+
|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 36: Line 36:
 
  local arePlayersAround
 
  local arePlayersAround
 
  local errorMessage
 
  local errorMessage
  repeat
+
  while true do
 
   local event, id, success, executed, error = [[os.pullEvent]]( [[task_complete_(event)|"task_complete"]] )
 
   local event, id, success, executed, error = [[os.pullEvent]]( [[task_complete_(event)|"task_complete"]] )
 
 
  arePlayersAround = success and executed
 
 
    
 
    
 
   if id == taskID then
 
   if id == taskID then
 +
    arePlayersAround = success and executed
 +
   
 
     if not success then
 
     if not success then
 
       errorMessage = executed
 
       errorMessage = executed
Line 47: Line 47:
 
       errorMessage = error
 
       errorMessage = error
 
     end
 
     end
 +
   
 +
    break
 
   end
 
   end
  until id == taskID
+
  end
 
   
 
   
 
  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." )
Line 65: Line 67:
 
|return1={{type|number}} taskID - the ID number of the task
 
|return1={{type|number}} taskID - the ID number of the task
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
|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, 6, etc..
+
|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
 
|return4={{type|table}} commands - a numerically indexed table filled with strings representing acceptable commands for [[commands.exec]]() / [[commands.execAsync]]()
 
|return4={{type|table}} commands - a numerically indexed table filled with strings representing acceptable commands for [[commands.exec]]() / [[commands.execAsync]]()
 
}}
 
}}
Line 75: Line 77:
 
|return1={{type|number}} taskID - the ID number of the task
 
|return1={{type|number}} taskID - the ID number of the task
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
|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, 6, etc..
+
|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
 
|return4={{type|table}} blockInfo - a table containing information on the block at the specified world co-ordinate
 
|return4={{type|table}} blockInfo - a table containing information on the block at the specified world co-ordinate
 
}}
 
}}
Line 85: Line 87:
 
|return1={{type|number}} taskID - the ID number of the task
 
|return1={{type|number}} taskID - the ID number of the task
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
|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, 6, etc..
+
|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
 
|return4={{type|string}} command - the command in the command block
 
|return4={{type|string}} command - the command in the command block
 
}}
 
}}
Line 95: Line 97:
 
|return1={{type|number}} taskID - the ID number of the task
 
|return1={{type|number}} taskID - the ID number of the task
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
|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, 6, etc..
+
|return3=[<nowiki/>{{type|string}} error] - if <var>success</var> is <var>false</var> then this is the error message saying why the task failed.
 
}}
 
}}
  
Line 104: Line 106:
 
|return1={{type|number}} taskID - the ID number of the task
 
|return1={{type|number}} taskID - the ID number of the task
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
 
|return2={{type|boolean}} success - <var>true</var> if the task completed without error, <var>false</var> otherwise
|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, 6, etc..
+
|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=[<nowiki/>{{type|string}} error] - if <var>commandResult</var> is <var>false</var> then this is the error message saying why the command failed
 
}}
 
}}

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