Run faster
You'll learn: what a line of code is, and how one number changes a game.
Time: 5 minutes.
Try itOpen this lesson in the Bitflinger Games app to try it.
The game
This lesson's game is built on Starter World: a meadow with trees, lamps and ten coins to collect. Play it first. Run around and get a feel for how fast you move.
The code
Open layer.gd. It holds this:
extends Node
## Lesson 1: Run faster. Change the number, then play.
func _ready() -> void:
Variations.set_rule("player.speed", 7.0)
Most of that is the same in every layer, and you can leave it alone for now. The line that matters is the last one:
Variations.set_rule("player.speed", 7.0)
Read it from left to right:
Variationsis how your code talks to the game.set_rulemeans "change a rule". A rule is a named setting that the game reads, such as how fast you run or how high you jump."player.speed"is the name of the rule. It's in quotes because it's text.7.0is the new value. It's a number, so it has no quotes.
The line starts with a tab. That tells the game the line belongs to _ready,
the part of your layer that runs once, when the game starts.
Change it
Change 7.0 to 14.0:
Variations.set_rule("player.speed", 14.0)
Play again. You run twice as fast.
Try this
- Try
3.0. Is the game harder or easier when you're slow? - Try
40.0. Can you still steer? - Find a speed that feels best to you, and keep it.
What you learned
- A game reads rules, and
Variations.set_rulechanges one. - A rule has a name in quotes and a value.
- Code inside
_readyruns once, when the game starts.
Next: Make it dusk.