[RGGS3] Search only for that precise name

Hello everybody.


In my game, I implemented a player registration system: when you register, you are asked to choose your name, and your save files will be named with that name ($game_party.leader.name + '*.rvdata2'). This works perfectly, but the issues start when I try to selectively delete save files by name.


I put all the names of the registered players in an array, and I start a for cycle to check if, for every name, there are save files.


In the spoiler below I wrote the part of the script I am using where it messes up.

Spoiler
for i in 6..$namesArray.size - 1 # The first six names are by default without save files (NPC accounts) and I do not need to check them, so I start the index from 6.


if Dir.glob($namesArray + '*.rvdata2').empty?


print Dir.glob($namesArray + '*.rvdata2') # Prints a list of the save files for the name stored in "i" position in the array. But not only them! It includes all the save files with names


                                                                   # containing the name I am checking!


print $namesArray + '*.rvdata2' # The save file name I am looking for, despite the engine includes other files too.


end


end

Whit this bug, if I look for Hello*.rvdata2 files, and in my directory there are Hello01.rvdata2 and Hello02.rvdata2, but also a HelloMe.rvdata2 file, the search will include all of them! This is pretty annoying, because in my game I need to ensure if a single player, with a specific name, has save files or not, and this non-exclusive search is a big obstacle. Morevoer, when it comes for the engine to delete save files by name, it does not make mistakes, and searches only for the save files with the name I asked.


 


Hoping I was inderstandale, what I would like to know is why the engine includes, in a search by name, names that include the name you are searching for and how to avoid that.


 


Maybe it is just a trivial problem, but I did not find anything about it on the forums or on the web, so thank you in advance for any help 🙂


 


 


EDIT: I just tried to remove the "*" from the code (searching for $namesArray + '.rvdata2' instead of $namesArray + '*.rvdata2'), and then the engine found no saves for every player.


This means, as I supposed, that the "*" stands for the number of the save file, and is necessary to the search, but is it possible that the engine also reads it as any letter after the given name (* == number from 01 to whatever or * == any string)?
I've moved this thread to Script Support. Please be sure to post your threads in the correct forum next time. Thank you.



The script forum is for presenting finished scripts you wrote.
Whit this bug, if I look for Hello*.rvdata2 files, and in my directory there are Hello01.rvdata2 and Hello02.rvdata2, but also a HelloMe.rvdata2 file, the search will include all of them!



So which one should be deleted in that case? Only the ones that have numbers?

print $namesArray + '*.rvdata2' # Correctly prints the save file name I am looking for, despite the engine includes other files too.



Of course this will return only the save file ur needing since you're directly reading it from the names array instead of the list of actual save files. This one doesn't do the actual search that ur Dir.glob is doing, even a save file that doesn't exist yet can be shown via this line as long as the name exist in the array.
$namesArray = ["Bob","Mark","Hugh","Dough"]
def search_by_name(name_array)
save_file_names = []
name_array.each do |name|
if !Dir.glob(name + '[1-99].rvdata2').empty?
save_file_names += Dir.glob(name + '[1-99].rvdata2')
end
end
return save_file_names
end
p search_by_name($namesArray)


The function will return an array includes name of all available save files (Index from 1 to 99) of 4 players Bob, Mark, Hugh and Dough for instance. Hope this will help you : D
That snippet in the opening post is full of errors.


I am surprised it didn't give you any crashes. 😳


I'm assuming that $namesArray is an array, otherwise, why would you name it that way?


First, you are trying to subtract 1 from an array. That will crash you game for sure.


Next, you are trying to use an array to set the end of a Range object. This would crash your game too.


Than, even if you fix these (by using $namesArray.size - 1 at the end), you never use any iteration indexes anywhere inside your iteration, so those indexes will do absolutely nothing.


At this point, that code will do... well, nothing useful.


This is how it should look like:

Spoiler
Code:
# Syntax 1:
for i in 6..$namesArray.size - 1
  files = Dir.glob($namesArray[i] + '*.rvdata2') # Getting the file list
  p files # Prints the name of all found files
  if files.empty?
    # Do your "No files found" stuffs here.
  end
end

# Syntax 2
$namesArray.each_with_index do |fname,i|
  next if i < 6
  files = Dir.glob(fname + '*rvdata2') # Getting the file list
  p files # Prints the name of all found files
  if files.empty?
    # Do your "No files found" stuffs here.
  end
end

You must use the iterated elements in an iteration or else there is really no need to iterate them, right?


So many iterations, yay! 😀


Also, if you need to use some variables which needs to be calculated before usage multiple times in your code, the best thing you can do is to store them in a local variable, and read from that when you need them instead of always recalculating the variables whenever you need them.


If you just typed in that snippet and made these errors here only, well, if you ask for help, make sure to provide the correct information without errors and differences from your original code, or else we can't really help, or know what part of the code you can't get to work (because here, all of your code was unusable, for example).


And in case of checking file names like this, you must make sure that there is no way of getting a file more than 1 time.


For example, using "Actor1" and "Actor11" would make the check return both of them when you check for "Actor1".


Or "Bob" and "BobSuperCool", these will act the same when you search for "Bob".


Ohh, and to answer your last question, the * in this syntax stands for any character in the file name in any amount.


Anyway, both of the snippets I showed should work.
Sorry, guys, but I saw just now all those errors I put in the spoiler. @Sixth, the code did not crash because I wrote it correctly in the engine. Meanwhile, I also found something about the "*" character used in that context in the Ruby online documentation: in that context, "*" stands for any character, and this explains why the engine found any save file containing the name I checked for. I also discovered that a more exclusive check can be done by putting [set] before the "*". Inside the brackets you just need to put the only characters you want to be allowed (in my case, the numbers of the save files).


I am sorry for all the mess with those spoilers and that badly copied code, and I really thank you all for your help. Now this thread can be closed.


Sorry again.

Original thread: https://forums.rpgmakerweb.com/threads/rggs3-search-only-for-that-precise-name.74210/