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.

Again with @Cases and Parameters.

Well, This Time I try but not get that I want. How I can reduce this piece of code to less lines without affect the work of the same?.

My code:

Code:
    case $game_variables[@parameters[0]]
 when 0..19
    self.contents.font.color = Color.disabled
    self.contents.draw_text(0, 33, 116, 32, Profession::Rank[0], 1)
 when 20..39
    self.contents.font.color = Color.yellow
    self.contents.draw_text(0, 33, 116, 32, Profession::Rank[1], 1)
 when 40..59
    self.contents.font.color = Color.crisis
    self.contents.draw_text(0, 33, 116, 32, Profession::Rank[2], 1)
 when 60..79
    self.contents.font.color = Color.orange
    self.contents.draw_text(0, 33, 116, 32, Profession::Rank[3], 1)
 when 80..5000
    self.contents.font.color = Color.knockout
    self.contents.draw_text(0, 33, 116, 32, Profession::Rank[4], 1)
  end
end

Well, sorry for ask to much sometimes I'm very annoying.':|

Thanks In advance! :thumb:
 

khmp

Sponsor

I don't believe you can do anything to shorten that code. Perhaps if there was a way to shorten the Ranges to a single number. Than yes but as it is right now I don't believe so. Sorry. ':|
 
There's almost always to shorten a code that long and repetitive.

Code:
color = {0..19 => Color.disabled, 20..39 => Color.yellow, 40..59 => Color.crisis,  60..79 => Color.orange, Color.knockout}
text  = {0..19 => Profession::Rank[0], 20..39 => Profession::Rank[1], 40..59 => Profession::Rank[2],  60..79 => Profession::Rank[3], 80..5000 => Profession::Rank[4]}
colors.each do |range, c|
  if range.include?($game_variables[@parameters[0]])
    self.contents.font.color = c
    self.contents.draw_text(0, 32, 116, 32, text[range], 1)
    break
  end
end
 

khmp

Sponsor

You and your hash magic. I need to start using those instead of arrays. I now recall the reason I don't use them. In C++ the equivalent is the STD::Map and the guy is a memory hog and not efficient in comparison to an associative dynamic array class you could write yourself. So I blame C++ for hindering my affection towards using hashes. :)
 
SephirothSpawn;327137":1oxbawwa said:
There's almost always to shorten a code that long and repetitive.

Code:
color = {0..19 => Color.disabled, 20..39 => Color.yellow, 40..59 => Color.crisis,  60..79 => Color.orange, Color.knockout}
text  = {0..19 => Profession::Rank[0], 20..39 => Profession::Rank[1], 40..59 => Profession::Rank[2],  60..79 => Profession::Rank[3], 80..5000 => Profession::Rank[4]}
colors.each do |range, c|
  if range.include?($game_variables[@parameters[0]])
    self.contents.font.color = c
    self.contents.draw_text(0, 32, 116, 32, text[range], 1)
    break
  end
end

O_o I never thought about it. It's a faster and cleaner way to do it.
 

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