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.

Manipulating a filepath string to... just read the damn topic :P

There's just a couple simple things I need to know how to do, this has to do with manipulating filename strings...

Okay, my script currently calls for mission information written on a .txt, the directories are set up like this...


Code:
# Category 1, Mission 1

Data/Missions/Category[1]/Mission[1].txt

I want to be able to name the folder and .txt like so...

Category[1]-Name of the Category/
Mission[1]-Name of the Mission.txt

Only problem is then the script won't be able to find it with the name appended to the filename, how would I get it to automatically match and open the filepath reguardless of if they've got the 'name' appended to 'em or not?
 

khmp

Sponsor

Why not create a separate look up file that has this information? Create a mission.dat file or something. In the dat file you would dump a Hash/Array that would look like this:
Code:
mission_list = {
  'Act1' => ['Pick up clothes', Fight the Hamburglar', 'Procrastinate'],
  'Act2' => ['Trash Red Roof Inn Room', 'Watch Battle Royale'],
  'Act3' => ['Ride your bike with no handlebars']
}

file = File.open('mission.dat', 'wb')
Marhal.dump(mission_list, file)
file.close

Then when you open the game. Attempt to find the mission.dat file. If you've found it you will have all the information that you need to figure out all the directories.
Code:
mission_list = {}
if File.exists?('mission.dat')
  file = File.open('mission.dat', 'rb')
  mission_list = Marshal.load(file)
  file.close
end

I would seriously consider making the mission list more formulaic. Instead of naming a text file with the mission name give it a number. Are you creating something that will make it easy for someone to plug in their own missions and categories?

You can also grab all the missions using Dir[], Dir.glob.
Code:
mission_list = Dir.glob('Data/Missions/*/*.txt')

That will effectively grab any txt file buried a directory that can be named whatever, and place it all within an array. But I'm only guessing your application. So I could be heading in a completely opposite direction than what you wanted. If that is the case you'll need be more specific at least with me.

Good luck with it Kain Nobel! :thumb:
 

OS

Sponsor

I don't know if I fully understand what you are saying. Assuming that I am correct in saying that you want to access a file with a dynamic name. If so, then look for a specific part of the filename.

Run through all of the filenames in a given folder and check if filename.includes?(Mission[1]).

Does that help at all?

Here is the code, if you wish to see it:

Code:
module FileName
  #=============================================================================
  # Constants by Owen Sael
  #=============================================================================
  GameDir = Dir.pwd
  #=============================================================================
  # Filename With String
  # Written by Owen Sael
  #=============================================================================
  def self.filename_with_string(folder, text)
    dir = GameDir + "/" + folder
    files = Dir.entries(dir)
    i = 0
    while i < files.length
      if files[i].include?(text)
        return files[i]
      end
      i += 1
    end
  end
end

Just use FileName.filename_with_string(folder, text), where folder is Graphics/etc. or Data. Any folder in your Game Directory, and text is the string you are trying to match.

Hope this is helpful.

~Owen Sael

EDIT: khmp beat me to it. Ah, well.
 

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