如何優雅的研究 RGSS3 番外(二) 顯示文字資訊的視窗中的纖程

來源:互聯網
上載者:User

標籤:os   ar   amp   new   window   res   windows   ef   

Ruby 中的 Fiber 通常稱為纖程,是一種非搶佔式的多執行緒模式。

纖程不能在外部被終止,只能等待其主動讓出執行權。

RGSS3 在事件解譯器 Game_Interpreter 與顯示文字資訊的視窗 Window_Message 中都用到了纖程。


以 Window_Message 為例。

  #--------------------------------------------------------------------------  # ● 更新畫面  #--------------------------------------------------------------------------  def update    super    update_all_windows    update_back_sprite    update_fiber  end


在視窗更新時,對纖程進行了更新。

  #--------------------------------------------------------------------------  # ● 更新纖程  #--------------------------------------------------------------------------  def update_fiber    if @fiber      @fiber.resume    elsif $game_message.busy? && !$game_message.scroll_mode      @fiber = Fiber.new { fiber_main }      @fiber.resume    else      $game_message.visible = false    end  end  #----------


纖程的更新並不複雜,如果當前已經存在纖程,就繼續執行它,否則建立一個纖程。

@fiber = Fiber.new { fiber_main }
Fiber#new接受一個block,block裡就是這個Fiber將要執行的任務。

一個Fiber有三種狀態:Created、Running、Terminated,分別表示建立完成、執行、終止。

啟動Fiber是通過Fiber#resume方法,這個Fiber將進入Running狀態。


  #--------------------------------------------------------------------------  # ● 處理纖程的主邏輯  #--------------------------------------------------------------------------  def fiber_main    $game_message.visible = true    update_background    update_placement    loop do      process_all_text if $game_message.has_text?      process_input      $game_message.clear      @gold_window.close      Fiber.yield      break unless text_continue?    end    close_and_wait    $game_message.visible = false    @fiber = nil  end






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.