• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Our friends from the Johto Times are hosting a favorite Pokémon poll - and we'd love for you to participate! Click here for information on how to vote for your favorites!
  • Akari, Selene, Mint, Solana - which Pokémon protagonist is your favorite? Let us know by voting in our poll!
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Help with changing pages in a custom book script

Telemetius

Tele*
  • 256
    Posts
    10
    Years
    Hello there, I was hoping that someone could help me with a simple scene script.
    It's basically a book script entirely made of pictures, the hero will start from the first page(picture), pressing right will load the next page, pressing left will load the previous one.
    The problem I'm having is that the first page shows up no problem, but pressing left or right will crash the game.

    Here's the script:

    Code:
    class Bookascene
      
    def update
        pbUpdateSpriteHash(@sprites)
      end
      
    def pbStartScene
      page = 1
      @sprites={}
      @viewport=Viewport.new(0,0,Graphics.width,Graphics.height)
      @viewport.z=99999
      @sprites["background"]=IconSprite.new(0,0,@viewport)
      @sprites["background"].setBitmap("Graphics/Pictures/Book/Book template#{page}")
      pbFadeInAndShow(@sprites) { update }
    end
    
    def pbMain
        loop do
          Graphics.update
          Input.update
          self.update
          if Input.trigger?(Input::B)
            break
          elsif Input.trigger?(Input::LEFT) && page!=1
            pbSEPlay("Page turn sound effect",80)
            page -= 1 
          elsif Input.trigger?(Input::RIGHT) && page!=14
            pbSEPlay("Page turn sound effect",80)
            page += 1 
            elsif page == 14
            $game_switches[131] = true
          end
        end 
      end
      
       def pbEndScene
        pbFadeOutAndHide(@sprites) { update }
        pbDisposeSpriteHash(@sprites)
        @viewport.dispose
        Graphics.update
      end
    end
    
    
    class Booka
      def initialize(scene)
        @scene=scene
      end
    
      def pbStartScreen
        @scene.pbStartScene
        @scene.pbMain
        @scene.pbEndScene
      end
    end
    
    def pbBooka
      scene=Bookascene.new
      screen=Booka.new(scene)
      screen.pbStartScreen
    end
     
    Last edited by a moderator:
    Local variables like your "page" one cannot be called within multiple defs. This is coding 101. The "page" variable exists in pbStartScene, but it does not exist in pbMain. If you want to use a variable, within the same class but in multiple defs, you need @page, not just page. Also, your update loop has nothing in it that would update your actual graphic of the page you're on.

    P.S. I sincerely hope this indentation mess is because of the CODE tags on PC, and not because you actually have no indentation in your code. Keeping things tidy goes a long way.
     
    Since you haven't specified what type of error will appear, it's likely that the game will crash, because the "page" variable is a local variable and, therefore, can only be used within the function in which it was declared. I suggest, therefore, to transform this local variable in a semiglobal, adding @ before the variable's name, in any correspondence.
     
    Local variables like your "page" one cannot be called within multiple defs. This is coding 101. The "page" variable exists in pbStartScene, but it does not exist in pbMain. If you want to use a variable, within the same class but in multiple defs, you need @page, not just page. Also, your update loop has nothing in it that would update your actual graphic of the page you're on.

    P.S. I sincerely hope this indentation mess is because of the CODE tags on PC, and not because you actually have no indentation in your code. Keeping things tidy goes a long way.

    Haha, yes it's been messed up by the forum or by the spoiler tag but I can assure you it's correctly indented since I've been using Gemini to do it.

    Ok, I can't believe it was this simple, making the variable a semiglobal seems to have solved the crash issue (thanks you too Folle!) now what should I add to the update loop to show the changes?

    (Strangely
    Graphics.update
    Input.update
    pbUpdateSpriteHash(@sprites)
    self.update
    aren't working)
     
    now what should I add to the update loop to show the changes?

    (Strangely
    Graphics.update
    Input.update
    pbUpdateSpriteHash(@sprites)
    self.update
    aren't working)

    There is nothing strange about it. Yeah, you may be updating the sprites, but you're not changing their bitmap to match the new page. Your .setBitmap is only used once, and only when initially loading the script, which is no good for continuous page updates.
     
    There is nothing strange about it. Yeah, you may be updating the sprites, but you're not changing their bitmap to match the new page. Your .setBitmap is only used once, and only when initially loading the script, which is no good for continuous page updates.

    Working perfectly now ^^
     
    Haha, yes it's been messed up by the forum or by the spoiler tag but I can assure you it's correctly indented since I've been using Gemini to do it.
    Did you know that this forum has
    Code:
    [/COLOR] tags? They're like [COLOR=Blue][spoiler][/COLOR] tags, only they say [COLOR=Blue][code][/COLOR] and they're for code. Handy if you're posting code.
     
    Back
    Top