Module: Scheduler

Defined in:
scripts/00700 Ajout_PSDK/00100 Scheduler.rb,
scripts/00700 Ajout_PSDK/00101 EventTasks.rb,
scripts/99999 Scripts_a_haute_dependences/00100 PSDK_Task.rb

Overview

Module that allows you to schedule some tasks and run them at the right time

The Scheduler has a @tasks Hash that is organized the following way:

@tasks[reason][class] = [tasks]
reason is one of the following reasons :
  on_update: during Graphics.update
  on_scene_switch: before going outside of the #main function of the scene (if called)
  on_dispose: during the dispose process
  on_init: at the begining of #main before Graphics.transition
  on_warp_start: at the begining of player warp process (first action)
  on_warp_process: after the player has been teleported but before the states has changed
  on_warp_end: before the transition starts
  on_hour_update: When the current hour change (ex: refresh groups)
  on_getting_tileset_name: When the Map Engine search for the correct tileset name
  on_transition: When Graphics.transition is called
class is a Class Object related to the scene where the Scheduler starts

The Sheduler also has a @storage Hash that is used by the tasks to store informations

Defined Under Namespace

Modules: EventTasks Classes: MessageTask, ProcTask

Class Method Summary collapse

Class Method Details

.__add_task(reason, klass, task)

add a task (and sort them by priority)

Parameters:

  • reason (Symbol)

    the reason

  • klass (Class, :any)

    the class of the scene

  • task (ProcTask, MessageTask)

    the task to run



75
76
77
78
79
80
81
82
83
84
# File 'scripts/00700 Ajout_PSDK/00100 Scheduler.rb', line 75

def __add_task(reason, klass, task)
  task_hash = @tasks[reason]
  return unless task_hash # Bad reason

  klass = klass.to_s unless klass.is_a?(Symbol)
  task_array = task_hash[klass] || []
  task_hash[klass] = task_array
  task_array << task
  task_array.sort! { |a, b| a.priority <=> b.priority }
end

.__remove_task(reason, klass, name, priority)

Remove a task

Parameters:

  • reason (Symbol)

    the reason

  • klass (Class, :any)

    the class of the scene

  • name (String)

    the name that describe the task

  • priority (Integer)

    its priority



63
64
65
66
67
68
69
# File 'scripts/00700 Ajout_PSDK/00100 Scheduler.rb', line 63

def __remove_task(reason, klass, name, priority)
  task_array = @tasks.dig(reason, klass.is_a?(Symbol) ? klass : klass.to_s)
  return unless task_array

  priority = -priority
  task_array.delete_if { |obj| obj.priority == priority && obj.name == name }
end

.add_message(reason, klass, name, priority, object, *message)

Add a message task to the Scheduler

Parameters:

  • reason (Symbol)

    the reason

  • klass (Class, :any)

    the class of the scene

  • name (String)

    name that describe the task

  • priority (Integer)

    the priority of the task

  • object (Object)

    the object that receive the message

  • message (Array<Symbol, *args>)

    the message to send



154
155
156
# File 'scripts/00700 Ajout_PSDK/00100 Scheduler.rb', line 154

def add_message(reason, klass, name, priority, object, *message)
  __add_task(reason, klass, MessageTask.new(name, priority, object, message))
end

.add_proc(reason, klass, name, priority, proc_object = nil, &block)

Add a proc task to the Scheduler

Parameters:

  • reason (Symbol)

    the reason

  • klass (Class)

    the class of the scene

  • name (String)

    the name that describe the task

  • priority (Integer)

    the priority of the task

  • proc_object (Proc) (defaults to: nil)

    the Proc object of the task (kept for compatibility should not be defined)

  • block (Proc)

    the Proc object of the task



117
118
119
120
# File 'scripts/00700 Ajout_PSDK/00100 Scheduler.rb', line 117

def add_proc(reason, klass, name, priority, proc_object = nil, &block)
  proc_object = block if block
  __add_task(reason, klass, ProcTask.new(name, priority, proc_object))
end

.get_boot_sceneObject

Return the object of the Boot Scene (usually Scene_Title)

Returns:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'scripts/00700 Ajout_PSDK/00100 Scheduler.rb', line 160

def get_boot_scene
  if PARGV[:tags]
    ScriptLoader.load_tool('Editors/SystemTags')
    return Editors::SystemTags.new
  end
  return Yuki::WorldMapEditor if PARGV[:worldmap]
  return Yuki::AnimationEditor if PARGV[:"animation-editor"]

  test = PARGV[:test].to_s # ARGV.grep(/--test=./).first.to_s.gsub("--test=","")
  return Scene_Title.new if test.empty?

  test = "tests/#{test}.rb"
  return Tester.new(test) if File.exist?(test)

  return Scene_Title.new
end

.init

Initialize the Scheduler with no task and nothing in the storage



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'scripts/00700 Ajout_PSDK/00100 Scheduler.rb', line 23

def init
  @tasks = {
    on_update: {},
    on_scene_switch: {},
    on_dispose: {},
    on_init: {},
    on_warp_start: {},
    on_warp_process: {},
    on_warp_end: {},
    on_hour_update: {},
    on_getting_tileset_name: {},
    on_transition: {}
  }
  @storage = {}
end

.start(reason, klass = $scene.class)

Start tasks that are related to a specific reason

Parameters:

  • reason (Symbol)

    reason explained at the top of the page

  • klass (Class, :any) (defaults to: $scene.class)

    the class of the scene



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'scripts/00700 Ajout_PSDK/00100 Scheduler.rb', line 44

def start(reason, klass = $scene.class)
  task_hash = @tasks[reason]
  return unless task_hash # Bad reason

  if klass != :any
    start(reason, :any)
    klass = klass.to_s
  end
  task_array = task_hash[klass]
  return unless task_array # No task for this class

  task_array.each(&:start)
end