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.

About Arrays and @Cases

Status
Not open for further replies.
Well, I have a little question about a Reduce of code (to make my script more shorter), I use arrays defined from a @case in a menucommand, this is the code.

Code:
      # Branch by command window cursor position
      case @command_window.index
      
      when 0  # item
          if $game_switches[Profession::Interruptores[0]] == false
          $game_system.se_play($data_system.buzzer_se)
          @help_window.set_text(Profession::Desactivada , 1)
          return
        end
      $game_system.se_play($data_system.decision_se)
      @help_window.set_text(Profession::Description[0])
      when 1  
          if $game_switches[Profession::Interruptores[1]] == false
          $game_system.se_play($data_system.buzzer_se)
          @help_window.set_text(Profession::Desactivada , 1)
          return
        end
      $game_system.se_play($data_system.decision_se)
      @help_window.set_text(Profession::Description[1])
      when 2  
          if $game_switches[Profession::Interruptores[2]] == false
          $game_system.se_play($data_system.buzzer_se)
          @help_window.set_text(Profession::Desactivada , 1)
          return
        end
      $game_system.se_play($data_system.decision_se)
      @help_window.set_text(Profession::Description[2])
      when 3  
          if $game_switches[Profession::Interruptores[3]] == false
          $game_system.se_play($data_system.buzzer_se)
          @help_window.set_text(Profession::Desactivada , 1)
          return
        end
      $game_system.se_play($data_system.decision_se)
      @help_window.set_text(Profession::Description[3])
      when 4  
          if $game_switches[Profession::Interruptores[4]] == false
          $game_system.se_play($data_system.buzzer_se)
          @help_window.set_text(Profession::Desactivada , 1)
          return
        end
      $game_system.se_play($data_system.decision_se)
      @help_window.set_text(Profession::Description[4])
      when 5  
          if $game_switches[Profession::Interruptores[5]] == false
          $game_system.se_play($data_system.buzzer_se)
          @help_window.set_text(Profession::Desactivada , 1)
          return
        end
      $game_system.se_play($data_system.decision_se)
      @help_window.set_text(Profession::Description[5])
      when 6
          if $game_switches[Profession::Interruptores[6]] == false
          $game_system.se_play($data_system.buzzer_se)
          @help_window.set_text(Profession::Desactivada , 1)
          return
        end
      $game_system.se_play($data_system.decision_se)
      @help_window.set_text(Profession::Description[6])
      when 7
         if $game_switches[Profession::Interruptores[7]] == false
          $game_system.se_play($data_system.buzzer_se)
          @help_window.set_text(Profession::Desactivada , 1)
          return
        end
      $game_system.se_play($data_system.decision_se)
      @help_window.set_text(Profession::Description[7])
      end
      return
    end
  end
end

I have maked a Module that contains the arrays called 'Profession', and the  
 

khmp

Sponsor

In place of that case statement create a temporary variable and set it equal to the command window's index:

Code:
index = @command_window.index
if $game_switches[Profession::Interruptores[index]] == false
  $game_system.se_play($data_system.buzzer_se)
  @help_window.set_text(Profession::Desactivada , 1)
  return
end
$game_system.se_play($data_system.decision_se)
@help_window.set_text(Profession::Description[index])

Or you can just scrap that idea of creating and temporary variable and just place that command window index directly into the array index:

Code:
if $game_switches[Profession::Interruptores[@command_window.index]] == false
  $game_system.se_play($data_system.buzzer_se)
  @help_window.set_text(Profession::Desactivada , 1)
  return
end
$game_system.se_play($data_system.decision_se)
@help_window.set_text(Profession::Description[@command_window.index])

From the code above you can simplify it very easily since the behavior is very alike. It was a good thought to make the descriptions an array that matches up with the Interruptores indexes.

Good luck with it Mephisto! :thumb:
 
Thanks, that works very perfect, and I have another question about the reduce, its possible reduce this code withouth affect the work of the same? (I tryed but I think i take wrong way)

Code:
  if $game_switches[Profession::Interruptores[0]] == false
   @command_window.disable_item(0)
 end
  if $game_switches[Profession::Interruptores[1]] == false
   @command_window.disable_item(1)
 end
  if $game_switches[Profession::Interruptores[2]] == false
   @command_window.disable_item(2)
 end

Thanks in advance!:)
 

khmp

Sponsor

I'd just 'for loop' that code:
Code:
for i in 0..2
  if $game_switches[Profession::Interruptores[i]] == false
    @command_window.disable_item(i)
  end
end
If you want to access the amount of items in the command window instead of hard coding a number like 2. Just add this to somewhere in your project.
Code:
class Window_Command < Window_Selectable
  def command_count
    return @commands.size
  end
end
Then you could use this:
Code:
for i in 0...@command_window.command_count
  if $game_switches[Profession::Interruptores[i]] == false
    @command_window.disable_item(i)
  end
end
 

arev

Sponsor

This topic has been resolved. If mephisto or any other users have any questions or further problems regarding this topic, please create a new thread about them.

Thank you!
 
Status
Not open for further replies.

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