Count how many file exists with a certain prefix ?

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

Previous topic - Next topic
Yeah i'm completely right with Diarandor's solution, yet, something is unclear because you need to open the concerned save to get the value and write it to another file, maybe using Chris's solution will work, allowing unlimited saves

That also get me to the point where input customisation need to be stored in a file so we can bind custom inputs, even if we are not in a game, but that's something else.

March 14, 2017, 11:28:42 AM #16 Last Edit: March 14, 2017, 12:28:22 PM by MetalZelda
I find a solution, but, I'm unable to extract the needed values in order to insert them in a table, io.lines(file) always return an error. (bad argument #1 to 'lines' (string expected, got userdata))

EDIT: it works, but now how can I extract the value I need from this certain file ?

Doing through setfenv, don't know if that's the right way

Code (lua) Select
function title_screen:do_file(i, name)
  local slot = self.slots[i]
 
  local environment = {
    prop = function()
  slot.max_health = _max_life,
  slot.current_health = _current_life
  slot.player_name = player_name
  slot.tunic = _ability_tunic
  slot.shield = _ability_shield
  slot.sword = _ability_sword
end,
  }
 
  setmetatable(environment, {
    __index = function()
      return function() end
    end
  })
 
  local chunk = sol.main.load_file(name)
  setfenv(chunk, environment)
  chunk()
end

Just use file:lines() instead of io.lines(). io.lines takes a filename, not a file.

setfenv also works (parsing the data file as Lua) but I don't like it so much because it is a function that no longer exists in Lua 5.2. If we switch to a newer Lua version than 5.1 one day, there will be a compatibility issue. Not a big deal, because Lua 5.2 introduces a new (and better) way, but still some manual changes required.

I finally got the file to load and store values in a array, things like hearts display need to be done completely manually.

Thanks for the help

March 14, 2017, 07:41:49 PM #19 Last Edit: March 14, 2017, 10:05:10 PM by MetalZelda
Is it possible to arrange an array in order to display at first the last game played, the modification date of the file might be enough but is it possible through lua ?
100 saves slots should be enough, there are no more lags whe loading all files

Edit: restict to 90 saves, above 90 saves (90 * 90), the surface won't even draw, don't know if it's a bug when a surface above 8100 pixels won't draw

I think 20 or 30 savegames is more than enough. In order to get first the last game played, I think the easiest solution is to save another variable with the index of the last savegame. Each time you save a game you should update the file with your savegame menu info (I think that is what you are already doing, but I am not sure), and at that time you can save this index too.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

You can use a custom sort function to arrange things however you want. The following example assumes that the date is some number that increases over time.

Code (lua) Select
local function sort_by_date(a, b)
return a.date > b.date --descending order
--return a.date < b.date --ascending order
end

local example_data = {
{ date = 56, name = "second" },
{ date = 99, name = "first" },
{ date = 21, name = "third" },
}

print"Original Order:"
for i,data in ipairs(example_data) do
print(string.format("%i - date: %f, name: %s", i, data.date or 0, data.name))
end

table.sort(example_data, sort_by_date)

print"\nSorted by Date:"
for i,data in ipairs(example_data) do
print(string.format("%i - date: %f, name: %s", i, data.date or 0, data.name))
end

March 15, 2017, 12:04:56 PM #22 Last Edit: March 15, 2017, 12:10:56 PM by MetalZelda
Quote from: llamazing on March 14, 2017, 11:16:35 PM
You can use a custom sort function to arrange things however you want. The following example assumes that the date is some number that increases over time.

Code (lua) Select
local function sort_by_date(a, b)
return a.date > b.date --descending order
--return a.date < b.date --ascending order
end

local example_data = {
{ date = 56, name = "second" },
{ date = 99, name = "first" },
{ date = 21, name = "third" },
}

print"Original Order:"
for i,data in ipairs(example_data) do
print(string.format("%i - date: %f, name: %s", i, data.date or 0, data.name))
end

table.sort(example_data, sort_by_date)

print"\nSorted by Date:"
for i,data in ipairs(example_data) do
print(string.format("%i - date: %f, name: %s", i, data.date or 0, data.name))
end


This might be the only solution, yet, the only parameter needed here is the file's last modifcation date which will be stored in file.date, then this will be fairly possible with your function

This is how infos are retrieved from a savegame file

Code (lua) Select
function title_screen:do_file(i, name)
  local file = sol.file.open(name)
  local slot = { dungeon = {} }

  -- This should be where the last modification date will be stored
  -- slot.date = ???
 
  local font, font_size = sol.language.get_dialog_font()
  slot.player_name_text = sol.text_surface.create{
    font = font,
    font_size = font_size,
  }
 
  for line in chunk:lines() do
    -- Player stats
    if line:match("player_name")then
  slot.player_name_text:set_text(line:sub(16, -2))
end
if line:match("_ability_shield") then
  slot.shield = line:sub(19)
end
if line:match("_ability_sword") then
  slot.sword = line:sub(18)
end
if line:match("_ability_tunic") then
  slot.tunic = line:sub(18)
end
-- Game stats
if line:match("_current_life") then
  slot.current_life = line:sub(17)
end
if line:match("_max_life") then
  slot.max_life = line:sub(13)
end
if line:match("hero_mode") then
  slot.hero_mode = line:sub(13)
end
if line:match("finished_game") then
  slot.finished_game = line:sub(17)
end

-- Dungeon state
if line:match("dungeon_1_finished") then
  slot.dungeon[1] = true
end
       -- blablabla
  end
 
  file:close()
  slot.savegame_number = i
  self.slots[i] = slot
end


The file organisation is done somewhere else when another function, more explainatory, is called, here, i just want the last modification date

There is probably a shorter way to parse the savegame files. Here is an example of the string.gmatch official doc:
Code (lua) Select

-- The next example collects all pairs key=value from the given string into a table:
     t = {}
     s = "from=world, to=Lua"
     for k, v in string.gmatch(s, "(%w+)=(%w+)") do
       t[k] = v
     end

This example can be adapted to include string values (enclosed inside double quotes) and it should just work.