Help understanding Draw Icons (and a bit of math) (SOLVED)

Goooooood Morning / Timezone everyone!

I'm currently looking into drawing some custom icons on the Party Command Window, so figured I'd start my learning by breaking down how icons are drawn (by default) on the Window_BattleStatus window.

Here's what I've deciphered so far:
Ruby:
  #--------------------------------------------------------------------------
  # * Draw State and Buff/Debuff Icons
  #--------------------------------------------------------------------------
  def draw_actor_icons(actor, x, y, width = 96)
     # draw_actor_icons method, takes in arguments: actor, x, y, width
      # width by default is 96 if not overwritten

    icons = (actor.state_icons + actor.buff_icons)[0, width / 24]
     # local variable icons = actor's state_icons (array of icon numbers) +
      # actor's buff icons.
                               # But how does "[0, width / 24]" work?
                     
    icons.each_with_index {|n, i| draw_icon(n, x + 24 * i, y) }
     # for each value in the icons array that has an index number:
      # assign n to one then i to another, then draw the icon from the bitmap where:
       # icon_index = n, x = x + 24 * i, y = y
        #
  end
.
So, as you can (probably) see, I have a couple of questions / things that are stumping me:

1) Does anyone know what the "[0, width / 24]" bit of code is doing and HOW it is doing it?
i ) When I comment it out, if you have 3 or more state icons active... then the third icon onwards start appearing behind the HP numbers and gauge.
ii ) So I know it has something to do with limiting the amount of visible state icons to 2 (so they don't run behind the other graphics like the HP gauge), but, again, I'm not really sure what it's interacting with to do that. Any thoughts?
iii ) Oh! Because icons is now an array, that means the 0 in the [ ] represents INDEX 0 right? Silly me.
That means this bit of code is interacting with the array itself.
Still not sure what the "width / 24" bit is doing though, it doesn't seem to be changing the icon width when I mess with it, so is it to do with the array width somehow? Confused :/
-end of edit2-
.
2) When it comes to math, can anyone help me understand the "x + 24 * i" equation?
i ) I know it's to do with spacing the icons out a bit so they don't overlap eachother, but I don't really understand the math.
ii ) I mean, I get that it's x plus 24 and then multiplied by i (or is it x plus whatever 24 multiplied by i is?), but how does that translate into an x position (for the rect on the bitmap) that is equal to each individual icon's width starting point?
-end of edit1-
.
.
.
Thanks for any help!
🙂
Ruby allows you to use length for slice notation in the index brackets; in this case, it's taking from element 0 to however many will fit in the available width.

For example, if I did [1, 2, 3, 4, 5][0, 2] it would return [1, 2].

The x + 24 * i basically means that it'll start your icon draw coordinate at whatever x is, and then add 24 multiplied by the index. i starts at 0, meaning the first icon will just be drawn at x. Then for each subsequent icon, it'll be 24 pixels further right.
Ruby allows you to use length for slice notation in the index brackets; in this case, it's taking from element 0 to however many will fit in the available width.

For example, if I did [1, 2, 3, 4, 5][0, 2] it would return [1, 2].

The x + 24 * i basically means that it'll start your icon draw coordinate at whatever x is, and then add 24 multiplied by the index. i starts at 0, meaning the first icon will just be drawn at x. Then for each subsequent icon, it'll be 24 pixels further right.

Oh I see (I think), so because the method (for its appearance in Window BattleStatus) is called through these arguments:
Ruby:
  #--------------------------------------------------------------------------
  # * Draw Basic Area
  #--------------------------------------------------------------------------
  def draw_basic_area(rect, actor)
    draw_actor_name(actor, rect.x + 0, rect.y, 100)
    draw_actor_icons(actor, rect.x + 104, rect.y, rect.width - 104)
  end
.
What's happening is:
1) A rect is drawn to be filled with actor name and actor icons.
2) It is then filled with actor name using a different method I haven't looked at yet.

3) Then, the actor's icons are based on a starting x position of + 104 (presumably so they appear AFTER the actor's name).

4) That line also states that the actor's icons will be drawn within a rect width (aka length) of this initial rect's width - 104 pixels.

5) Then, moving onto our draw_actor_icons method:
Code:
    icons = (actor.state_icons + actor.buff_icons)[0, width / 24]
Starting with the first Icon in our new icons array, we draw them within that previously established rect width / 24 (as each icon is 24 pixels wide).
- Thus, when I comment this bit out, it doesn't divide the previously established rect width (aka length) by 24, and ends up showing more Icons further along the window (resulting in that previously mentioned icons-underneath-the-HP-gauge situation).

6) Moving onto the next line:

Code:
    icons.each_with_index {|n, i| draw_icon(n, x + 24 * i, y) }
For each icon index number, it takes turns assigning one of them "n" and the rest of them "i" when one is "n".
Then, to get the bitmap's rect x starting position, it multiplies x (aka 0) + 24 (an icon width size) by the number of "i"s it takes until it finds "n".
Then it grabs "n".
Then it repeats this with the next icon index number in the array (assigning that one "n" next).


~~~~~~

Is that how it works then?
That makes a logical kind of sense to me after reading what you wrote.
Hopefully that's it and I'm not just reading it entirely wrong lol.
You're almost there. each_with_index iterates through an array providing two parameters: the current element, and that element's index. In this case, each icon ID is represented by "n" and the index of the current iteration is stored in "i".
You're almost there. each_with_index iterates through an array providing two parameters: the current element, and that element's index. In this case, each icon ID is represented by "n" and the index of the current iteration is stored in "i".

Right... summary time! 😀

Does that mean that combining the following lines:
Ruby:
# This line from draw_actor_icons:
    icons.each_with_index {|n, i| draw_icon(n, x + 24 * i, y) }

# With this method:
def draw_icon(icon_index, x, y, enabled = true)

# And this line in this method:
     rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
.
.
Gets you this (for icon id 20 in an array of 1 (aka 0)):

n = icon id (aka 20)
i = array element (aka 0)

draw_icon method = draw_icon(20, 0 + 24 * 0 (aka 0), y, enabled = true)

rect = Rect.new with the following math:
x = 20's remainder when divided by 16 (aka 4) then multiplied by 24 (aka 96)
y = 20 / 16 (aka 1) then multiplied by 24 (aka 24)
width = 24
height = 24


?
And presumably that 16 the icon id (20 in my example) is divided by is 16 because there are 16 icons in a row on the Iconset bitmap/picture.

I think... lol.
Yep, that's it! Well done. 🙂

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/help-understanding-draw-icons-and-a-bit-of-math-solved.126567/