Last active
June 5, 2022 12:27
-
-
Save XanderCodes/da34f3380882295797e7ef9bb7274b17 to your computer and use it in GitHub Desktop.
Unturned Mod Keybind Handler (thanks Shimmy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credit for this code goes to ShimmyMySherbert - They kindly gave it to me when I asked about this in the ImperialPlugins Discord server. | |
using System; | |
using SDG.Unturned; | |
using UnityEngine; | |
namespace InputListener | |
{ | |
public delegate void PlayerKeyInputArgs(Player player, EPlayerKey key, bool down); | |
public class PlayerInputListener : MonoBehaviour | |
{ | |
public static event PlayerKeyInputArgs PlayerKeyInput; | |
public PlayerInput Input { get; private set; } | |
private bool[] m_KeyStates = new bool[0]; | |
private void Awake() | |
{ | |
Input = GetComponentInParent<PlayerInput>(); | |
if (Input == null) | |
{ | |
throw new InvalidOperationException("Must be attached to a Player"); | |
} | |
m_KeyStates = new bool[Input.keys.Length]; | |
} | |
private void FixedUpdate() | |
{ | |
for (int i = 0; i < m_KeyStates.Length; i++) | |
{ | |
if (m_KeyStates[i] != Input.keys[i]) | |
{ | |
m_KeyStates[i] = Input.keys[i]; | |
RaiseFor(i, m_KeyStates[i]); | |
} | |
} | |
} | |
private void RaiseFor(int key, bool state) | |
{ | |
PlayerKeyInput?.Invoke(Input.player, (EPlayerKey)key, state); | |
} | |
} | |
public enum EPlayerKey : int | |
{ | |
Jump = 0, | |
Primary = 1, | |
Secondary = 2, | |
Crouch = 3, | |
Prone = 4, | |
Sprint = 5, | |
LeanLeft = 6, | |
LeanRight = 7, | |
Nill = 8, | |
HotKey1 = 9, | |
HotKey2 = 10, | |
HotKey3 = 11, | |
HotKey4 = 12, | |
HotKey5 = 13 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Load() | |
{ | |
PlayerInputListener.PlayerKeyInput += OnPlayerInput; | |
} | |
private void OnPlayerInput(Player player, EPlayerKey key, bool down) | |
{ | |
if (down) | |
{ | |
Console.WriteLine($"Player pressed {key}!"); | |
} | |
else | |
{ | |
Console.WriteLine($"Player released {key}!"); | |
} | |
} | |
public void PlayerJoined(Player player) // pseudocode | |
{ | |
player.gameObject.AddComponent<PlayerInputListener>(); | |
} |
How do you check what the key code is for something.
private void OnPlayerInput(Player player, EPlayerKey key, bool down)
{
if (key == EPlayerKey.HotKey1)
{
Console.WriteLine($"Player pressed {key}!");
}
}
Is this what you mean?
Na i ment that you could check the PlayerKey for when like the skills key is pressed and stuff.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you check what the key code is for something.