Saving and Loading data from table tips

So Im experimenting with save files and came across something interest. For anyone new(ish) to defold you can actually access nested tables pretty easily. Im working on a game with multiple levels each with collectables etc

this is basically how the table looks

game_data ={
coins = 0,
levels = {
{score=0, collectable1=false,collectable2=false, collectable3=false},
{score=0, collectable1=false,collectable2=false, collectable3=false},
{score=0, collectable1=false,collectable2=false, collectable3=false}},
options={sound=true,music=true}
}

the way how I access the nested data is by using a function

function get_level_data(self, level)
local level_info = {
score = game_data.levels[level].score,
collectable1 = game_data.levels[level].collectable1,
collectable2 = game_data.levels[level].collectable2,
collectable3 = game_data.levels[level].collectable3
}
return level_info
end

pretty neat!

1 Like

Here are some more neat things Lua can do!

1 Like

The Lua table type is a beautiful data structure that is easy to work with (almost always) and very very flexible. If you’re serious about game dev in Lua you’ll have to learn to love Lua tables!

1 Like

Will come in handy thanks for the share.