Count how many file exists with a certain prefix ?

Started by MetalZelda, March 10, 2017, 05:42:24 PM

Previous topic - Next topic
Greetings.

Recently i've been working on the final title screen based on RPG / Zelda BOTW.
And so comes this idea of unlimited saves number.



however, i wanna display all files stats here, i already know how to do it, the problem is, the menu is a scroll type menu, with as max_position, the number of saves, however, I can't figure out how to count how many files with "save" prefix exists, if anyone have a clue, i'll gladly take it

You have to test them one by one in a loop, there is no way to get all files from a folder (yet).

You can use a break statement in Lua, so it should be pretty easy to accomplish (as long as they're named sequentially).

Okay I've been able to make it work, however, sol.game.load is slow when dealing with a lot of files, yet, there is no huge ressource consumption, whick makes the slowdown weird, so I'm limitting the files to 4



Thanks

Don't try to sol.load() all potential files, just test if they exist. With sol.file.exists().

Quote from: Christopho on March 11, 2017, 01:19:00 AM
Don't try to sol.load() all potential files, just test if they exist. With sol.file.exists().

Yes, I know this, this is how i is done currenly, but if all of them exists, we need to load them all, which cause freeze sometimes. Strangely, loading all files only takes around a megabyte on the ram, so this is weird

I mean, to display infos of a certain file, you need to sol.game.load, and this is why there are some freezes

Well, my advice is to only load the visible files in the menu when required to show them (i.e. a scrolling menu where only three are shown at a time.), store the variable that you need into a list with in a list (arrays work too). This should spread out the load time to when the user attempts to access the other files additional files. Also, you should probably make it so that it sorts by most recently saved. Most users are not going to touch their old saves unless they want to show off their progress or just mess with it.

You could also attempt to use
Code ( lua) Select
local game_var = require("path/to/save/data.dat")
But don't know if that will work.
This signature was way too long before, but now it's short!
Also, I am Still Alive!
On ad Off I go!

Do you ever get the feeling that the fandom of a product(s) ruin the potential that you could have had to enjoy the product?

March 13, 2017, 11:34:16 AM #8 Last Edit: March 13, 2017, 11:45:02 AM by MetalZelda
Quote from: YoshiMario2000 on March 12, 2017, 10:35:00 PM
Well, my advice is to only load the visible files in the menu when required to show them (i.e. a scrolling menu where only three are shown at a time.), store the variable that you need into a list with in a list (arrays work too). This should spread out the load time to when the user attempts to access the other files additional files. Also, you should probably make it so that it sorts by most recently saved. Most users are not going to touch their old saves unless they want to show off their progress or just mess with it.

You could also attempt to use
Code ( lua) Select
local game_var = require("path/to/save/data.dat")
But don't know if that will work.

The first idea is a good solution, only loading 2 files (the visible ones) is a rather cool solution, gonna try what I can get with this solution, and sorting by the most recent save is a rather good solution, but, it might be complex to do, I mean the table containing saves need to be arranged with one parameter, the most recent file, don't know if Lua have this or if it might work with all OS.

One solution here
http://stackoverflow.com/questions/33470835/get-file-creation-time-with-lua
One other here
http://stackoverflow.com/questions/33296834/how-can-i-get-last-modified-timestamp-in-lua

At least, the menu is nearly done, so this might be not so long
https://www.youtube.com/watch?v=goCbfhRsnOY

And, one save = 1 quest save, so this helps not dealing with a lot of saves, i should re-implement file copying, there is already something that analyse if 1 file doesn't exist so it will be the first file to be created

The idea of loading files lazily (only when the info is needed to be displayed) is great!

About the second suggestion with require, forget about it. require() is meant to include other scripts, not data files. It would not work since savegames are data files that don't return anything. You would not be able to read any result. And even if you could, it would be wrong because savegame files can have changed in the meantime if the user has played a while, has saved, and then went back to the savegame menu. Remember that require() only loads the script file the first time and keep its result in cache.

Quote from: Christopho on March 13, 2017, 11:51:10 AM
The idea of loading files lazily (only when the info is needed to be displayed) is great!

About the second suggestion with require, forget about it. require() is meant to include other scripts, not data files. It would not work since savegames are data files that don't return anything. You would not be able to read any result. And even if you could, it would be wrong because savegame files can have changed in the meantime if the user has played a while, has saved, and then went back to the savegame menu. Remember that require() only loads the script file the first time and keep its result in cache.

Yes that's a cool idea, it could break the freeze when loading a lot of savegames, yet, I am liking YoshiMario2000's idea of sorting the save menu by the most recently played game, the code already organize saves so, even if save 3 is missing but save 4 exists, it'll be correctly display save 4 in the GUI's slot 3, is there a simpler way than the links i've poster above to organize the array by the last modified file ?

Am I correct in assuming that the only reason you are "loading" the savegame files is to get access to the state of the savegame variables?

If so, perhaps what is needed is some sort of quick load feature that only gives read access to savegame variables but doesn't do any of the other loading of resources needed to actually start the game and therefor would be faster for checking multiple files.

With sol.main.load_file (and not require!) and setfenv it is possible.
Or sol.file.open and parsing the file by hand, because setfenv no longer exists in Lua 5.2 (replaced by ENV_) so you will have a problem if one day we change the Lua version.

Another solution is to save in a common different file the savegames info that you need to show, so you can load it fast. You can override the saving function "game:save()", so that this info is saved in that other file. Maybe a new instance of "game" can be used to save this in the new file, without starting that game.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Good idea! Since you can create any file, you can create one that maintains the list of savegames. Simple as that.

But don't overwrite game:save(), just create your own function (in game_manager or whatever) that calls game:save() and updates the file. It is best practice not to change the official API when it is not necessary.