Page 1 of 1

Code to lock inputs in order to unlock them with SaveData

Posted: Tue Jul 01, 2025 3:08 pm
by AlphaBlue1011
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

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

Posted: Tue Jul 01, 2025 6:44 pm
by Emral
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.