[RGSS1]Help with text strings pointing at file names

Started by Shadow Eye31, June 02, 2012, 11:03:39 am

Previous topic - Next topic

Shadow Eye31

I am currently trying to make a basic save/load system that creates a unique save file for each character, which is named (actor name).save, and then on the loading screen, brings up all character files. located via the .save extension. I have successfully written the saving portion, but I am stuck on the loading portion. I can't seem to find a way for the script to just look for .save and not a file header as well. Here's a better example:


The default File, Save, and Load scripts deal with this:
return "Save#{file_index + 1}.rxdata"

I want to have them search for just the extension, the .rxdata, of the files, not the header as well. Something like:
return ".rxdata"

But that doesn't work, I tried.


I've searched through several RGSS guides, including the ones that come with the program, but nothing has told me how to do this. If anyone could help, I'd appreciate it. It's probably really easy, but I've been at it for the past few hours and its midnight now, so my brain is shot.

Blizzard

The default saving system doesn't work that way. This piece of code.
return "Save#{file_index + 1}.rxdata"

is basically used to generate the filenames Save1.rxdata to Save4.rxdata. It doesn't really scan the directory for .rxdata files. If you want to do something like that, you'd have to use something like this:

filenames = []
Dir.each(DIRECTORY) {|filename|
 if filename.ends_with(".rxdata") # can't remember if it was ends_with or endswith
   filenames.push(filename)
 end
}


Keep in mind that this isn't foolproof either. It will detect all the files with the extension .rxdata. If there is a file with .rxdata that isn't a save file, it will likely crash the game. To fix that, you would have to implement much safer loading by using begin-catch blocks.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

filenames = Dir.glob("Data/*.rxdata")

This will find all files in the "Data" folder that have an ".rxdata" extension.

You can also search for partial filename matches with it. For example, this will get a list of all ".rxdata" files that start with the "Map":
Dir.glob("Data/Map*.rxdata")
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

I wasn't aware of the glob function. Are you sure it exists in 1.8.2/RGSS1?
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

Yeah, I didn't know if 1.8 had it either, but I tested it in RMXP before posting and it does.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Shadow Eye31

@Blizzard: I couldn't get that to work. I kept getting a string error for DIRECTORY, and couldn't figure out how to initialize it for use. Then again, it was 2 am when I tried.

@ForeverZer0: That's just what I needed. Thank you!

You, sirs, are gentlemen and scholars both! Thank you for your help!

Blizzard

Obviously you have to put a string instead of DIRECTORY.

Dir.each(".") {|filename| # current directory

Dir.each("Data/Saves") {|filename| # some directory

Dir.each("../some other folder") {|filename| # going up one directory with ..

Dir.each("C:/Windows") {|filename| # absolute path, avoid this type unless you know what you're doing

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.