简体   繁体   中英

Save table in corona SDK

Good day, for a while I'm trying to save my table in Corona SDK. The unlocked levels still have to be unlocked when exiting and entering the game.

I've tried several things, but I still can't figure it out. How can I accomplish this?

Here is part of my code:

    G = {
Gamescore1=0,
Gamescore2=0,
Gamescore3=0,
Gamescore4=0,
Gamescore5=0,
Gamescore6=0,
Gamescore7=0,
Gamescore8=0,
Gamescore9=0,
}

    --Gamescore1=0
function addscore1()
G.Gamescore1=G.Gamescore1+1

    -----------------------------------------------------------
end
    ----------------------------------------------------------
function addscore2()
G.Gamescore2=G.Gamescore2+1
end
    ---------------------------

function addscore3()
G.Gamescore3=G.Gamescore3+1
end

function addscore4()
G.Gamescore4=G.Gamescore4+1
end

function addscore5()
G.Gamescore5=G.Gamescore5+1
end

Gamescore6=0
function addscore6()
G.Gamescore6=G.Gamescore6+1
end


function addscore7()
G.Gamescore7=G.Gamescore7+1
end


function addscore8()
G.Gamescore8=G.Gamescore8+1
end

function addscore9()
G.Gamescore9=G.Gamescore9+1
end

Note: the Gamescore variables are to get to the next level if the Gamescore = 1 the next level will be unlocked.

So every time an addscore function is activated I need to save the new Gamescore.

Example: Gamescore1 = 0

the addscore1 function is triggered so now Gamescore1 = 1. The next level is unlocked if Gamescore1 = 1. I have all that set. Now I only need to save Gamescore1 with its new value (1).

I hope this is enough information. I would really appreciate it if someone could help me.

Thanks in advance!

The table values will be lost when your program ends. You have to save your table to a file or database.

For this you have to serialize your table in some way as you cannot save tables directly. There are countless ways so I won't explain to much here. Just search the web for Lua table serialization or read this: http://lua-users.org/wiki/TableSerialization

You basically transform the content of your table into something you can write / read to / from a file.

The file content could look like this:

Gamescore1=1;Gamescore2=0; and so on if its just 0 and 1 you could simply store some binary representation.

Choose whatever suits your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM