When the game says no
You'll learn: why some rules can't be changed, and how to tell when a
change didn't work.
Time: 10 minutes.
Try itOpen this lesson in the Bitflinger Games app to try it.
Jump higher
func _ready() -> void:
Variations.set_rule("player.jump_height", 2.6)
Change 2.6 to 6.0 and play. You can reach the tops of the trees.
Now try gravity
Lower gravity would make you float. Add a line:
func _ready() -> void:
Variations.set_rule("player.jump_height", 6.0)
Variations.set_rule("world.gravity", 6.0)
Play. Nothing changed. Gravity is still the same.
Why
Whoever makes a world decides what other people may change. Starter World's maker let you change almost every rule, but kept gravity for themselves. That's called sealing a rule. A sealed rule stays the way the maker set it, in every game built on theirs.
The game doesn't crash when you try. It ignores the change, keeps running, and writes it down in its report. Yours says:
learn-says-no may not change rules.world.gravity
Asking before you change
set_rule answers you. It gives back true if the change worked and false
if it was refused. You can use that answer:
func _ready() -> void:
Variations.set_rule("player.jump_height", 6.0)
if not Variations.set_rule("world.gravity", 6.0):
Variations.set_rule("player.double_jump", false)
Read the if line as "if changing gravity did not work, then...". The line
under it starts with one more tab, so it only runs when the if is true. Here,
because gravity can't change, the game turns off double jump instead, so your
high single jump is the only way up.
You can also ask first, without changing anything, using
Variations.allowed("rules.world.gravity"). It gives true or false too.
What you learned
- A world's maker can seal rules, and nobody building on it can change them.
- A refused change doesn't break the game. It's listed under Problems.
set_ruleanswerstrueorfalse, andiflets your code act on the answer.
Next: Say hello.