Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Broken Algorithm

Ruby:
   #--------------------------------------------------------------------------

  # * Frame Update

  #--------------------------------------------------------------------------

  def update

    super

    if self.active and @item_max > 0 and @index >= 0

      if Input.repeat?(Input::DOWN)

        if Input.press?(Input::SHIFT)

          $game_system.se_play($data_system.cursor_se)

          n = @index + page_item_max

          @index = n > @item_max - 1 ? @item_max - 1 : n

        elsif (@column_max == 1 and Input.trigger?(Input::DOWN)) or

            @index < @item_max - @column_max

          $game_system.se_play($data_system.cursor_se)

          unless @commands[(@index + @column_max) % @item_max] == nil

            @index = (@index + @column_max) % @item_max

          else

            @index = (@index + 1 + @column_max) % @item_max until

                @commands[(@index + @column_max) % @item_max] != nil

          end

        end

      end

      if Input.repeat?(Input::UP)

        if Input.press?(Input::SHIFT)

          $game_system.se_play($data_system.cursor_se)

          n = @index - page_item_max

          @index = n < 0 ? 0 : n

        elsif (@column_max == 1 and Input.trigger?(Input::UP)) or

            @index >= @column_max

          $game_system.se_play($data_system.cursor_se)

          unless @commands[(@index - @column_max + @item_max) % @item_max] == nil

            @index = (@index - @column_max + @item_max) % @item_max

          else

            @index = (@index - 1 - @column_max + @item_max) % @item_max until

                @commands[(@index - @column_max + @item_max) % @item_max] != nil

          end

        end

      end

      if Input.repeat?(Input::RIGHT)

        if @column_max >= 2 and @index < @item_max - 1

          $game_system.se_play($data_system.cursor_se)

          @index += 1

        end

      end

      if Input.repeat?(Input::LEFT)

        if @column_max >= 2 and @index > 0

          $game_system.se_play($data_system.cursor_se)

          @index -= 1

        end

      end

      if Input.repeat?(Input::R)

        if self.top_row + (self.page_row_max - 1) < (self.row_max - 1)

          $game_system.se_play($data_system.cursor_se)

          @index = [@index + self.page_item_max, @item_max - 1].min

          self.top_row += self.page_row_max

        end

      end

      if Input.repeat?(Input::L)

        if self.top_row > 0

          $game_system.se_play($data_system.cursor_se)

          @index = [@index - self.page_item_max, 0].max

          self.top_row -= self.page_row_max

        end

      end

    end

    if self.active and @help_window != nil

      update_help

    end

    update_cursor_rect

    update_scroll

    update_cursor

  end

The algorithm is a simple up and down cursor movement algorithm for a command menu. It's supposed to skip commands that are set to nil, but it doesn't seem to work properly and I have no idea what's wrong. Can someone please take a look? If you need more information, please let me know.
 
This is too much to look into at this hour, tbh... and even if I would, I'd probably confuse myself with XP stuff too much to be useful. Because I'm feeling pretty awesome now that I got mentioned namely, though... lemme try to help you out :p (don't try this in Script Requests please, I don't go there ;) )

The only thing I see quickly and without being able to test it is the until loop... it might be iterating until @item_max, but not any further, meaning if you have nil items up until the last item, and you'd theoretically have to go back to the 'start', your loop won't, as you only iterate upwards (@index+1).
Again, more or less just a guess... but it definately looks problematic to me. Other than that... what is working wrong actually? ^^
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top