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.

[RESOLVED] For i in 0..(.)6

In RGSS there is the function:
Code:
for i in 0..6
which can do different things depending on the length of ellipsis. I have no idea what the base function does, let alone the ellipsis length. Could somebody fill me in on this?

Problem now resolved
 

poccil

Sponsor

That functionality is part of Ruby, not just RGSS.

The expression "0..6" specifies a range; in this case, "from 0 through 6", meaning that the number 6 is included (that is, 0 1 2 3 4 5 6).  If the expression were "0...6" instead (with three dots) it would read "from 0 up to but not including 6", meaning that the number 6 is excluded (that is, 0 1 2 3 4 5).

I hope this helps.
 
The 'for i' statement is listing all of the indexes in an array, or just a range of numbers (like poccil stated).

We'll take a good look at Window_MenuStatus from my CMS...

Code:
class Window_MenuStatus
  #...
  #-----------------------------------------------------------------------------
  # * Refresh Method
  #-----------------------------------------------------------------------------
  def refresh
    self.contents.clear
    @item_max = $game_party.actors.size
    for i in 0...@item_max
      @x = 64
      @y = (i * 120)
      actor = $game_party.actors[i]
      draw_actor_graphic(actor, @x - 40, @y + 80) unless CMS::Faces
      draw_actor_face(actor, @x - 65, @y)         if CMS::Faces
      draw_actor_name(actor, @x + 37, @y - 5)
      draw_actor_hp(actor, @x + 40, @y + 22)
      draw_actor_sp(actor, @x + 40, @y + 52)
      draw_actor_class(actor, @x + 240, @y - 5)
      draw_actor_level(actor, @x + 158, @y - 5)
      draw_actor_exp(actor, @x + 192, @y + 22)
      draw_actor_next(actor, @x + 192, @y + 52)
    end
  end
end

See what the code is doing? It's measuring the size of how many actors are in your party, then drawing stuff based on that multiplied by the designated coordinate the 'stat' is supposed to go.

So, lets say you're using faces of 120x120 in size, you'd have them placed at a certain spot on the window, it would be at 0 for the first window, 120 for the second, 360 for the 3rd...

This basically does the math for you

draw_actor_face(actor, 0, 120 * i)

That would be the same as...

draw_actor_face(actor, 0, 0)
draw_actor_face(actor, 0, 120)
draw_actor_face(actor, 0, 240)
draw_actor_face(actor, 0, 360)
 
So, say you wanted to display eight different images in a screen (like gym badges from Pokémon or something like that), yo would do:
Code:
def badges
for i in 0..8
$badges[i]
sprite=Bitmap.new("/Pictures/badge1.png")
sprite.new(64,64*i)
end

Or something along those lines?
 

khmp

Sponsor

[ code][ /code] Use those instead of [script][/script] minus the spaces in the code tags.

To get something like the 8 images to display in a geometric sequence you would need eight unique sprites. So don't use local variables. Instead use instance variables. Next problem is the range you are using to set up the loop. As poccil mentioned there's a difference in the amount of times the loop will run when using ".." or "...". Two dots signifies that the loop will include the last number in the range. Three dots means that the loop will exclude the last number in the range. Don't use globals for a situation like this either, "$badges". You would most likely use $game_switches to decide which badges are acquired or not.
Code:
0..8 # >> [0, 1, 2, 3, 4, 5, 6, 7, 8] 9 elements
0...8 # >> [0, 1, 2, 3, 4, 5, 6, 7] 8 elements

Code:
# The array of sprites that will hold the badges.
@sprites = []
# x - the x location of the sprite
# y - the y location of the sprite
# offset - the switch offset to start looking for badges acquired.
x, y, offset = 0, 0, 0

for i in 0...8
  # See if four badges were drawn yet
  if i / 4 >= 1
    x = 0
    y += 480 / 2
  end

  # Sprite initialization
  @sprites << Sprite.new
  badge_image = 'Graphics/Pictures/badge' + (i + 1).to_s
  @sprites[i].bitmap = Bitmap.new(badge_image)
  @sprites[i].visible = $game_switches[i + offset]
  @sprites[i].x = x
  @sprites[i].y = y
  x += 640 / 4
end

...

# Disposal
@sprites.each do |sprite|
  sprite.bitmap.dispose
  sprite.dispose
end

Alternatively you can also create a window that blankets the whole screen and blt each acquired badge onto it. There won't be the need to create eight separate sprites. Or worry about their disposal.

Code:
@badge_window = Window_Base.new(0, 0, 640, 480)
@badge_window.contents = Bitmap.new(608, 448)

# x - the x location of the sprite
# y - the y location of the sprite
# offset - the switch offset to start looking for badges acquired.
x, y, offset = 0, 0, 0

for i in 0...8
  # See if four badges were checked yet
  if i / 4 >= 1
    x = 0
    y += 480 / 2
  end

  if $game_switches[i + offset]
    badge_image = Bitmap.new('Graphics/Pictures/badge' + (i + 1).to_s)
    @badge_window.contents.blt(x, y, badge_image, badge_image.rect)
  end
  x += 640 / 4
end

...

#Disposal
@badge_window.dispose

I hope that helps. Let me know if you have any other questions.
 
Whoah! Thanks so much, khmp, that really, really helped. I had no clue before, and now you have filled me in on it :)

Just one more question: how would you specify how much the badge_image.rect takes out of the base image?

I really appreciate this, thanks.
 
On this line:
Code:
    @badge_window.contents.blt(x, y, badge_image, badge_image.rect)

If you want to modify itto only display part of your image, use:
Code:
    rect = Rect.new(x, y, width, height)
    @badge_window.contents.blt(x, y, badge_image, rect)

Replace the rect = line on the x, y, width and height with the position on your bitmap to take the image from.
 

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