Rebuilding Procedural Playground by hand
Stage 10 of 11: all the posts
  1. Before you start
  2. 1A new project
  3. 2Leela's model and texture
  4. 3The ground, the sun and the dusk
  5. 4Second-order dynamics
  6. 5The camera
  7. 6The picture
  8. 7Leela's scene
  9. 8Standing and walking on physics
  10. 9Her bones
  11. 10The player's keys
  12. 11Going further
  13. Last: full screen
  14. How this was checked

Stage 10 of 11

The player's keys

By the end

  • Her scene finished, with the same four children as the repo's: Shape, Model, Rig, Parts.
  • robots/robots.gd on the Robots node. WASD walks her from the start, Space jumps, and clicks knock her parts off and send them home.

One more script takes over what you've been doing by hand: robots/robots.gd, on the Robots node, is what the player does. It says which robot is steered, and passes on the jump key and the mouse's clicks.

It also reads clicks for a feature this tutorial only names: knocking her parts off. That needs one more node in her scene, or a left click stops the game with an error. So this stage finishes her scene first.

Finish her scene: Parts

  1. Create robots/piece.gd, and then robots/parts.gd (it loads the first). They aren't walked through here.
  2. In leela.tscn, add a Node under Leela, name it Parts, and drag parts.gd onto it. It must come below Rig in the Scene dock: children are made ready from the top down, and Parts asks Rig for her skeleton as it starts.
  3. Select Leela, and press the Dictionary (size 0) button beside Part Masses. It opens with a New Key and a New Value, both <null>.
  4. Press the pencil button beside each to choose its type: String for the key and float for the value. (The list is long, and in the alphabet's order after its first three.) They keep those types for the entries after.
  5. Add three entries. For each, type the key, type the value, and press Add Key/Value Pair:
KeyValue
head6
leg.L5
leg.R5
Clip · 1:07 · no sound Steps 2 to 5, in leela.tscn: a Node added under Leela and renamed Parts (F2 renames), parts.gd dragged onto it from the FileSystem dock, then Part Masses on Leela: the two types chosen once, and the three entries. A new node goes last, which is where this one has to be: below Rig.

That's the weight, in kg, that each part takes with it. Her scene now has the same four children as the repo's: Shape, Model, Rig, Parts.

The script

  1. Create robots/robots.gd.
  2. In main.tscn, drag it onto Robots.
  3. Set First to Leela with Assign....

How it works

Run it now if you like: the checkpoint at the end of this post says what to expect. First, what the script does.

At the start it collects its robots, and steers one:

robots/robots.gd lines 23–30
func _ready() -> void:
	for child in get_children():
		if child is RigidBody3D and child.has_method("lose"):
			if child.visible:
				_robots.append(child)
			else:  # (hidden: nothing of it runs, and it leaves the physics)
				child.process_mode = Node.PROCESS_MODE_DISABLED
	_switch_to.call_deferred(maxi(_robots.find(first), 0))

A robot is any child that's a RigidBody3D with a lose function, which is to say one running robot.gd. A hidden one is left out and switched off: that's how the repo keeps George in its scene without him being in play.

_switch_to() is what was ticking controlled for you:

robots/robots.gd lines 64–73
func _switch_to(index: int) -> void:
	if _robots.is_empty():
		return
	active = posmod(index, _robots.size())
	for i in _robots.size():
		_robots[i].controlled = i == active
		_robots[i].follow = follow and i == active
	var camera := get_viewport().get_camera_3d()
	if camera and "target" in camera:
		camera.target = _robots[active]

It also hands the camera its target, so the camera follows whoever's steered.

And the keys:

robots/robots.gd lines 39–46
	if key and key.pressed and not key.echo:
		if key.keycode == KEY_TAB:
			_switch_to(active + 1)
		elif key.keycode == KEY_SPACE:
			_robots[active].jump()
		elif key.keycode == KEY_F:
			follow = not follow
			_switch_to(active)

These are read straight from key events, not through the input map. Only steering needs the input map, because the stick, the arrows and WASD all have to come out as one direction.

Checkpoint

Run it. No more ticking boxes.

  • WASD, the arrows or the left stick walk her from the start.
  • Space (or a pad's A) jumps.
  • Left-click her head or a leg and it comes off and falls. Right-click the piece and it flies home.
  • F switches chasing the mouse's point on and off. Tab would switch robots; with one, it does nothing.

If not:

  • An error naming "Parts" on a left click: that node is missing or misnamed.
  • An error in parts.gd as the game starts: Parts is above Rig in the Scene dock, so it found a Rig that wasn't ready yet. Or Rig is misnamed.