Created
October 18, 2018 15:11
-
-
Save xphere/e7d7fc3915a0c2c1e1957ba83a996aed to your computer and use it in GitHub Desktop.
Variable extraction changes outcome when inserting into dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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