Scripting 15 - Listening on player input
Scripting in SFD assumes you have a fair knowledge of C#.
The following code demonstrates how to listen on player key input. Available in v.1.3.1b.
Code: Select all
// Example script to listen on player key input
public void OnStartup()
{
// This registers key input from players as long as the players exist in the game. This will register input from dead and disabled players too (but not removed players)!
Events.PlayerKeyInputCallback.Start(OnPlayerKeyInput);
}
public void OnPlayerKeyInput(IPlayer player, VirtualKeyInfo[] keyEvents)
{
for (int i = 0; i < keyEvents.Length; i ++)
{
Game.WriteToConsole(string.Format("Player {0} keyevent: {1}", player.UniqueID, keyEvents[i].ToString()));
// Spawn a chair at the player if the player pressed the block button while holding WALK
if (keyEvents[i].Event == VirtualKeyEvent.Pressed && keyEvents[i].Key == VirtualKey.BLOCK && player.KeyPressed(VirtualKey.WALKING))
{
Game.CreateObject("Chair00", player.GetWorldPosition(), 0f, Vector2.Zero, 0f, player.FacingDirection);
}
// Example code to move a tile named "ABC" around by 8 units using the movement keys from any player:
if (keyEvents[i].Event == VirtualKeyEvent.Pressed)
{
Vector2 posChange = Vector2.Zero;
switch (keyEvents[i].Key)
{
case VirtualKey.AIM_RUN_RIGHT: posChange = Vector2.UnitX; break;
case VirtualKey.AIM_RUN_LEFT: posChange = -Vector2.UnitX; break;
case VirtualKey.AIM_CLIMB_UP: posChange = Vector2.UnitY; break;
case VirtualKey.AIM_CLIMB_DOWN: posChange = -Vector2.UnitY; break;
}
if (posChange != Vector2.Zero)
{
IObject obj = Game.GetObject("ABC");
if (obj != null) {
obj.SetWorldPosition(obj.GetWorldPosition() + posChange * 8f);
}
}
}
}
}
To listen on key input you must have a player instance to listen to (a technical limitation from the very first Pre-Alpha of the game).
If you don't care about the player you could set input mode to ReadOnly for the player and disable the nametags and move the player outside the camera area.
Code: Select all
foreach(IPlayer plr in Game.GetPlayers())
{
//plr.SetInputMode(PlayerInputMode.ReadOnly);
//plr.SetNametagVisible(false);
}
You can also check pressed keys yourself using the IPlayer.KeyPressed(..) function without using Events.PlayerKeyInputCallback depending on your needs.
Code: Select all
IPlayer plr = ...
if (plr.KeyPressed(VirtualKey.SPRINT))
{
...
}
Note: You can only read the pressed player action key. You can NOT read which physical button was pressed (e.g. if it's a keyboard/gamepad or which button it is). So if you're doing any sort of text prompt always refer to the action (e.g. "Attack", "Kick", "Block") instead of the physical buttons ("A", "F", "P") which are different depending on the user's key bindings.
ScriptAPI implementation for key input:
► Show Spoiler
IPlayer:
Code: Select all
/// <summary>
/// Gets if the virtual key is pressed for the player.
/// </summary>
/// <returns>bool</returns>
public abstract bool KeyPressed(VirtualKey key);
/// <summary>
/// Gets if the movement is flipped after walking into a portal.
/// </summary>
public abstract bool KeyMovementIsFlipped { get; }
/// <summary>
/// Gets if double tapping direction keys enable sprint.
/// Note: This is only up-to-date after the first key input has been registered.
/// </summary>
public abstract bool KeyDoubleTapSprintEnabled { get; }
/// <summary>
/// Gets if pressing attack and block should start the grab is enabled.
/// Note: This is only up-to-date after the first key input has been registered.
/// </summary>
public abstract bool KeyAttackAndBlockToGrabEnabled { get; }
/// <summary>
/// Gets input mode.
/// </summary>
public abstract PlayerInputMode InputMode { get; }
/// <summary>
/// Sets input mode.
/// </summary>
/// <param name="value">PlayerInputMode</param>
public abstract void SetInputMode(PlayerInputMode value);
VirtualKey, VirtualKeyInpu, VirtualKeyInfo:
Code: Select all
/// <summary>
/// A virtual key event
/// </summary>
[StructLayout(LayoutKind.Explicit)]
[Serializable()]
public struct VirtualKeyInfo
{
/// <summary>
/// Raw value
/// </summary>
[FieldOffset(0)]
public int Value;
/// <summary>
/// VirtualKey
/// </summary>
[FieldOffset(0)]
public VirtualKey Key;
/// <summary>
/// VirtualKeyEvent
/// </summary>
[FieldOffset(2)]
public VirtualKeyEvent Event;
}
/// <summary>
/// VirtualKeyEvent - Represents an action key event.
/// </summary>
public enum VirtualKeyEvent : short
{
/// <summary>
/// 0, Pressed. On key event DOWN
/// </summary>
Pressed = 0,
/// <summary>
/// 2, Released. On key event RELEASE
/// </summary>
Released = 2
}
/// <summary>
/// VirtualKey - Represents an action key in the game
/// </summary>
public enum VirtualKey : short
{
/// <summary>
/// -1, NONE
/// </summary>
NONE = -1,
/// <summary>
/// 0, Aim & Climb Up
/// </summary>
AIM_CLIMB_UP = 0,
/// <summary>
/// 1, Aim & Climb Down
/// </summary>
AIM_CLIMB_DOWN = 1,
/// <summary>
/// 2, Aim & Move Left
/// </summary>
AIM_RUN_LEFT = 2,
/// <summary>
/// 3, Aim & Move Right
/// </summary>
AIM_RUN_RIGHT = 3,
/// <summary>
/// 8, Draw Melee Weapon
/// </summary>
DRAW_MELEE = 8,
/// <summary>
/// 9, Draw Handgun Weapon
/// </summary>
DRAW_HANDGUN = 9,
/// <summary>
/// 10, Draw Rifle Weapon
/// </summary>
DRAW_RIFLE = 10,
/// <summary>
/// 11, Draw Grenades
/// </summary>
DRAW_GRENADE = 11,
/// <summary>
/// 12, Draw/Use Special Item
/// </summary>
DRAW_SPECIAL = 12,
/// <summary>
/// 4, Attack
/// </summary>
ATTACK = 4,
/// <summary>
/// 15, Kick
/// </summary>
KICK = 15,
/// <summary>
/// 5, Block
/// </summary>
BLOCK = 5,
/// <summary>
/// 26, Aim
/// </summary>
AIM = 26,
/// <summary>
/// 16, Activate/Interact
/// </summary>
ACTIVATE = 16,
/// <summary>
/// 13, Sheathe current drawn weapon/item
/// </summary>
SHEATHE = 13,
/// <summary>
/// 14, Reload
/// </summary>
RELOAD = 14,
/// <summary>
/// 17, Drop
/// </summary>
DROP = 17,
/// <summary>
/// 18, Sprint
/// </summary>
SPRINT = 18,
/// <summary>
/// 19, Jump
/// </summary>
JUMP = 19,
/// <summary>
/// 20, Crouch/Roll/Dive
/// </summary>
CROUCH_ROLL_DIVE = 20,
/// <summary>
/// 21, Walk
/// </summary>
WALKING = 21,
/// <summary>
/// 22, Grab
/// </summary>
GRAB = 22
}