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.

Help with a screen capture script.

Code:
 

module Screen

  

  @screen = Win32API.new 'screenshot', 'Screenshot', %w(l l l l p l l), ''

  @readini = Win32API.new 'kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l'

  @findwindow = Win32API.new 'user32', 'FindWindowA', %w(p p), 'l'

  module_function

  # Settings for file name and file type.

  def shot(file = "pic_camera", typ = 2)

    if typ == 0

      typname = ".bmp"

    elsif typ == 1

      typname = ".jpg"

    elsif typ == 2

      typname = ".png"

    end

    file_index = 1

    # File directory.

    dir = "Graphics/Pictures/"

    # File tag.

    file_name = dir + file.to_s + "_#{file_index}" + typname.to_s

    # New tag/file if one exists already.

    if FileTest.exist?(file_name)

      file_name = dir + file.to_s + "_#{file_index + 1}" + typname.to_s

    else

      file_name = dir + file.to_s + "_#{file_index}" + typname.to_s

    end

    @screen.call(0,0,640,480,file_name,handel,typ)

  end

  def handel

    game_name = "0" * 256

    @readini.call('Game','Title','',game_name,255,".Game.ini")

    game_name.delete!("0")

    return @findwindow.call('RGSS Player',game_name)

  end

end

So I was playing around with making an efficient screen capture script that can take an infinite amount of them and save them off to the "Pictures" folder. I have one problem with it that I know has a very simple solution, but I can't seem to figure it out.

The problem is the way the code is set up. "index" is constantly reset to "1". Here's what happens...
- The code runs once and "pic_camera1.png" is created. (the 1 being what "index" equals)
- Code runs again, recognizes "pic_camera(index).png" already exists, so creates "pic_camera2.png" (index +1)
- Now here's the problem, since we're reset to 1, if the code runs again it simply replaces "pic_camera2.png" again.
So what I need is for the code to recognize the last file created instead of the first one, or make index increase each time or something.
 

Zeriab

Sponsor

Try this modification:
[rgss]module Screen
 
  @screen = Win32API.new 'screenshot', 'Screenshot', %w(l l l l p l l), ''
  @readini = Win32API.new 'kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l'
  @findwindow = Win32API.new 'user32', 'FindWindowA', %w(p p), 'l'
  module_function
  # Settings for file name and file type.
  def shot(file = "pic_camera", typ = 2)
    if typ == 0
      typname = ".bmp"
    elsif typ == 1
      typname = ".jpg"
    elsif typ == 2
      typname = ".png"
    end
    @file_index = 1 if @file_index.nil?
    # File directory.
    dir = "Graphics/Pictures/"
    # File tag.
    file_name = dir + file.to_s + "_#{@file_index}" + typname.to_s
    # New tag/file if one exists already.
    while FileTest.exist?(file_name)
      @file_index += 1
      file_name = dir + file.to_s + "_#{@file_index}" + typname.to_s
    end
    @screen.call(0,0,640,480,file_name,handel,typ)
  end
  def handel
    game_name = "0" * 256
    @readini.call('Game','Title','',game_name,255,".Game.ini")
    game_name.delete!("0")
    return @findwindow.call('RGSS Player',game_name)
  end
end
[/rgss]

*hugs*
- Zeriab
 
Ah, that did the charm, thanks!

Now here's a curve ball. Would it be possible to save off different files based on the current game-save file? Like...

If save file ID = 1
Then file_name = dir + file.to_s + "A_#{@file_index}" + typname.to_s
ElseIf save file ID = 2
Then file_name = dir + file.to_s + "B_#{@file_index}" + typname.to_s

Just something to prevent a new game overwriting screen captures from a previous game.
 

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