Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
Speak2Erase edited this page Mar 24, 2021 · 2 revisions

Fiber module

TODO: MORE EXAMPLES AND BETTER EXPLANATION

Fibers are primitives for implementing lightweight cooperative concurrency in ruby. They are mainly useful for executing scripts while keeping the event interpreter running. In order to properly implement Fibers, you will need to merge the changes to Interpreter 1 and 7 present in /Scripts/ to your xScripts.rxdata.

Fibers are NOT threads and do not function as you would expect.

You can define Fibers as you would creating an instance of any other class.

<name> = Fiber.new

Essentially, Fibers are a block of code that can be paused and resumed.

  • A Fiber will take priority over other code execution until finished, or paused.
  • Fibers can be paused using <Fiber>.yield.
  • Fibers can also be resumed on demand by using <Fiber>.resume.
  • Only one Fiber can execute at a time, but this can be circumnavigated with some clever trickery and the usage of <Fiber>.transfer.

A Fiber will stop execution once it runs out of code to run. When this happens, the Fiber "dies" and can no longer be resumed or called. You will get an error such as "FiberError: dead fiber called" when trying to resume a dead fiber.

As the Interpreter is set up currently it will try and resume a Fiber every frame. You are free to modify this if you please, however.

You can get more detailed information on fibers here.

Examples:


This script "bounces" the window whilst keeping event processing running.

def getWindowPos
  result = ModWindow.GetWindowPosition()
  @x = result[0]
  @y = result[1]
end

def createBounceFiber(direction, length, acceleration)
  Fiber = Fiber.new
  return Fiber.new do
           velocity = 0

           case direction

           when "u", "d"

             while velocity < length
               getWindowPos
               direction == "d" ? @y += velocity : @y -= velocity
               velocity += acceleration
               ModWindow.SetWindowPosition(@x, @y)
               Fiber.yield
             end

             while velocity > 0
               getWindowPos
               direction == "d" ? @y += velocity : @y -= velocity
               velocity -= acceleration
               ModWindow.SetWindowPosition(@x, @y)
               Fiber.yield
             end

             while velocity > -length
               getWindowPos
               direction == "d" ? @y += velocity : @y -= velocity
               velocity -= acceleration
               ModWindow.SetWindowPosition(@x, @y)
               Fiber.yield
             end

             while velocity < 0
               getWindowPos
               direction == "d" ? @y += velocity : @y -= velocity
               velocity += acceleration
               ModWindow.SetWindowPosition(@x, @y)
               Fiber.yield
             end #While

           when "r", "l"

             while velocity < length
               getWindowPos
               direction == "r" ? @x += velocity : @x -= velocity
               velocity += acceleration
               ModWindow.SetWindowPosition(@x, @y)
               Fiber.yield
             end

             while velocity > 0
               getWindowPos
               direction == "r" ? @x += velocity : @x -= velocity
               velocity -= acceleration
               ModWindow.SetWindowPosition(@x, @y)
               Fiber.yield
             end

             while velocity > -length
               getWindowPos
               direction == "r" ? @x += velocity : @x -= velocity
               velocity -= acceleration
               ModWindow.SetWindowPosition(@x, @y)
               Fiber.yield
             end

             while velocity < 0
               getWindowPos
               direction == "r" ? @x += velocity : @x -= velocity
               velocity += acceleration
               ModWindow.SetWindowPosition(@x, @y)
               Fiber.yield
             end #While

           else

             print("Invalid bounce direction")

           end #Case

         end #Return

end #Function

This function randomly plays sound effects at specified intervals.

  def self.mines(period)
    Fiber = Fiber.new
    return Fiber.new do
             while $game_switches[375] == true
               fr_to = Graphics.frame_count
               rand(0..1) == 1 ? fr_to += (period - rand(0..200)) : fr_to += (period + rand(0..200))
               while Graphics.frame_count < fr_to
                 Fiber.yield
               end # While 1
               Audio.se_play("Audio/AMB/mines" + rand(1..10).to_s + ".ogg", rand(70..100), rand(50..100))
               Fiber.yield
             end # While 2
           end # Fiber
  end # Method