chevgc
February 19, 2018, 3:19am
1
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
Pkeod
February 19, 2018, 5:10am
2
Here are some more neat things Lua can do!
--- This file is not meant to be ran as a whole, it's a ref
-- instead copy and paste (or better type) into other files to run each
-- it's a cheatsheet refresher of examples
-- / common ideas / idioms / design patterns
--- create a key value store / map / associative array
-- get contents of a table
tool_data_table = {name = "Defold", use = "gamedev"}
print(tool_data_table.name)
--- set random seed to current local time
-- get local unix timestamp in seconds
math.randomseed(os.time())
--- get formatted time in YYYY-MM-DD
print(os.date("%F", os.time()))
print(os.date("%F", os.time({year = 2000, month = 8, day = 24})))
--- get a random number between a range
lower = 0
This file has been truncated. show original
1 Like
britzl
February 19, 2018, 5:22am
3
chevgc:
pretty neat!
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!
2 Likes
chevgc
February 19, 2018, 5:33am
4
Will come in handy thanks for the share.