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.

Pictures and Arrays

Right my problem is, I want to load either the filenames or the actual files from a directory like "Graphics/Gallery/" . The files would all be pictures.

However I need to load it into an array eg.
Code:
PICS = []
PICS.load_stuf_here

If there's a way of just loading the filenames, that would help a lot.
However, if there isn't, I need a way of getting the filenames from the loaded pictures... if that makes sense?


Anyway thanks in advance, hope someone can help xD
 

khmp

Sponsor

You need a way of determining what graphics are within a given directory. Like you would never know how many or which pictures were contained within "Gallery"? The array wouldn't be hardcoded. It would be interpreted at run time what graphics were in that directory?

Code:
PIC_FILENAMES = Dir.glob('/Graphics/Gallery/*.{jpg,jpeg,bmp,png}')

Like that will give you an Array of every jpg, bmp, and png in that directory.
 
Aah, right...

Thankyou, just one thing will that load the filenames, or the files? xD

:EDIT:
Well I'm not entirely sure what it's done, but now one of my arrays is counted as a nil:class.


Right I'll put the coding that I've got that which needs this...

Code:
   module Pictures
     NAMES = Dir.glob('/Graphics/Gallery/*.{jpg,jpeg,bmp,png}')
   end
   
   module RPG
     module Cache
       def self.gallery(filename)
         self.load_bitmap("Graphics/Gallery/", filename)
       end
     end
   end

module Gallery
  PICS = []
  for i in 0...Pictures::NAMES.size
    PICS.push(RPG::Cache.gallery(Pictures::NAMES[i]))
  end
end

#==============================================================================
# ** Window_Picture
#==============================================================================

class Window_Picture < Window_Base
  #--------------------------------------------------------------------------
  def initialize(picture)
    super(0, 0, 640, 480)
    self.contents = Bitmap.new(width - 32, height - 32)
    @pic = Gallery::PICS
    @pics = @pic[picture]
    refresh
  end
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    x = (640 - @pics.width) / 2
    y = (480 - @pics.height) / 2
    rect = Rect.new(0, 0, @pics.width, @pics.height)
    rect2 = Rect.new(x, y, @pics.width>608 ? 608 : @pics.width, @pics.height>448 ? 448 : @pics.height)
    self.contents.stretch_blt(rect2, @pics, rect)
  end
end


I haven't included everything, but that's all that you really need.
Now when I play it says undefined method 'width' for @pics in Window_Picture.
Hope you know what I should do...

Thanks again
 

khmp

Sponsor

That's because you should instead be loading the pictures like this:
Code:
module Gallery
  PICS = []
  for i in 0...Pictures::NAMES.size
    PICS.push(Bitmap.new(Pictures::NAMES[i]))
  end
end

If you print the array from here:
Code:
module Pictures
  NAMES = Dir.glob('/Graphics/Gallery/*.{jpg,jpeg,bmp,png}')
end
p Pictures::NAMES
It will print the directory and the filename of the file found.

Then when you try to load the files using this method:
Code:
RPG::Cache.gallery
This will be the filename being read in:
Code:
self.load_bitmap("Graphics/Gallery/", "Graphics/Gallery/filename.pic")

Now if you are dead set on using the cache. You will need to do some additional work in module Pictures. Mainly slicing out the directory.
Code:
module Pictures
  NAMES = Dir.glob('/Graphics/Gallery/*.{jpg,jpeg,bmp,png}')
  for i in 0...NAMES.size
    NAMES[i].slice!('/Graphics/Gallery/')
  end
end

And then the rest of the code doesn't need to change.

Good luck wit hit sykval! :thumb:
 
Aah thankyou I think I'v got it now ^_^

I'm going to have use the Cache otherwise it loads the wrong name. I'm probably guna have to slice the .pngs and whatnot.

Oh and the main problem was the fact that there was a '/' infront of Graphics. That messed everything up.

Anyway thanks for the help, I hope it works ^_^
Punk will be most pleased xD


:EDIT:

Just one last problem xD

Code:
   module Pictures
     NAMES = Dir.glob('Graphics/Gallery/*.{jpg,jpeg,bmp,png}')
     for i in 0...NAMES.size
       NAMES[i].slice!('Graphics/Gallery/')
       NAMES[i].slice!('.png')
       NAMES[i].slice!('.jpg')
       NAMES[i].slice!('.jpeg')
       NAMES[i].slice!('.bmp')
     end
   end

Is there anyway of removing all the PNGs and JPGs on one line? Just wondering?
 

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