[SOLVED]Touble When Saving Tables on Android

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
uederson
Prole
Posts: 30
Joined: Mon May 23, 2016 7:59 pm
Location: Brazil
Contact:

[SOLVED]Touble When Saving Tables on Android

Post by uederson »

Hi all!
I'm working on a simple game.
All the levels are made up from tables.
I want let the player create levels and I need some way of save the created level as a table.
I tried some libs to do it: Tserial I didn't was able to get it working on my code; from Ser I got a message saying my table is unsurpported. Then I found the lib Save Table To File: https://love2d.org/wiki/libSaveTableToFile
The issue is: on Linux, it works fine, but on Android, it does not work (no save, no load).
Does it have something to do with Android filesystem?
Last edited by uederson on Wed May 24, 2017 8:43 pm, edited 1 time in total.
User avatar
DiegoG
Prole
Posts: 20
Joined: Sat Apr 29, 2017 2:43 am
Location: Venezuela

Re: Touble When Saving Tables on Android

Post by DiegoG »

I suggest you try checking how and where the table's saved
Maybe it's just the way LOVE saves data across platforms?

Regardless, here's the custom-made code I use for this; you might need to change a few bits

Code: Select all

local dat = "local options = {"
  local gtotal = 0
  local gcurrent = 0
  
  for i,v in pairs(main.config) do
    gtotal = gtotal+1
  end
  
  for i,v in pairs(main.config) do
    
    if type(v) == "table" then
      
      dat = dat.."\n  "..i.." = {"
      local total = 0
      local current = 0
      for i,v in pairs(v) do
        total = total+1
      end
      
      for i1,v1 in pairs(v) do
        
        if type(v1) == "string" then
          
          if current == total-1 then
            dat = dat.."\n    "..i1.." = ".."\""..(v1).."\""
          else
            dat = dat.."\n    "..i1.." = ".."\""..(v1).."\""..","
          end
          current=current+1
          
        else
          
          if current == total-1 then
            dat = dat.."\n    "..i1.." = "..tostring(v1)
          else
            dat = dat.."\n    "..i1.." = "..tostring(v1)..","
          end
          current=current+1
          
        end
        
      end
      
    end
    
    if gcurrent == gtotal-1 then
      dat=dat.."\n  }"
    else
      dat=dat.."\n  },"
    end
    
    gcurrent = gcurrent+1
    
  end
  
  dat=dat.."\n}\nreturn options"
function earth:destroy()
00count=5000
00while count>0 do
0000lg.draw(explosion,math.random(0,600),math.random(0,800))
0000count=count-1
00end
00earth = nil
00print("Earth has been destroyed.")
end
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Touble When Saving Tables on Android

Post by zorg »

Also, that library (libSaveTableToFile) uses lua's own io functions; i'd use love.filesystem functions instead.
Also, Ser's pretty lenient with what it allows for you to serialize, so i'm a bit suprised it said a type was unsupported.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
uederson
Prole
Posts: 30
Joined: Mon May 23, 2016 7:59 pm
Location: Brazil
Contact:

Re: Touble When Saving Tables on Android

Post by uederson »

DiegoG wrote: Tue May 23, 2017 11:09 pm I suggest you try checking how and where the table's saved
Maybe it's just the way LOVE saves data across platforms?
On Linux, SaveTableToFile saves the file inside the lovegame folder (different place from Love's functions like love.filesystem.newFile). Then, by what zorg said about the lua's io functions, I think the problem is that SaveTableToFile is trying to save the file inside the lovegame folder on Android (by what I verified using love.filesystem.getSaveDirectory, the path for saving files on Android is data/data/org.love2d.android/files/savegame) and maybe it can not do it.
Sadly I don't know how SaveTableToFile saves the file because I am very beginner in programming and the code is too complex to me.
Thank you so much by the code! I will try it! :)
zorg wrote: Tue May 23, 2017 11:18 pm Also, that library (libSaveTableToFile) uses lua's own io functions; i'd use love.filesystem functions instead.
Also, Ser's pretty lenient with what it allows for you to serialize, so i'm a bit suprised it said a type was unsupported.
Yes! I am a very new programmer, but how I said before (by your observation about lua's io functions), I think the problem is that SaveTableToFile is trying to save the file inside the lovegame folder.

Attached is a picture from my game.
The only thing I store inside the table is the position and an ID for each stone in the level (the yellow, blue, grey blocks and the one on the right side of the player). The table is simple. I don't know why Ser is giving me the error saying "unsupported type userdata"

Thank you guys by your help!
Attachments
bobjombis.png
bobjombis.png (502.86 KiB) Viewed 4580 times
uederson
Prole
Posts: 30
Joined: Mon May 23, 2016 7:59 pm
Location: Brazil
Contact:

Re: Touble When Saving Tables on Android

Post by uederson »

zorg wrote: Tue May 23, 2017 11:18 pm Also, Ser's pretty lenient with what it allows for you to serialize, so i'm a bit suprised it said a type was unsupported.
My bad! :cry:
Ser was giving me the error message because my code was insertting image data inside the table (I was unable to see this). I am a very very bad programmer (if someone really can call me a programmer)! :megagrin:
Now Ser is working like a charm!
Thank you again, guys!
User avatar
DiegoG
Prole
Posts: 20
Joined: Sat Apr 29, 2017 2:43 am
Location: Venezuela

Re: Touble When Saving Tables on Android

Post by DiegoG »

uederson wrote: Wed May 24, 2017 8:42 pm
zorg wrote: Tue May 23, 2017 11:18 pm Also, Ser's pretty lenient with what it allows for you to serialize, so i'm a bit suprised it said a type was unsupported.
My bad! :cry:
I am a very very bad programmer (if someone really can call me a programmer)! :megagrin:
Dude, don't sweat it :P
We all make mistakes, just learn from them
The very fact that you can write functioning code at all makes you a programmer
Soon enough you'll become better
function earth:destroy()
00count=5000
00while count>0 do
0000lg.draw(explosion,math.random(0,600),math.random(0,800))
0000count=count-1
00end
00earth = nil
00print("Earth has been destroyed.")
end
uederson
Prole
Posts: 30
Joined: Mon May 23, 2016 7:59 pm
Location: Brazil
Contact:

Re: Touble When Saving Tables on Android

Post by uederson »

DiegoG wrote: Wed May 24, 2017 10:13 pm Dude, don't sweat it :P
We all make mistakes, just learn from them
The very fact that you can write functioning code at all makes you a programmer
Soon enough you'll become better
Thank you, friend!
I really like so much programming! :3
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 24 guests