class Rsrb::World::Task

A Task executed in the context of the game

task = Rsrb::World::Task.new(3, 3, @player) do |task, player|

player.inventory.remove(Rsrb::Item[995], 50000)
player.inventory.add(Rsrb::Item[4151], 1)

end

WORLD.schedule_player_task(task) #=> in 3 ticks, the world will remove 50000 gp from the player's inventory and add 1 abbyssal whip 3 times

Constants

Properties

A struct for holding property data. @param delay [Integer] the delay in ticks before the task should be executed. @param block [Proc] the block to execute during the task.

Attributes

delay[R]

@return [Integer] The delay in ticks before the task will execute.

id[R]

@return [Integer] The ID for the task.

operations[R]

@return [Array] Operations the task will perform.

properties[R]

@return [Struct] Properties for the task.

Public Class Methods

new(props = { delay: 0, count: 1, params: [self] }, &block) click to toggle source

Create a task with a specified delay. @param props [Hash] properties for this task. @param block [Proc] the code to execute during the task.

properties include:

  • delay [Integer] delay in ticks before operations should start executing.

  • exec_count [Integer] the number of times the block should execute

  • label [String, Symbol, Integer] the symbol for the task

Calls superclass method Rsrb::Internal::Types::Routine::new
# File lib/rsrb/world/task.rb, line 43
def initialize(props = { delay: 0, count: 1, params: [self] }, &block)
  init_loggers
  return unless block_given?

  @id = props[:id] || object_id
  @delay = props[:delay]
  @operations = []
  @observer = WORLD
  update(props[:delay], props[:count], block)
  super(props[:params])
end

Public Instance Methods

[](key) click to toggle source

Shorthand retrieval of properties.

# File lib/rsrb/world/task.rb, line 74
def [](key)
  @properties[key]
end
cancel() click to toggle source

Attempts to stop the task.

# File lib/rsrb/world/task.rb, line 66
def cancel
  return unless @execution

  @execution.running? ? @execution.shutdown : @execution.cancel
end
update(delay, exec_count, block) click to toggle source

Updates the task's execution block.

# File lib/rsrb/world/task.rb, line 57
def update(delay, exec_count, block)
  log!("Updating task: [#{@label}]:(#{@id})}") if WORLD.settings[:task_debug]
  @delay = delay
  exec_count.times { @operations << -> { block.call(@assets) } }
  log("Operations View: #{@operations}") if WORLD.settings[:task_debug]
end

Private Instance Methods

execute() click to toggle source

the execution function for the task.

# File lib/rsrb/world/task.rb, line 81
def execute
  Concurrent::ScheduledTask.execute(@delay * 0.600) do |task|
    @execution = task
    @operations.each(&:call)
  end.add_observer(@observer)
end
successful() click to toggle source

the callback function for the task. currently is used to add the bound world as the observer.

# File lib/rsrb/world/task.rb, line 90
def successful; end
unsuccessful() click to toggle source

the errback function for the task.

# File lib/rsrb/world/task.rb, line 93
def unsuccessful
  cancel
end