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.gdon theRobotsnode. 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
- Create
robots/piece.gd, and thenrobots/parts.gd(it loads the first). They aren't walked through here. - In
leela.tscn, add a Node underLeela, name itParts, and dragparts.gdonto it. It must come belowRigin the Scene dock: children are made ready from the top down, andPartsasksRigfor her skeleton as it starts. - Select
Leela, and press the Dictionary (size 0) button beside Part Masses. It opens with a New Key and a New Value, both<null>. - 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.
- Add three entries. For each, type the key, type the value, and press Add Key/Value Pair:
| Key | Value |
|---|---|
head | 6 |
leg.L | 5 |
leg.R | 5 |
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
- Create
robots/robots.gd. - In
main.tscn, drag it ontoRobots. - Set First to
Leelawith 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:
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:
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:
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.gdas the game starts:Partsis aboveRigin the Scene dock, so it found aRigthat wasn't ready yet. OrRigis misnamed.