Inputs2.lua

From PGE Wiki
Jump to navigation Jump to search

Attention: This page contains dead links!

Download Latest Version

Inputs2 is a small library which makes it easier to disable and override the player's input. It is not fully backwards compatible with Inputs, but it doesn't take much effort to update old code.

Installation

Save the above file as inputs2.lua and drop it in the LuaScriptsLib folder.

How to use

To use this api, add this to your lunadll.lua or lunaworld.lua:

local inputs2 = API.load("inputs2");

When the API is loaded, it will monitor all of the key press states of each Player object and store them in the inputs2.state table. Instead of a basic boolean, the state will be stored as one of four constants:

inputs2.UP (untouched)
inputs2.PRESS (was just pressed)
inputs2.HOLD (currently held down)
inputs2.RELEASE (just released)

You can get the state for a given key by using the corresponding player index and key name in lowercase, like so:

player.jumpKeyPressing --> inputs2.state[1].jump
player2.dropItemKeyPressing --> inputs2.state[2].dropitem

inputs2[#].any gets the first of those states in that order from all of the Player object's inputs.

If inputs2.locked[#].keyname is set to true, the corresponding movement will be disabled without impacting inputs2.state[#].keyname. If inputs2.frozen[#].keyname is true, the respective inputs2.state[][] entry will stop updating and the associated Player object will react accordingly every frame the input stays frozen. A key can be locked and frozen simultaneously to disable movement and freeze the state, and the all key name can be used to lock and/or freeze all inputs for a given Player object.

The valid KEYNAMES you can use for this are:
"up", "down", "left", "right", "jump", "altjump", "run", "altrun", "dropitem", "pause", "all"

If inputs2.debug is set to true, the states of each key will be displayed on the side of the screen.


Example code

local inputs2 = API.load("inputs2");
    
inputs2.locked[1].altjump = true


function onLoop ()
    if  inputs2.state[1].altjump == inputs.PRESS  then
	playSFX(26)
	
	-- Get direction multiplier
	local dirMult = -1
	if  player:mem (0x106, FIELD_WORD) ~= -1  then
	    dirMult = 1
	end
	
	-- Spawn boomerang
	local boomerang = NPC.spawn (292, player.x + (32*dirMult), player.y, 1)
	boomerang.speedX = 12*dirMult + player.speedX
	boomerang:mem (0x110, FIELD_DFLOAT, 1)
    end
end

This will replace spin jumping with a boomerang, but only for player 1. Player 2 will function normally.

local inputs2 = API.load("inputs2");
    
local freezeFrames = {0,0}

function onLoop ()
    updateFreezeCounter (1)
    updateFreezeCounter (2)
end

function updateFreezeCounter (playerNum)
    -- Iterate the counter
    freezeFrames[playerNum] = freezeFrames[playerNum]-1

    -- Unfreeze after countdown
    if  freezeFrames[playerNum] <= 1  then
        inputs2.frozen[playerNum].left = false
        inputs2.frozen[playerNum].right = false
        inputs2.frozen[playerNum].run = false
    end
end

function freezeRun (playerNum)
    -- Freeze the states
    inputs2.frozen[playerNum].left = true
    inputs2.frozen[playerNum].right = true
    inputs2.frozen[playerNum].run = true

    -- Set the timer
    freezeFrames[playerNum] = 60
end

when called, freezeRun will prevent the specified player from changing their horizontal movement for 60 frames.

Documentation

Constants Description
inputs2.UP Value for a key that is up.
inputs2.PRESS Value for a key that was just pressed.
inputs2.HOLD Value for a key that is currently held down.
inputs2.RELEASE Value for a key that was just released.
Fields Description
inputs2.state[playerIndex][keyname] This field contains the value for a key (one of the above constants). Args:
playerIndex - The player pressing the key. Use values above 2 with caution.
keyname - a string containing the name of the key. All letters are lowercase.
inputs2.locked[playerIndex][keyname] This field is a boolean that can lock a key if true. Args:
playerIndex - The player pressing the key. Use values above 2 with caution.
keyname - a string containing the name of the key. All letters are lowercase. Use "all" to lock all keys.
inputs2.frozen[playerIndex][keyname] This field is a boolean that can freeze a key's state if true. Args:
playerIndex - The player pressing the key. Use values above 2 with caution.
keyname - a string containing the name of the key. All letters are lowercase. Use "all" to lock all keys.
inputs2.debug This field is a boolean that causes all the key states to be drawn to the screen if true.