Author Topic: Count how many file exists with a certain prefix ?  (Read 541 times)

MetalZelda

  • Sr. Member
  • ****
  • Posts: 479
    • View Profile
Count how many file exists with a certain prefix ?
« on: March 10, 2017, 05:42:24 pm »
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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1008
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #1 on: March 10, 2017, 08:50:25 pm »
You have to test them one by one in a loop, there is no way to get all files from a folder (yet).

wrightmat

  • Full Member
  • ***
  • Posts: 218
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #2 on: March 10, 2017, 10:11:40 pm »
You can use a break statement in Lua, so it should be pretty easy to accomplish (as long as they're named sequentially).

MetalZelda

  • Sr. Member
  • ****
  • Posts: 479
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #3 on: March 11, 2017, 01:03:35 am »
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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1008
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #4 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().

MetalZelda

  • Sr. Member
  • ****
  • Posts: 479
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #5 on: March 11, 2017, 12:41:38 pm »
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

MetalZelda

  • Sr. Member
  • ****
  • Posts: 479
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #6 on: March 12, 2017, 10:11:50 pm »
I mean, to display infos of a certain file, you need to sol.game.load, and this is why there are some freezes

YoshiMario2000

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #7 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
  1. 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?

MetalZelda

  • Sr. Member
  • ****
  • Posts: 479
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #8 on: March 13, 2017, 11:34:16 am »
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
  1. 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
« Last Edit: March 13, 2017, 11:45:02 am by MetalZelda »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1008
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #9 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.

MetalZelda

  • Sr. Member
  • ****
  • Posts: 479
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #10 on: March 13, 2017, 12:08:51 pm »
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 ?

llamazing

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #11 on: March 13, 2017, 02:30:55 pm »
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.

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1008
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #12 on: March 13, 2017, 02:32:17 pm »
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.

Diarandor

  • Hero Member
  • *****
  • Posts: 700
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #13 on: March 13, 2017, 02:51:57 pm »
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.

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1008
    • View Profile
Re: Count how many file exists with a certain prefix ?
« Reply #14 on: March 13, 2017, 03:20:46 pm »
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.