Skip to content
Ciro Continisio edited this page Jan 23, 2021 · 2 revisions

Input in Chop Chop uses Unity's "new" Input System package. On top of that, we have built some intermediate layers to process the input coming from physical devices and handle the scenarios required by this particular game.

Table of Contents

Basic setup

Input Actions mapping

To read the Actions from the InputActions asset (pictured above, and which you can find in /Settings/Input/GameInput.inputactions), we use what is called Generate C# Class, which creates a C# file that contains the interfaces we implement in the scripts. This removes the need to look up for Actions using a string, and allows us to have auto-completion in code. You can find more info on this technique in the package manual.

The file generated can be found under /Scripts/Input/GameInput.cs.

Input processing

Once we have the C# file, we create callbacks inside a ScriptableObject called "InputReader", of which only one instance exists in the game. All GameObjects that need to access input reference this ScriptableObject, so they are all reading from the same source. The reason for this layer is that we process some of the input before making it available to scripts.

For instance, here's an extract from InputReader.cs. Here we first have the delegate UnityActions, which other scripts hook into. When the Input System invokes the callback OnJump as a result of pressing or releasing the jump button, we evaluate the phase of the input ('performed' or 'canceled') and fire the appropriate UnityAction to create two different phases for the jump (ascending and descending) based on the same button.

// Gameplay
public event UnityAction jumpEvent = delegate { };
public event UnityAction jumpCanceledEvent = delegate { };

public void OnJump(InputAction.CallbackContext context)
{
	if (context.phase == InputActionPhase.Performed)
		jumpEvent.Invoke();

	if (context.phase == InputActionPhase.Canceled)
		jumpCanceledEvent.Invoke();
}

Action Maps

We use Action Maps to differentiate between different phases of gameplay. We have so far:

  • Gameplay: This is active whenever you have control of the Pig Chef character, and it includes moving around, jumping, fighting, and interacting with cooking pot, NPCs, plus pausing the game.
  • Menus: Active in all menus (main menu, settings, pause menu, inventory) and includes mostly moving the selection around, confirming, canceling, and unpausing (only works in the pause menu).
  • Dialogues: Similar to Menus, this includes only moving the selection around and confirming. It's used in dialogues with multiple choices, to decide which answer to give. Cutscenes, having no multiple choice dialogues, don't fall into the Dialogues Action Map even though a character might be talking.

Switching Action Maps happens in the InputReader.cs class (see above), through functions like EnableDialogueInput() or DisableAllInput() (useful for cutscenes).

More info

Home
Video materials

Basics

World building and Graphics

Game architecture

The game systems explained, with API examples. For programmers.

Game design

How-tos for designers to expand the game's gameplay.

  • Adding quests
  • Adding items
  • Creating dialogues
  • Making a cutscene
Clone this wiki locally