Skip to content

Commit

Permalink
docs: update README for controller extension changes
Browse files Browse the repository at this point in the history
* Rewrite section on extension methods to describe event-based input
  simulation instead of old action-based simulation
* Point to action-simulation section for use in simulating controller
  actions
  • Loading branch information
wlsnmrk committed Apr 3, 2024
1 parent abe4017 commit ef550f2
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,28 +331,48 @@ node.TypeKey(KeyList.A);

### Simulating controller input

Controller input in Godot [is handled through mapped actions](https://docs.godotengine.org/en/stable/tutorials/inputs/controllers_gamepads_joysticks.html#which-input-singleton-method-should-i-use). Support for direct action input is described below, but as a convenience, GodotTestDriver provides some extension methods for analog controller axes:
GodotTest provides a number of extension functions on `SceneTree`/`Node` that allow you to simulate controller input using Godot's [`InputEventJoypadButton`](https://docs.godotengine.org/en/stable/classes/class_inputeventjoypadbutton.html#class-inputeventjoypadbutton) and [`InputEventJoypadMotion`](https://docs.godotengine.org/en/stable/classes/class_inputeventjoypadmotion.html#class-inputeventjoypadmotion) events.

```csharp
// you can set unidirectional axes like gamepad triggers with values from 0 to 1
node.SetControllerSingleAxis("left_trigger", 0.6f);
node.SetControllerSingleAxis("left_trigger", 0.2f);
// end unidrectional axis input
node.ReleaseControllerSingleAxis("left_trigger");

// you can also set bidirectional axes, like the x-axis of a thumbstick, from -1 to 1
node.SetControllerDoubleAxis("move_left", "move_right", -0.3f);
node.SetControllerDoubleAxis("move_left", "move_right", 0.7f);
// end bidirectional axis input
node.ReleaseControllerDoubleAxis("move_left", "move_right");

// hold an axis input for 1 second
await node.HoldControllerSingleAxisFor(1.0f, "left_trigger", 0.6f);
// hold a bidirectional axis input for 1 second
await node.HoldControllerDoubleAxisFor(1.0f, "move_left", "move_right", -0.3f);
// you can press down a controller button
node.PressJoypadButton(JoyButton.Y);

// you can release a controller button
node.ReleaseJoypadButton(JoyButton.Y);

// you can specify a particular controller device
var deviceID = 0;
node.PressJoypadButton(JoyButton.Y, deviceID);
node.ReleaseJoypadButton(JoyButton.Y, deviceID);

// you can simulate pressure for pressure-sensitive devices
var pressure = 0.8f;
node.PressJoypadButton(JoyButton.Y, deviceID, pressure);
node.ReleaseJoypadButton(JoyButton.Y, deviceID);

// you can combine pressing and releasing a button
node.TapJoypadButton(JoyButton.Y, deviceID, pressure);

// you can move an analog controller axis to a given position, with 0 being the rest position
// for instance:
// * a gamepad trigger will range from 0 to 1
// * a thumbstick's x-axis will range from -1 to 1
node.MoveJoypadAxisTo(JoyAxis.RightX, -0.3f);

// you can release a controller axis (equivalent to setting its position to 0)
node.ReleaseJoypadAxis(JoyAxis.RightX);

// you can specify a particular controller device
node.MoveJoypadAxisTo(JoyAxis.RightX, -0.3f, deviceID);
node.ReleaseJoypadAxis(JoyAxis.RightX, deviceID);

// hold a controller button for 1.5 seconds
await node.HoldJoypadButtonFor(1.5f, JoyButton.Y, deviceID, pressure);
// hold a controller axis position for 1.5 seconds
await node.HoldJoypadAxisFor(1.5f, JoyAxis.RightX, -0.3f, deviceID);
```

To simulate controller button input, use action simulation.
To simulate [controller input using mapped actions](https://docs.godotengine.org/en/stable/tutorials/inputs/controllers_gamepads_joysticks.html#which-input-singleton-method-should-i-use), for use with Godot's `Input.GetActionStrength()`, `Input.GetAxis()`, and `Input.GetVector()` methods, see the next section.

### Simulating other actions

Expand Down

0 comments on commit ef550f2

Please sign in to comment.