Test the presence of a character/picture through code

Hello !

I am currently working on a script that will change the display of characters based on situations.
These characters are linked to a battler such that when the battler has some states (for instance is dead), the character should be changed to something else based on its original name.

For instance, a slime using the charset "Monster2" should use instead "Monster2_down".
If no such charset exists, it should load a default charset (say "!Flame"). The code would then be something like this:

Ruby:
class MyCharacter < Game_Character
    #bat_char_name bat_char_index are defined before and are modified manually
    #example here :
    #@bat_char_name = "Monster2"
    #@bat_char_index = 2
    def change_graphic_state(bat)
        def_char = "!Flame"
        def_index = 5
        suffix = bat.dead? ? "" : "_down"
        cname = @bat_char_name+suffix
        if File.exist?("Graphics/Characters/" + cname)
          set_graphic(cname,@bat_char_index)
        elsif not def_char.nil? and File.exist?("Graphics/Characters/" + def_char)
          set_graphic(def_char, def_index.nil? ? @bat_char_index : def_index)
        else
          set_graphic(@bat_char_name,@bat_char_index)
        end
    end
end

However, File.exist? (along with variation like File.exists? or FileTest.exist? or FileTest.exists?) will return false even when given "Graphics/Characters/!Flame".

I guess it requires the extension of the file too in order to find the file. So my question is: can we test the existence of a file (more precisely a picture readable by rpg maker) given only the name that we usually store in a Game_Character ?

I want to avoid raising errors as I want to provide default solutions when no such files exist. I am also looking for a solution that is hopefully not too heavy as this kind of function might be called several times in my system.
Without delving deep into your issue, I can tell you that @character_name already exists and should be used in this kind of scenario they way you described above.
I don't understand what you mean.

I don't want to use @character_name as it will be replaced when calling set_graphic.
For instance, if I were to kill a battler, call change_graphic_state, then resurrect the battler, when calling change_graphic_state again, the character should go back to the original charset, hence why I store its original name in @bat_char_name in order to retrieve it.
Hey,

Are you planning on using an encrypted archive when deploying your project? If so, then traditional methods of checking those files will probably no longer work (unless you exclude them from the archive).

To check for files with any extension, you could try to use Dir to create an array of filenames matching the pattern (see also Ruby-Doc):
if Dir["Graphics/Characters/#{cname.gsub(/[\\*?[{]/) { |s| "\\#{s}" }}{,.*}"].length > 0

(Never actually used this method, I believe the pattern should work like this, if not there is always Dir.entries)


Of course you can also just try to load the graphic and see if it succeeds:
if (Cache.character(cname) rescue nil)

Regardless of the weight of your approach, you can always cache which graphics were available to avoid repeated look-ups (raising exceptions is comparably slow):
Using the Cache module assumes you will likely use the graphic if it is available since they will be held in reserve once loaded:
Ruby:
class Game_Character
  @@available_graphics = Hash.new { |h, k|
    begin
      Cache.character(k)
      h[k] = true
    rescue Errno::ENOENT
      h[k] = false
    end
  }
 
  def graphic_available?(name)
    return @@available_graphics[name]
  end
end


Another alternative might be to create a table of contents before deploying your game, which would include the names of eligible files.
Are you planning on using an encrypted archive when deploying your project? If so, then traditional methods of checking those files will probably no longer work (unless you exclude them from the archive).
I did not know about this, I plan on sharing my script at some point but its use could have been on an encrypted project, I will have to be careful about file checking then!

I did not know about Dir either, I will have to check this.

For now I went along with catching errors when calling Cache.character, because of this I decided to move my checking directly to the Sprite_Character class when trying to load the bitmap as it made more sense. I will probably use the Hash table if catching errors is too slow.

Thank you for your solutions!

Original thread: https://forums.rpgmakerweb.com/threads/test-the-presence-of-a-character-picture-through-code.175672/