It would hardly be any work, since we have a robust saving system.
Take this piece of code for example:
Code: Select all
local npcManager = require("npcManager") -- Load a library that is helpful for checking whether item are collected
SaveData.collectibles = SaveData.collectibles or { -- Create a table of collectibles. SaveData is special because it saves whenever SMBX saves.
crystals = 0,
gems = 0,
}
local collectibleIDNames = { -- Maps a NPC ID to the names. I use red coin and sonic ring here.
[103] = "crystals",
[153] = "gems"
}
function onPostNPCKill(id, reason) -- Once an NPC has died
if collectibleIDNames[id] and npcManager.collected(id, reason) then -- Check if it's one we care for and if it was collected
SaveData.collectibles[collectibleIDNames[id]] = SaveData.collectibles[collectibleIDNames[id]] + 1 -- And increment the respective counter
end
end
function onDraw()
Text.print(SaveData.collectibles.crystals, 100, 100) -- Draw the value of the counters
Text.print(SaveData.collectibles.gems, 100, 120)
end
This is basically the functional aspect of it in its simplest form. There's a counter (or two) and we increment it when an NPC of a specific ID is collected by the player. The segment in onDraw just draws the number contained within. It can be expanded to have more elaborate constructs of Graphics.drawImage too, like here if you have a "gems.png" in the episode folder:
Code: Select all
local gemsImg = Graphics.loadImage(Misc.resolveFile("gems.png"))
function onDraw()
Graphics.drawImage(gemsImg, 100, 100)
Text.print(SaveData.collectibles.gems, 140, 100) -- print text 40 pixels to the right of the left edge of the image
end
More information about text and image drawing functions can be found here:
https://wohlsoft.ru/pgewiki/LunaLua_global_functions
More information about tables like SaveData can be found in the official lua documentation:
https://www.tutorialspoint.com/lua/lua_tables.htm
They're a bit like arrays, if you've heard of those.
If you then want to check the value of these, I recommend making 3 events in SMBX. Maybe one called "checkCrystals", "checkCrystalsPassed" and "checkCrystalsFailed". Then you can do this:
Code: Select all
function onEvent(eventname)
if eventname == "checkCrystals" then
if SaveData.collectibles.crystals >= 10 then
triggerEvent("checkCrystalsPassed")
else
triggerEvent("checkCrystalsFailed")
end
end
end
This will execute checkCrystalsPassed only when the player has 10 or more. You can add more checks by duplicating the if statement with different event names.
Hope this makes sense. Lemme know if any questions arise.