How to savedata in Lunalua
Posted: Wed Apr 24, 2019 10:13 am
This topic was made for showing and discussing how to save data in Lunalua. Below shows the exact thing that Enjl did in his video.
In a more advanced way, can we save data one by one after collecting each dragon coins?
Code: Select all
local counter = 0
SaveData[Level.filename()] = SaveData[Level.filename()] or {}
local levelSD = SaveData[Level.filename()]
-- Saving Data
function onStart()
player.character=CHARACTER_TOAD
player.powerup=PLAYER_BIG
if levelSD.dragonCoinsCollected then
for k,v in ipairs(NPC.get(310)) do
v:kill(9)
end
end
end
-- 100% completion
function onDraw()
Text.print(counter,100 ,100)
if SaveData[Level.filename()].dragonCoinsCollected then
Text.print("Good job player!", 100, 100)
triggerEvent("Unlocked")
end
end
-- Adding Achievement
function onNPCKill(eventObj, killedNPC, killReason)
if killedNPC.id ~= 310 then return end
if killReason ~= HARM_TYPE_OFFSCREEN then return end
if Colliders.collide(player, killedNPC) then
counter = counter+1
if counter == 5 then
levelSD.dragonCoinsCollected = true
end
end
end
Spoiler: show