Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the newly introduced is not operator #9777

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions tutorials/scripting/gdscript/static_typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,12 @@ get full autocompletion on the player variable thanks to that cast.

player.damage()

or ``assert()`` statement::
You can also simplify the code by using the ``is not`` operator::

if body is not PlayerController:
push_error("Bug: body is not PlayerController")

Alternatively, you can use the ``assert()`` statement::

assert(body is PlayerController, "Bug: body is not PlayerController.")

Expand All @@ -279,6 +284,7 @@ get full autocompletion on the player variable thanks to that cast.

player.damage()


.. note::

If you try to cast with a built-in type and it fails, Godot will throw an error.
Expand Down Expand Up @@ -407,10 +413,10 @@ that has a script attached with ``class_name MyScript`` and that ``extends
Node2D``. If we have a reference to the object as a ``Node2D`` (for instance,
as it was passed to us by the physics system), we can first check if the
property and method exist and then set and call them if they do::

if "some_property" in node_2d:
node_2d.some_property = 20 # Produces UNSAFE_PROPERTY_ACCESS warning.

if node_2d.has_method("some_function"):
node_2d.some_function() # Produces UNSAFE_METHOD_ACCESS warning.

Expand All @@ -420,7 +426,7 @@ in the referenced type - in this case a ``Node2D``. To make these operations
safe, you can first check if the object is of type ``MyScript`` using the
``is`` keyword and then declare a variable with the type ``MyScript`` on
which you can set its properties and call its methods::

if node_2d is MyScript:
var my_script: MyScript = node_2d
my_script.some_property = 20
Expand All @@ -443,7 +449,7 @@ collision area to show the area's name. Once the object enters the collision
area, the physics system sends a signal with a ``Node2D`` object, and the most
straightforward (but not statically typed) solution to do what we want could
be achieved like this::

func _on_body_entered(body: Node2D) -> void:
body.label.text = name # Produces UNSAFE_PROPERTY_ACCESS warning.

Expand Down