[Tutorial] Using Scenes

The "Scene" style workings of RPG maker are even easier to implement into Gosu.

First you need a scene:

  1. class Scene_Start
  2.   def update
  3.  
  4.   end
  5. end
Expand to see the code.


We don't need to update Graphics like in RPG Maker; nor do we need to run a loop (see below).

We need our game window:

  1. class Game < Window
  2.   def initialize
  3.     super(640, 480, false)
  4.     self.caption = "Scene Changing Demo"
  5.     $scene = Scene_Start.new
  6.   end
  7.   def update
  8.     $scene.update
  9.   end
  10. end
Expand to see the code.


The method initialize is called when the game is first opened. As such this can be used to call our initial scene (here, Scene_Start).

The method update is ran once per frame, automatically. Therefore, we can call our scene update methods here for convenience.

From before you'll note that super(x, y, fullscreen?) here makes the window 640x480, with no fullscreen mode.


Now all that remains is to call the script:

  1.  
  2. $game = Game.new
  3. $game.show
Expand to see the code.



Demo

  1. require 'rubygems'
  2. require 'gosu'
  3. include Gosu
  4.  
  5. class Scene_End
  6.   def update
  7.     $game.draw_text('Scene_End (Press Down to close)', 0, 0, 0)
  8.     if $game.button_down?(Button::KbDown)
  9.       exit
  10.     end
  11.   end
  12. end
  13.  
  14. class Scene_Start
  15.   def update
  16.     $game.draw_text('Scene_Start (Press Up to continue)', 0, 0, 0)
  17.     if $game.button_down?(Button::KbUp)
  18.       $scene = Scene_End.new
  19.     end
  20.   end
  21. end
  22.  
  23. class Game < Window
  24.   def initialize
  25.     super(640, 480, false)
  26.     self.caption = "Scene Changing Demo"
  27.     $scene = Scene_Start.new
  28.     @font = Font.new(self, "Times New Roman", 24)
  29.   end
  30.   def update
  31.     $scene.update
  32.   end
  33.   def draw_text(text, x, y, z)
  34.     @font.draw(text, x, y, z, 1, 1)
  35.   end
  36. end
  37.  
  38. $game = Game.new
  39. $game.show
Expand to see the code.



You'll noticed I added a draw_text method; this was just for ease and probably wasn't needed. Nonetheless, play around with the "demo" some and yeah.


This probably didn't need a tutorial; nonetheless if you have any problems with this post here.
You don't need to make the scene a global variable; it could be a simple class variable, and then if you want to access it from outside you could just have it attr_accessor'd or something.
Ah, I never thought of it like that. I'm trying too hard to replicate what's in RGSS.
button_down!!!!
<.<

the correct use is:
def button_down(id)
if id == #key
#something
end
end

Gosu have the Input in another way than rgss so you need:

  1. class Game < Window
  2.       def initialize
  3.         super(640, 480, false)
  4.         self.caption = "Scene Changing Demo"
  5.         $scene = Scene_Start.new
  6.       end
  7.  
  8.       def button_down(id)
  9.          $scene.button_down(id)
  10.       end
  11.  
  12.       def update
  13.         $scene.update
  14.       end
  15.  
  16.       def draw
  17.         $scene.draw
  18.       end
  19.    end
Expand to see the code.


You can draw things in the update, but by default Gosu calls update to do the operations and after calls draw() to draw the things, to have all more organizated.
You dont need to use it, but is called anyway.
Don't forget about button_up; button_down will be called every tick for as long as the button's pressed, whereas button_up will only be called when it's released, so it depends on what you want to do.

button_down? is useful if you want to check in the update method if the button is currently pressed, but you don't want to actually deal with it (i.e.: check for combos or whatever).
thats not true :3

you have 3 methods

button_down()

button_up()

window_object.button_down?(id)

the two button_down(?) are different.

button_down is called every time a key is pressed and only returns true one time

.button_down? is called all the ticks while you are pressing the button, thats the difference

see it like:

button_down() -> Input.trigger?()
button_down?() -> Input.press?()
Um...if you read the doc carefully, it says button_down is called every time before update, which itself called on every tick; so button_down will always be called on every tick if a button is pressed, regardless of whether its the same or not.

button_down? is never called by the engine; it is explicitly called by the user. Check the doc, the examples, etc; it doesn't make sense otherwise.
i checked all and I know how it works, i had some problems with these at the beginning, asked in #gosu (freenode) and in the forums, and now works perfect for me, im sure of what im saying.

button_down can be called in all the frames, but only return one true for key, only one time :3

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=67246