Simplifying some logic

● ARCHIVED · READ-ONLY
Started by Rello 17 posts View original ↗
  1. Hey all,

    I solved my previous problem and now I was just wondering if someone can help me simplify some logic I'm using.

    I'm using

    if new_value2 > @actor.param(2) draw_picture("uparrow", 204-43, dy + 22 * 2 + 8) end if new_value2 < @actor.param(2) draw_picture("down arrow", 204-43, dy + 22 * 2 + 8) end if new_value2 == @actor.param(2) draw_picture("equalarrow", 204-43, dy + 22 * 2 + 8) endbut I'm sure there's a way to simplify this. Doing it this way would give me over 81 lines for all the things I'm displaying =_=;
  2. To keep it fairly readable to a newer scripter, you could do it like this:

    if new_value2 > @actor.param(2) draw_picture("uparrow", 204-43, dy + 22 * 2 + 8)elsif new_value2 < @actor.param(2) draw_picture("down arrow", 204-43, dy + 22 * 2 + 8)else draw_picture("equalarrow", 204-43, dy + 22 * 2 + 8)endOr if you want to minimize the number of lines, do it like this:
    Code:
    picture = new_value2 > @actor.param(2) ? "uparrow" : new_value2 < @actor.param(2) ? "down arrow" : "equalarrow"draw_picture(picture, 204-43, dy + 22 * 2 + 8)
    And you should really convert those numbers - do the calculation yourself, where possible, and put the result into the script, rather than having it do the calculation every time.204-43 is always going to be 161, so replace all instances of 204-43 with 161 instead.

    dy + 22 * 2 + 8 is always going to be dy + 52, do replace all instances of dy + 22 * 2 + 8 with dy + 52 instead.

    According to the normal order of operations, dy + 22 * 2 + 8 is NOT going to be calculated from left to right. The 22 * 2 will be done first, giving 44. Then dy and 8 will be added. And the order doesn't matter when you're adding, so you can do 44 + 8 to give 52.

    That would make your last command be this:

    Code:
    draw_picture(name, 161, dy + 52)
  3. Instead of copy pasting and hardcoding all your param ID's just create a method that takes a param ID as an argument.
  4. @Shaz, I do it that way fo quick edits and then I simplify when I'm done with the visual work. It's just something I do. :\

    @Tsuki, I wish I knew how- or even knew what that means... ^^;;
  5. param2 = @actor.param(2)bitmap = 'uparrow'bitmap = 'downarrow' if new_value2 < param2bitmap = 'equalarrow' if new_value2 == param2draw_picture(bitmap, 161, dy + 52)It's a good idea to make it calculate the parameter once instead of calling the param(2) method multiple times, since you would end up executing the following method without good reason:

    #-------------------------------------------------------------------------- # * Get Parameter #-------------------------------------------------------------------------- def param(param_id) value = param_base(param_id) + param_plus(param_id) value *= param_rate(param_id) * param_buff_rate(param_id) [[value, param_max(param_id)].min, param_min(param_id)].max.to_i endAlso:

    HelloRello said:
    but I'm sure there's a way to simplify this. Doing it this way would give me over 81 lines for all the things I'm displaying =_=;
    You could probably use an iterator, what are you trying to do?
  6. Breaking Owl, I'm completely re-doing the parameter comparison on EquipStatus. I don't like the pre-defined automated stuff by the core system or YEA, so I'm just redoing it all to better suit what and how I want to show this. Right now, this is basically me hard coding the arguments for every stat to respective show arrows.

    def draw_new_param_rello(dx, dy, param_id) @actor = $game_party.menu_actor self.contents.font.size = 20 new_value0 = @temp_actor.param(0) new_value1 = @temp_actor.param(1) new_value2 = @temp_actor.param(2) new_value3 = @temp_actor.param(3) new_value4 = @temp_actor.param(4) new_value5 = @temp_actor.param(5) new_value6 = @temp_actor.param(6) new_value7 = @temp_actor.param(7) draw_number(-2, dy + 22 * 0, contents.width-4, new_value0, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 1, contents.width-4, new_value1, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 2, contents.width-4, new_value2, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 3, contents.width-4, new_value3, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 4, contents.width-4, new_value4, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 5, contents.width-4, new_value5, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 6, contents.width-4, new_value6, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 7, contents.width-4, new_value7, 4, 2, Color.new(255,255,255,100)) reset_font_settings #using param(2) (strength) as a test for the displays since it's quickly done by changing weapons    param2 = @actor.param(2)    bitmap = 'uparrow'    bitmap = 'down arrow'  if new_value2 <  param2    bitmap = 'equalarrow' if new_value2 == param2    draw_picture(bitmap, 161, dy + 52)endI know this is horribly primitive work, and a definite eyesore, but I'm just doing what I know how to do.
  7. Oh, then you could do something like this:

    Code:
    def get_arrow(new_value, old_value)  return "uparrow" if new_value > old_value  return "downarrow" if new_value < old_value  return "equalarrow"end
    And in the method where you show stuff:
    Code:
    draw_picture(get_arrow(new_value2, @actor.param(2)), 204-43, dy + 22 * 2 + 8)
  8. Would I have to explicity define that for every stat new/old? or could I do something like:

    def get_arrow(@temp_actor.param(param_Id), @actor.param(param_id) return "uparrow" if @temp_actor.param(param_Id > @actor.param(param_id return "downarrow" if @temp_actor.param(param_Ide < @actor.param(param_id return "equalarrow"endand it would automatically know which things to compare?
  9. But isn't that method getting the parameter id as an argument? Why do you want to draw all the parameters each time the method is intended to draw one?

    def draw_new_param_rello(dx, dy, param_id)You see each parameter goes from 0 to 7?

    new_value0 = @temp_actor.param(0) new_value1 = @temp_actor.param(1) new_value2 = @temp_actor.param(2) new_value3 = @temp_actor.param(3) new_value4 = @temp_actor.param(4) new_value5 = @temp_actor.param(5) new_value6 = @temp_actor.param(6) new_value7 = @temp_actor.param(7)You can use that to your advantage since you use that numbers here;

    draw_number(-2, dy + 22 * 0, contents.width-4, new_value0, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 1, contents.width-4, new_value1, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 2, contents.width-4, new_value2, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 3, contents.width-4, new_value3, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 4, contents.width-4, new_value4, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 5, contents.width-4, new_value5, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 6, contents.width-4, new_value6, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 7, contents.width-4, new_value7, 4, 2, Color.new(255,255,255,100))So you can just say, param_id:

    Code:
     def draw_new_param_rello(dx, dy, param_id)    @actor = $game_party.menu_actor    self.contents.font.size = 20    new_value = @temp_actor.param(param_id)    draw_number(-2, dy + 22 * param_id, contents.width-4, new_value, 4, 2, Color.new(255,255,255,100))    reset_font_settings    #using param(2) (strength) as a test for the displays since it's quickly done by changing weapons    param2 = @actor.param(param_id)    bitmap = 'uparrow'    bitmap = 'down arrow'  if new_value2 <  param2    bitmap = 'equalarrow' if new_value2 == param2    draw_picture(bitmap, 161, dy + 52)  end
  10. If it legimitely that simple? *facepalm*

    Witht what you posted, I'm only seeing the HP (param0) being displayed. Unless I was supposed to do more than a copy and paste...
  11. Check where draw_new_param_rello is called, maybe you're calling it once instead of replacing the draw_new_param call in draw_item (Window_EquipStatus).
  12. I did a direct replace, but the previous call was for a mass (new_param.group) or something like that. I didn't like how it was all bundled under one thing that I couldn't personally control
  13. Try iterating in your method:

    Code:
       def draw_new_param_rello(dx, dy, param_id)    @actor = $game_party.menu_actor    (0..7).each { |param_id|       self.contents.font.size = 20      new_value = @temp_actor.param(param_id)      draw_number(-2, dy + 22 * param_id, contents.width-4, new_value, 4, 2, Color.new(255,255,255,100))      reset_font_settings    #using param(2) (strength) as a test for the displays since it's quickly done by changing weapons      param2 = @actor.param(param_id)      bitmap = 'uparrow'      bitmap = 'down arrow'  if new_value2 <  param2      bitmap = 'equalarrow' if new_value2 == param2      draw_picture(bitmap, 161, dy + 52)    }  end
  14. What is "iterating" exactly? Sorry for newbie questions.
  15. def draw_new_param_rello(dx, dy, param_id) @actor = $game_party.menu_actor.param contents.font.size = 20 new_value = @temp_actor.param(param_id) draw_number(dx, dy + 22 * param_id, contents.width-4, new_value, 4, 2, Color.new(255,255,255,100)) reset_font_settings bitamp = 'uparrow' bitamp = 'uparrow' if new_value < @actor.param(param_id) bitamp = 'uparrow' if new_value == @actor.param(param_id) draw_picture(bitmap, dx + 158, dy + 22 * param_id) endadded in the dx to the def cause it wasn't used before

    to go through the list you'll want to do something like this

    def draw_params for i in 0..7 draw_new_param_rello(-2, 0, i) end endthat says to start posting the stats at -2 (on x axis) and at the top of the window on the y axis. The for statement says going through numbers 0 to 7 (this is the iteration part). When we're on the 0 number.. the i = 0 and run the draw_new_param_rello with 0 as the param_id. Then run through with i = 1, then do it again and this time i = 2, etc till 7

    also I changed your bitmap thing to scale it's place with the param id as well. I can't play test since I don't have all your code or resources ;) so you might need to adjust some of the numbers.
  16. http://en.wikipedia.org/wiki/Iterator

    http://en.wikipedia.org/wiki/Iteration#Computing

    (0..7) is a range, any number from 0 to 7 (inclusive). So iterating in that case is doing something each time the "current number" changes, it will execute the code between brackets one time for 0, one time for 1, one time for 2, one time for 3, until it gets to 7 and stops since that is the end of the given range.

    As you can see, iterators are extremely useful.

    Here you have some Ruby resources.

    http://code.tutsplus.com/tutorials/ruby-for-newbies-iterators-and-blocks--net-17089

    https://thenewcircle.com/static/bookshelf/ruby_tutorial/iterators.html
  17. Thanks, everybody. I genuinely learned quite a lot today. :]