Skip to content

Instantly share code, notes, and snippets.

@xphere
Created October 18, 2018 15:11
Show Gist options
  • Save xphere/e7d7fc3915a0c2c1e1957ba83a996aed to your computer and use it in GitHub Desktop.
Save xphere/e7d7fc3915a0c2c1e1957ba83a996aed to your computer and use it in GitHub Desktop.
Variable extraction changes outcome when inserting into dictionary
extends Node
func _ready() -> void:
var values
# Bad setup: prints {a:40, b:42}
values = { "a": 0, "b": 0 }
for idx in range(0, 100):
values["a" if randf() > 0.9 else "b"] += 1
print(values)
# Good setup: prints {a:8, b:92}
values = { "a": 0, "b": 0 }
for idx in range(0, 100):
var value = "a" if randf() > 0.9 else "b"
values[value] += 1
print(values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment