소스 검색

Show example of casting get_node() for inferred static typing in style guide

zacryol 3 년 전
부모
커밋
35211f6002
2개의 변경된 파일14개의 추가작업 그리고 0개의 파일을 삭제
  1. 12 0
      tutorials/scripting/gdscript/gdscript_styleguide.rst
  2. 2 0
      tutorials/scripting/gdscript/static_typing.rst

+ 12 - 0
tutorials/scripting/gdscript/gdscript_styleguide.rst

@@ -889,6 +889,18 @@ should set the type explicitly.
 
    onready var health_bar: ProgressBar = get_node("UI/LifeBar")
 
+Alternatively, you can use the ``as`` keyword to cast the return type, and
+that type will be used to infer the type of the var.
+
+.. rst-class:: code-example-good
+
+::
+
+   onready var health_bar := get_node("UI/LifeBar") as ProgressBar
+   # health_bar will be typed as ProgressBar
+
+This option is also considered more :ref:`type-safe<doc_gdscript_static_typing_safe_lines>` than the first.
+
 **Bad**:
 
 .. rst-class:: code-example-bad

+ 2 - 0
tutorials/scripting/gdscript/static_typing.rst

@@ -178,6 +178,8 @@ get full autocompletion on the player variable thanks to that cast.
 
     If you try to cast with a built-in type and it fails, Godot will throw an error.
 
+.. _doc_gdscript_static_typing_safe_lines:
+
 Safe lines
 ^^^^^^^^^^