Code to lock inputs in order to unlock them with SaveData

Share guides you have written for SMBX, and related tools here.

Moderator: Userbase Moderators

AlphaBlue1011
Fighter Fly
Fighter Fly
Posts: 31
Joined: Sun Feb 02, 2025 3:00 pm
Flair: never underestimate hyperfixations :3
Pronouns: she/her or they/them

Code to lock inputs in order to unlock them with SaveData

Postby AlphaBlue1011 » Tue Jul 01, 2025 3:08 pm

I finally have enough LunaLua experience (and a bit of browsing through code examples on these forums) to do something neat. And that thing is... this. A way to disable certain buttons and re-enable them with SaveData.

Code: Select all

-- This initializes the SaveData.
SaveData.altJumpAvailable = SaveData.altJumpAvailable or false
SaveData.altRunAvailable = SaveData.altRunAvailable or false
SaveData.reserveAvailable = SaveData.reserveAvailable or false

-- This is recommended if you want to disable the reserve box, as it allows for hiding its graphics.
local hudoverride = require("hudoverride")

-- This allows the inputs if the corresponding SaveData is activated beforehand. If it's available, it'll make sure it's available. If not, it'll lock the input. onTick is NEEDED for locking inputs.
function onStart()
	function onTick()
		if SaveData.altJumpAvailable then
		else
			player.keys.altJump = nil
		end
		if SaveData.altRunAvailable then
		else
			player.keys.altRun = nil
		end
		if SaveData.reserveAvailable then
		else
			player.keys.dropItem = nil
			player.reservePowerup = 0
			hudoverride.visible.itembox = false
		end
	end
end

-- Change the names in quotation marks (such as "altJumpUnlocked") to whatever event you want to use to unlock each input.
function onEvent(eventName)
	if eventName == "altJumpUnlocked" then
		SaveData.altJumpAvailable = true
	end
	if eventName == "altRunUnlocked" then
		SaveData.altRunAvailable = true
	end
	if eventName == "reserveUnlocked" then
		SaveData.reserveAvailable = true
	end
	if eventName == "resetUnlocks" then
		SaveData.altJumpAvailable = false
		SaveData.altRunAvailable = false
		SaveData.reserveAvailable = false
	end
end
The only issue I came across is that I couldn't figure out how to hide or reveal the graphics for the reserve box without reloading the current level. But other than that there shouldn't be any issues (that I know of).

Added in 24 minutes 5 seconds:
Here's an example of how it works in action (with a controller overlay so you can see that yes, inputs are being enabled and disabled).

Image

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

Re: Code to lock inputs in order to unlock them with SaveData

Postby Emral » Tue Jul 01, 2025 6:44 pm

To make the itembox visible at runtime, you can simply set hudoverride.visible.itembox = SaveData.reserveAvailable in onTick I believe.

You could also turn this into a library and post it in the lunalua forum.


Return to “Guides”

Who is online

Users browsing this forum: No registered users and 1 guest

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari