Browse Source

Mention Object bracket property access and dynamic property indexing

This closes #3506.
Hugo Locurcio 5 years ago
parent
commit
0cba774249
1 changed files with 14 additions and 0 deletions
  1. 14 0
      getting_started/scripting/gdscript/gdscript_basics.rst

+ 14 - 0
getting_started/scripting/gdscript/gdscript_basics.rst

@@ -514,6 +514,20 @@ assign to it::
     d[4] = "hello" # Add integer 4 as a key and assign the String "hello" as its value.
     d["Godot"] = 3.01 # Add String "Godot" as a key and assign the value 3.01 to it.
 
+    var test = 4
+    # Prints "hello" by indexing the dictionary with a dynamic key.
+    # This is not the same as `d.test`. The bracket syntax equivalent to
+    # `d.test` is `d["test"]`.
+    print(d[test])
+
+.. note::
+
+    The bracket syntax can be used to access properties of any
+    :ref:`class_Object`, not just Dictionaries. Keep in mind it will cause a
+    script error when attempting to index a non-existing property. To avoid
+    this, use the :ref:`Object.get() <class_Object_method_get>` and
+    :ref:`Object.set() <class_Object_method_set>` methods instead.
+
 Data
 ----