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.

Create a Shorter Form?

Well, Take me Again  :cry:.... I have a question about my Messy code:
Code:
      if @actor.styles.include?(Styles::Tela)
        bitmap = RPG::Cache.icon('001-Weapon01')
        self.contents.blt(0 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Armor_No_Style)
        self.contents.blt(0 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end
      
      if @actor.styles.include?(Styles::Cuero)
        bitmap = RPG::Cache.icon('001-Weapon01')
        self.contents.blt(40 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Armor_No_Style)
        self.contents.blt(40 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end
      
      if @actor.styles.include?(Styles::Malla)
        bitmap = RPG::Cache.icon('001-Weapon01')
        self.contents.blt(80 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Armor_No_Style)
        self.contents.blt(80 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end
      
      if @actor.styles.include?(Styles::Placas)
        bitmap = RPG::Cache.icon('001-Weapon01')
        self.contents.blt(120 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Armor_No_Style)
        self.contents.blt(120 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end
      
      if @actor.styles.include?(Styles::Escudo)
        bitmap = RPG::Cache.icon('001-Weapon01')
        self.contents.blt(160 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Armor_No_Style)
        self.contents.blt(160 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end

Well, The problem is very simple, it's possible to create a shorter form of that?, I don't know too much about loops and that type of things, but maybe you can help.

Thanks.
 

poccil

Sponsor

Here's a "short form".  This code wasn't tested, though.
Code:
styles=[Styles::Tela, Styles::Cuero, Styles::Malla, Styles::Placas, Styles::Escudo]
for i in 0...styles.length
 icon=(@actor.styles.include?(styles[i])) ? '001-Weapon01' : Styles::Armor_No_Style
 bitmap = RPG::Cache.icon(icon)
 self.contents.blt(i*40, 0, bitmap, Rect.new(0, 0, 24, 24))
end
 
Thanks!. that works perfectly, but I have another question about the same... :).

I have another code, but in this code, X and Y coordinates are modified:

Code:
      if @actor.styles.include?(Styles::Espada_Corta)
        bitmap = RPG::Cache.icon('MP_001-Sword02')
        self.contents.blt(0 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(0 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Espada_Larga)
        bitmap = RPG::Cache.icon('MP_001-Sword01')
        self.contents.blt(0 , 40, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(0 , 40, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Hacha_Ligera)
        bitmap = RPG::Cache.icon('MP_001-Axe02')
        self.contents.blt(0 , 80, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(0 , 80, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Hacha_Pesada)
        bitmap = RPG::Cache.icon('MP_001-BigAxe01')
        self.contents.blt(40 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(40 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Arcos)
        bitmap = RPG::Cache.icon('MP_001-ArcArrow01')
        self.contents.blt(40 , 40, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(40 , 40, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Ballestas)
        bitmap = RPG::Cache.icon('MP_001-Ballesta01')
        self.contents.blt(40 , 80, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(40 , 80, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Maza_Ligera)
        bitmap = RPG::Cache.icon('MP_001-Hammer01')
        self.contents.blt(80 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(80 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Maza_Pesada)
        bitmap = RPG::Cache.icon('007-Weapon07')
        self.contents.blt(80 , 40, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(80 , 40, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Varas)
        bitmap = RPG::Cache.icon('MP_001-WoodRod01')
        self.contents.blt(80 , 80, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(80 , 80, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Bastones)
        bitmap = RPG::Cache.icon('008-Weapon08')
        self.contents.blt(120 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(120 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Pistolas)
        bitmap = RPG::Cache.icon('006-Weapon06')
        self.contents.blt(120 , 40, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(120 , 40, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Puños)
        bitmap = RPG::Cache.icon('MP_001-Hand01')
        self.contents.blt(120 , 80, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(120 , 80, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Lanzas)
        bitmap = RPG::Cache.icon('MP_001-Spear01')
        self.contents.blt(160 , 0, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(160 , 0, bitmap, Rect.new(0, 0, 24, 24))
      end
      if @actor.styles.include?(Styles::Herramientas)
        bitmap = RPG::Cache.icon('MP_001-Pick02')
        self.contents.blt(160 , 40, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(160 , 40, bitmap, Rect.new(0, 0, 24, 24))
      end
     if @actor.styles.include?(Styles::Especiales)
        bitmap = RPG::Cache.icon('MP_001-Wine01')
        self.contents.blt(160 , 80, bitmap, Rect.new(0, 0, 24, 24))
      else
        bitmap = RPG::Cache.icon(Styles::Weapon_No_Style)
        self.contents.blt(160 , 80, bitmap, Rect.new(0, 0, 24, 24))
      end

It's too much, I want to know if there is a shorter form to define the X and the Y coordinates, like the same code above. (your code), It's possible to recreate that, but with this new code?

Thanks.
 
Code:
styles = [[Styles::Espada_Corta, 'MP_001-Sword02'], [Styles::Espada_Larga, 'MP_001-Sword01'], [Styles::Hacha_Ligera, 'MP_001-Axe02']]
x = 0
xa = 0
styles.each_index {|i|
  bitmap = @actor.styles.include?(styles[i][0]) ? RPG::Cache.icon(styles[i][1]) : RPG::Cache.icon(Styles::Armor_No_Style)
  self.contents.blt(x , i*40%120, bitmap, Rect.new(0, 0, 24, 24))
  x += 40 if xa > 2
  xa = (xa%3)+1
}
I haven't added all of them to "styles" so I'm leaving that to you. (you should be able to do that without help, but if you would need any I'll help ya)
And I haven't tested it but it should work.
 

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