How to savedata in Lunalua

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
cato
Volcano Lotus
Volcano Lotus
Posts: 547
Joined: Thu Aug 24, 2017 3:06 am
Flair: Koishi enjoyer
Contact:

How to savedata in Lunalua

Postby cato » 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.

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
In a more advanced way, can we save data one by one after collecting each dragon coins?
Spoiler: show
Method 1: Change the variable after collecting dragon coins, then trigger an event if the variable is changed at the beginning of the level.
*Rejected by the devs due to a bug
Method 2: Identify a variable which is an NPC in a particular section, then set nil when that NPC is collected.
*Maybe works?
Method 3: Set a condition of finding the right NPC, then change the variable and check if the variable has been changed. Finally, kill that NPC in that section.
*Section: No such function exists; Layers: Doesn't work

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9737
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: How to savedata in Lunalua

Postby Emral » Wed Apr 24, 2019 12:13 pm

cato wrote:
Wed Apr 24, 2019 10:13 am
Method 1: Change the variable after collecting dragon coins, then trigger an event if the variable is changed at the beginning of the level.
*Rejected by the devs due to a bug
No... I just said that inside of onStart you might not be able to call triggerEvent currently due to a bug which I think is still around. You would still be able to call the event on the first frame of onTick instead, or avoid using events entirely.
cato wrote:
Wed Apr 24, 2019 10:13 am
Method 2: Identify a variable which is an NPC in a particular section, then set nil when that NPC is collected.
*Maybe works?
I already explained to you that setting a variable to nil will not kill the NPC, but just remove the reference. You recall I said you would have to call myNPCVariable:kill(killReason) where killReason is the kill type you want to apply.
cato wrote:
Wed Apr 24, 2019 10:13 am
*Section: No such function exists; Layers: Doesn't work
What? Sounds like you're just doing things wrong. Perhaps sharing the code that doesn't work would help in identifying what's wrong?

cato
Volcano Lotus
Volcano Lotus
Posts: 547
Joined: Thu Aug 24, 2017 3:06 am
Flair: Koishi enjoyer
Contact:

Re: How to savedata in Lunalua

Postby cato » Thu Apr 25, 2019 5:57 am

First, I would declare a variable using num1 = NPC.get(274, 0) --Section 0
Then I need to set up a condition using NPC. layerName, NPC.x / NPC.y or whatever I can to locate which Dragon Coins I collected.
Next, I assign a value to another variable.
When the level starts, if the value above changed, I would use num1:kill(9)
I don't know what happens next.
Also, is there a way to check the variable while playing the level using Developer's Console?

Edit: Tried making a variable true after triggered an event. Doesn't work.
Edit after above edit: error attempted to call a method "kill" ( a nil value). Safely assuming I cannot do the first step?
edit after above edit: It finally works as long as 1: I assign a variable to true after the event. 2: Kill use "for k,v in ipairs(NPC.get(274,0)) do v:kill(9)" 3: Re-add the counter

Added in 3 hours 18 minutes 8 seconds:
New codes for every 5-dragon coins levels:vPS: Every dragon coins must in different sections and must trigger an event, no matter what is inside of it

Code: Select all

local counter = 0

SaveData[Level.filename()] = SaveData[Level.filename()] or {}
local levelSD = SaveData[Level.filename()]

function onStart()
    player.character=CHARACTER_TOAD
    player.powerup=PLAYER_BIG
    if levelSD.Coincollected1 then 
        for k,v in ipairs(NPC.get(274, 0)) do  -- the 0 means the section 0 
        v:kill(9)
        counter = counter + 1
(copy above if there is more dragon coins in seperate section, just change the 0 to something else and also the variable name you used)
        end
    end
end

function onDraw()
    Text.print(counter,100 ,100)
end

function onNPCKill(eventObj, killedNPC, killReason)
    if killedNPC.id ~= 274 then return end  --check if it is a dragon coin

    if killReason ~= HARM_TYPE_OFFSCREEN then return end  

    if Colliders.collide(player, killedNPC) then 
               . . . . ( IDK what can you put if you have no event to trigger)
        counter = counter + 1
    end
    if counter == 5 then
        levelSD.dragonCoinsCollected = true
    end
end

function onEvent(eventName)
    if eventName == "open" then 
        levelSD.Coincollected1 = true
(copy above if there is more, change eventname and variable)
    end
end


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 0 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari