diff --git a/tutorials/scripting/gdscript/gdscript_basics.rst b/tutorials/scripting/gdscript/gdscript_basics.rst index b34aef5797f..9f34b10c846 100644 --- a/tutorials/scripting/gdscript/gdscript_basics.rst +++ b/tutorials/scripting/gdscript/gdscript_basics.rst @@ -1146,6 +1146,9 @@ Member variables are initialized in the following order: To fix this, move the ``_data`` variable definition above the ``a`` definition or remove the empty dictionary assignment (``= {}``). + +.. _doc_gdscript_basics_static_variables: + Static variables ~~~~~~~~~~~~~~~~ @@ -1557,6 +1560,9 @@ Lambda functions capture the local environment: lambda.call() print(a) # Prints `[1]`. + +.. _doc_gdscript_basics_static_functions: + Static functions ~~~~~~~~~~~~~~~~ diff --git a/tutorials/scripting/gdscript/index.rst b/tutorials/scripting/gdscript/index.rst index 99eb3d2e28e..255451e85d4 100644 --- a/tutorials/scripting/gdscript/index.rst +++ b/tutorials/scripting/gdscript/index.rst @@ -16,6 +16,7 @@ GDScript gdscript_styleguide static_typing warning_system + warnings/index gdscript_format_string .. seealso:: diff --git a/tutorials/scripting/gdscript/warnings/assert_always_false.rst b/tutorials/scripting/gdscript/warnings/assert_always_false.rst new file mode 100644 index 00000000000..a9ab2be68e0 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/assert_always_false.rst @@ -0,0 +1,47 @@ +ASSERT_ALWAYS_FALSE +======================= + +The warning message is: + +.. code-block:: none + + Assert statement will raise an error because the expression is always false. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Assert Always False`. + +When this warning occurs +------------------------ + +The :ref:`assert() ` keyword can be used to ensure that a given condition is met before allowing code execution to continue. If the first argument passed to it is true in a boolean context, the rest of the function will run as expected; if it is false in a boolean context, then the project will stop. + +If ``assert()`` is passed something guaranteed to be false in a boolean context, then the ``assert()`` call will always stop the project. + +.. code-block:: + + # Zero always evaluates to false. + assert(0, "Zero is false in a boolean context") + + # Likewise, an empty string always evaluates to false. + assert("", "An empty string is false in a boolean context") + +.. note:: + + Godot will *not* raise this warning if a literal false boolean is passed: + + .. code-block:: + + # Despite false being passed, this won't raise ASSERT_ALWAYS_FALSE. + assert(false, "False is false") + + This is because ``assert(false)`` calls are often used in development to forcibly halt program execution and avoid strange errors later on. + + See `GH-58087 `_ for more information. + +How to fix this warning +----------------------- + +Assuming you want code following the ``assert()`` to run, remove it from your code. If you do want code execution to stop at that point, replace the condition with ``false``, or :ref:`consider using breakpoints instead `. + + + diff --git a/tutorials/scripting/gdscript/warnings/assert_always_true.rst b/tutorials/scripting/gdscript/warnings/assert_always_true.rst new file mode 100644 index 00000000000..f66849a9e35 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/assert_always_true.rst @@ -0,0 +1,36 @@ +ASSERT_ALWAYS_TRUE +====================== + +The warning message is: + +.. code-block:: none + + Assert statement is redundant because the expression is always true. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Assert Always True`. + +When this warning occurs +------------------------ + +The :ref:`assert() ` keyword can be used to ensure that a given condition is met before allowing code execution to continue. If the first argument passed to it evaluates to ``true``, the rest of the function will run as expected; if it is ``false``, then the project will stop. + +If ``assert()`` is passed an expression that is guaranteed to be ``true``, then the ``assert()`` call will never stop the project, thus making it redundant. + +.. code-block:: + + # The boolean true will always be true, so this assert will never stop + # the program. + assert(true, "True is false, somehow?") + + # Likewise, 3 will never be equal to 4, so this assert will never stop + # the program. + assert(3 != 4, "3 is equal to 4") + +How to fix this warning +----------------------- + +Remove the ``assert()`` statement from your code. + + + diff --git a/tutorials/scripting/gdscript/warnings/confusable_capture_reassignment.rst b/tutorials/scripting/gdscript/warnings/confusable_capture_reassignment.rst new file mode 100644 index 00000000000..d07781d321c --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/confusable_capture_reassignment.rst @@ -0,0 +1,25 @@ +CONFUSABLE_CAPTURE_REASSIGNMENT +=================================== + +The warning message is: + +.. code-block:: none + + Reassigning lambda capture does not modify the outer local variable "my_var". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Confusable Capture Reassignment`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/confusable_identifier.rst b/tutorials/scripting/gdscript/warnings/confusable_identifier.rst new file mode 100644 index 00000000000..8f7032ccbcd --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/confusable_identifier.rst @@ -0,0 +1,31 @@ +CONFUSABLE_IDENTIFIER +========================= + +The warning message is: + +.. code-block:: none + + The identifier "my_vаr" has misleading characters and might be confused with something else. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Confusable Identifier`. + +When this warning occurs +------------------------ + +Some scripts such as Cyrillic have characters that look like Latin (such as English, Spanish, etc.) characters, but are actually different. + +.. code-block:: + + var engine_nаme = "Godot" + print(engine_name) + +In this code snippet, the ``print`` statement would fail, because ``engine_name`` is actually not defined. The identifier in the ``print`` statement uses the Latin character "a" (U+0061), while the identifier in the variable declaration above uses the Cyrillic character "а" (U+0430). + +How to fix this warning +----------------------- + +Avoid using Cyrillic or other scripts' characters that are visually similar to Latin ones. A good rule of thumb is to prefer the Latin alphabet for program identifiers. + + + diff --git a/tutorials/scripting/gdscript/warnings/confusable_local_declaration.rst b/tutorials/scripting/gdscript/warnings/confusable_local_declaration.rst new file mode 100644 index 00000000000..a9ae0096bde --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/confusable_local_declaration.rst @@ -0,0 +1,25 @@ +CONFUSABLE_LOCAL_DECLARATION +================================ + +The warning message is: + +.. code-block:: none + + The variable "my_param" is declared below in the parent block. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Confusable Local Declaration`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/confusable_local_usage.rst b/tutorials/scripting/gdscript/warnings/confusable_local_usage.rst new file mode 100644 index 00000000000..e74344d7dc2 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/confusable_local_usage.rst @@ -0,0 +1,25 @@ +CONFUSABLE_LOCAL_USAGE +========================== + +The warning message is: + +.. code-block:: none + + The identifier "my_var" will be shadowed below in the block. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Confusable Local Usage`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/deprecated_keyword.rst b/tutorials/scripting/gdscript/warnings/deprecated_keyword.rst new file mode 100644 index 00000000000..dc7ea1f7148 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/deprecated_keyword.rst @@ -0,0 +1,25 @@ +DEPRECATED_KEYWORD +====================== + +The warning message is: + +.. code-block:: none + + The "..." keyword is deprecated and will be removed in a future release. Please replace it with "...". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Deprecated Keyword`. + +When this warning occurs +------------------------ + +There are currently no deprecated keywords in GDScript; as such, this warning should never appear. + + +How to fix this warning +----------------------- + +Follow instructions on the Godot Docs for how to use the alternative keyword. + + + diff --git a/tutorials/scripting/gdscript/warnings/empty_file.rst b/tutorials/scripting/gdscript/warnings/empty_file.rst new file mode 100644 index 00000000000..c89c2310a23 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/empty_file.rst @@ -0,0 +1,24 @@ +EMPTY_FILE +============== + +The warning message is: + +.. code-block:: none + + Empty script file. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Empty File`. + +When this warning occurs +------------------------ + +This warning may appear when you create a ``.gd`` file with no code to run. A completely blank file will raise this warning, as will a file that only contains comments. + +How to fix this warning +----------------------- + +Add code to the ``.gd`` file or delete it. + + + diff --git a/tutorials/scripting/gdscript/warnings/enum_variable_without_default.rst b/tutorials/scripting/gdscript/warnings/enum_variable_without_default.rst new file mode 100644 index 00000000000..1da063a0104 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/enum_variable_without_default.rst @@ -0,0 +1,55 @@ +ENUM_VARIABLE_WITHOUT_DEFAULT +================================= + +The warning message is: + +.. code-block:: none + + The variable "my_var" has an enum type and does not set an explicit default value. The default will be set to "0". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Enum Variable Without Default`. + +When this warning occurs +------------------------ + +This warning may appear when declaring a variable whose type is an enum with values assigned to its members, but the variable is not assigned a value. + +.. code-block:: + + enum MyEnum { + A = 1, + B = 2, + C = 3 + } + + func _ready(): + var my_var: MyEnum # Will give warning ENUM_VARIABLE_WITHOUT_DEFAULT. + +Godot will usually default an enum-typed variable to the integer ``0``. However, if the enum does not have a member that corresponds to ``0``, Godot will be confused on how to assign it. + +How to fix this warning +----------------------- + +Provide the variable with a default value, like so: + +.. code-block:: + + var my_var: MyEnum = MyEnum.A + +Alternatively, if the enum has a member with a value of 0, Godot will use that as the default value. + +.. code-block:: + + enum MyEnum { + Z = 0, # Will be used as the default value. + A = 1, + B = 2, + C = 3 + } + + func _ready(): + var my_var: MyEnum # Will default to MyEnum.Z. + + + diff --git a/tutorials/scripting/gdscript/warnings/get_node_default_without_onready.rst b/tutorials/scripting/gdscript/warnings/get_node_default_without_onready.rst new file mode 100644 index 00000000000..82650f96225 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/get_node_default_without_onready.rst @@ -0,0 +1,63 @@ +GET_NODE_DEFAULT_WITHOUT_ONREADY +==================================== + +This warning appears when the default value of a node's property is set to a location in the scene tree without using the ``@onready`` annotation. + +Depending on how you attempt to access the scene tree, the warning message will be one of the following: + +.. code-block:: none + + The default value uses "$" which won't return nodes in the scene tree before "_ready()" is called. Use the "@onready" annotation to solve this. + + The default value uses "%" which won't return nodes in the scene tree before "_ready()" is called. Use the "@onready" annotation to solve this. + + The default value uses "get_node()" which won't return nodes in the scene tree before "_ready()" is called. Use the "@onready" annotation to solve this. + +The default warning level for this warning is **Error**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Get Node Default Without Onready`. + +When this warning occurs +------------------------ + +Instance properties can be set by declaring them outside of a method. Additionally, they can be given a default value using ``=``:: + +.. code-block:: + + extends Area2D + + var my_num = 3 + +The property ``my_num`` will always start out with a value of ``3`` in each instance of this class. +GDScript also has methods to retrieve specific nodes from the scene tree: namely the :ref:`get_node() ` method, and its shorthand versions ``$`` (and ``%`` for unique nodes). Thus, if you want to have a property's default value be a particular child node, it may be tempting to write something like the following:: + +.. code-block:: + + extends Area2D + + var my_collision_shape = $CollisionShape2D + +However, properties' default values are evaluated and assigned before the scene tree is set up. This means that at the time of assigning the default value, the node may not be in the scene tree, and the variable will be set to ``null`` instead. + +How to fix this warning +----------------------- + +The most straightforward solution is to add the ``@onready`` annotation before your property declaration:: + +.. code-block:: + + extends Area2D + + @onready var my_collision_shape = $CollisionShape2D + +Now, the default value of the property will not be assigned until the scene tree has been initialized, and the node will be present. + +Alternatively, you can set the value of the property at the beginning of your ``_ready()`` method:: + +.. code-block:: + + extends Area2D + + var my_collision_shape + + func _ready(): + my_collision_shape = $CollisionShape2D \ No newline at end of file diff --git a/tutorials/scripting/gdscript/warnings/incompatible_ternary.rst b/tutorials/scripting/gdscript/warnings/incompatible_ternary.rst new file mode 100644 index 00000000000..eac5ae65200 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/incompatible_ternary.rst @@ -0,0 +1,25 @@ +INCOMPATIBLE_TERNARY +======================== + +The warning message is: + +.. code-block:: none + + Values of the ternary operator are not mutually compatible. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Incompatible Ternary`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/index.rst b/tutorials/scripting/gdscript/warnings/index.rst new file mode 100644 index 00000000000..ae7cc94e5b5 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/index.rst @@ -0,0 +1,60 @@ +:allow_comments: False + +.. _doc_gdscript_warnings: + +GDScript warnings +================= + +This is a collection of the warnings that GDScript can emit, and information on how to fix the code that causes them. + +.. rubric:: Warnings + :heading-level: 2 + +.. toctree:: + :maxdepth: 1 + :name: toc-gdscript-warnings + + unassigned_variable + unassigned_variable_op_assign + unused_variable + unused_local_constant + unused_private_class_variable + unused_parameter + unused_signal + shadowed_variable + shadowed_variable_base_class + shadowed_global_identifier + unreachable_code + unreachable_pattern + standalone_expression + standalone_ternary + incompatible_ternary + untyped_declaration + inferred_declaration + unsafe_property_access + unsafe_method_access + unsafe_cast + unsafe_call_argument + unsafe_void_return + return_value_discarded + static_called_on_instance + missing_tool + redundant_static_unload + redundant_await + assert_always_true + assert_always_false + integer_division + narrowing_conversion + int_as_enum_without_cast + int_as_enum_without_match + enum_variable_without_default + empty_file + deprecated_keyword + confusable_identifier + confusable_local_declaration + confusable_local_usage + confusable_capture_reassignment + inference_on_variant + native_method_override + get_node_default_without_onready + onready_with_export diff --git a/tutorials/scripting/gdscript/warnings/inference_on_variant.rst b/tutorials/scripting/gdscript/warnings/inference_on_variant.rst new file mode 100644 index 00000000000..4fc35bae3e7 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/inference_on_variant.rst @@ -0,0 +1,37 @@ +INFERENCE_ON_VARIANT +======================== + +The warning message is: + +.. code-block:: none + + The variable type is being inferred from a Variant value, so it will be typed as Variant. + +The default warning level for this warning is **Error**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Inference on Variant`. + +When this warning occurs +------------------------ + +This warning may appear when using the ``:=`` operator to declare a variable with an inferred type, when the inferred type is :ref:`Variant `. + +.. code-block:: + + func _ready(): + var my_var := get_value() # Will give warning INFERENCE_ON_VARIANT. + + func get_value(): + return 3 + +Because the return type of ``get_value()`` isn't explicitly stated to be ``int``, Godot won't assume that it only returns an ``int``, and thus will consider its return type to be ``Variant``. The ``:=`` operator will then only be able to infer the type of ``my_var`` to ``Variant``, which is effectively the same as not inferring a type for ``my_var`` at all. + +How to fix this warning +----------------------- + +If initializing a variable based on the return value of a function (like in the example above), give the function an explicit return type: + +.. code-block:: + + func get_value() -> int: + return 3 + diff --git a/tutorials/scripting/gdscript/warnings/inferred_declaration.rst b/tutorials/scripting/gdscript/warnings/inferred_declaration.rst new file mode 100644 index 00000000000..513eb084d9f --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/inferred_declaration.rst @@ -0,0 +1,33 @@ +INFERRED_DECLARATION +======================== + +The warning message is: + +.. code-block:: none + + "for" iterator variable "i" has an implicitly inferred static type. + +The default warning level for this warning is **Ignore**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Inferred Declaration`. + +When this warning occurs +------------------------ + +This warning may appear when creating a variable for a particular scope, such as a ``for`` loop, without specifying its type explicitly: + +.. code-block:: + + for i in 10: # Will give warning INFERRED_DECLARATION. + print(i) + +In this example, the variable ``i`` did not have its type specified. It is inferred to be an ``int``, but not outright stated in the code. + +How to fix this warning +----------------------- + +Provide a type specifier for the variable, like so: + +.. code-block:: + + for i: int in 10: # Will not give a warning. + print(i) diff --git a/tutorials/scripting/gdscript/warnings/int_as_enum_without_cast.rst b/tutorials/scripting/gdscript/warnings/int_as_enum_without_cast.rst new file mode 100644 index 00000000000..3efe1d9916f --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/int_as_enum_without_cast.rst @@ -0,0 +1,31 @@ +INT_AS_ENUM_WITHOUT_CAST +============================ + +The warning message is: + +.. code-block:: none + + Integer used when an enum value is expected. If this is intended, cast the integer to the enum type using the "as" keyword. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Int as Enum Without Cast`. + +When this warning occurs +------------------------ + +This warning may appear when attempting to use an integer value in place of an enum value: + +.. code-block:: + + var my_var: MyEnum + my_var = 1 # Will give warning INT_AS_ENUM_WITHOUT_CAST. + +How to fix this warning +----------------------- + +Cast the integer value to the enum type. + +.. code-block:: + + var my_var: MyEnum + my_var = 1 as MyEnum diff --git a/tutorials/scripting/gdscript/warnings/int_as_enum_without_match.rst b/tutorials/scripting/gdscript/warnings/int_as_enum_without_match.rst new file mode 100644 index 00000000000..4148f10b298 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/int_as_enum_without_match.rst @@ -0,0 +1,56 @@ +INT_AS_ENUM_WITHOUT_MATCH +============================= + +The warning message is: + +.. code-block:: none + + Cannot cast 3 as Enum "MyClass.MyEnum": no enum member has matching value. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Int as Enum Without Match`. + +When this warning occurs +------------------------ + +This warning may appear when attempting to cast an integer value to an enum type, but the enum type doesn't have a member with a corresponding value. + +.. code-block:: + + enum MyEnum { ZERO, ONE, TWO } + + func _ready(): + var my_var = 3 as MyEnum # Will give warning INT_AS_ENUM_WITHOUT_MATCH. + +The purpose of the enum is to keep track of a pre-determined number of possible values. In most cases, there is no reason to assign a value outside of those pre-determined ones, so it is considered likely to be a mistake. + +How to fix this warning +----------------------- + +Ensure that the enum type you're attempting to cast an integer to has a corresponding value. If it doesn't, it may be a sign that you're attempting to use the wrong integer. + +.. code-block:: + + enum MyEnum { ZERO, ONE, TWO } + + func _ready(): + var my_var = 2 as MyEnum # Will correspond to MyEnum.TWO + +If you're certain that the integer is correct, then modify the enum's list of values so that it has one that corresponds to the integer. + +.. code-block:: + + enum MyEnum { ZERO, ONE, TWO, THREE } + + func _ready(): + var my_var = 3 as MyEnum # Will now correspond to MyEnum.THREE. + +Remember that, while Godot will assign integer values to enum members by default, you can also explicitly define their corresponding values: + +.. code-block:: + + enum MyEnum { ZERO, ONE, TWO, TEN_THOUSAND = 10000 } + + func _ready(): + var my_var = 10000 as MyEnum # Will correspond to MyEnum.TEN_THOUSAND. + diff --git a/tutorials/scripting/gdscript/warnings/integer_division.rst b/tutorials/scripting/gdscript/warnings/integer_division.rst new file mode 100644 index 00000000000..d0b9a1150cc --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/integer_division.rst @@ -0,0 +1,41 @@ +INTEGER_DIVISION +==================== + +The warning message is: + +.. code-block:: none + + Integer division. Decimal part will be discarded. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Integer Division`. + +When this warning occurs +------------------------ + +This warning may appear when attempting to divide two integers: + +.. code-block:: + + var result = 5 / 3 # Will give warning INTEGER_DIVISION. + +Because both operands are integers, the result will be an integer as well. Integers can't store fractional parts of numbers, so the result must be a whole number. Godot discards anything after the decimal point in the mathematical result to obtain the integer result. **Note that the number is not rounded to the nearest whole number.** + + +How to fix this warning +----------------------- + +Use a floating-point number (``float``) for at least one operand of the division operation: + +.. code-block:: + + var result = 5.0 / 3 + +If the integers being divided are variables, cast them to ``float``: + +.. code-block:: + + var a: int = 5 + var b: int = 3 + var result = float(a) / float(b) + diff --git a/tutorials/scripting/gdscript/warnings/missing_tool.rst b/tutorials/scripting/gdscript/warnings/missing_tool.rst new file mode 100644 index 00000000000..f92ddbdfbfc --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/missing_tool.rst @@ -0,0 +1,25 @@ +MISSING_TOOL +================ + +The warning message is: + +.. code-block:: none + + The base class script has the "@tool" annotation, but this script does not have it. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Missing Tool`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/narrowing_conversion.rst b/tutorials/scripting/gdscript/warnings/narrowing_conversion.rst new file mode 100644 index 00000000000..3edd31634c7 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/narrowing_conversion.rst @@ -0,0 +1,25 @@ +NARROWING_CONVERSION +======================== + +The warning message is: + +.. code-block:: none + + Narrowing conversion (float is converted to int and loses precision). + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Narrowing Conversion`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/native_method_override.rst b/tutorials/scripting/gdscript/warnings/native_method_override.rst new file mode 100644 index 00000000000..41689b052eb --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/native_method_override.rst @@ -0,0 +1,37 @@ +NATIVE_METHOD_OVERRIDE +========================== + +The warning message is: + +.. code-block:: none + + The method "get_class()" overrides a method from native class "Object". This won't be called by the engine and may not work as expected. + +The default warning level for this warning is **Error**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Native Method Override`. + +When this warning occurs +------------------------ + +This warning may appear if you try to define a method in a script that has the same name as a native class's method: + +.. code-block:: + + extends Node + + func get_class(): # Will give warning NATIVE_METHOD_OVERRIDE. + return "MyCoolClass" + +Here, ``get_class()`` is a method on ``Object``. When Godot tries to call this class's ``get_class()`` method internally, it will use its own internal method, not the one the user has defined here. + +How to fix this warning +----------------------- + +Name the function something else: + +.. code-block:: + + func get_class_name(): # Will not give a warning. + return "MyCoolClass" + +(Note that doing this still won't override the behavior of a native method.) diff --git a/tutorials/scripting/gdscript/warnings/onready_with_export.rst b/tutorials/scripting/gdscript/warnings/onready_with_export.rst new file mode 100644 index 00000000000..593d2eee6af --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/onready_with_export.rst @@ -0,0 +1,25 @@ +ONREADY_WITH_EXPORT +======================= + +The warning message is: + +.. code-block:: none + + "@onready" will set the default value after "@export" takes effect and will override it. + +The default warning level for this warning is **Error**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Onready With Export`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/redundant_await.rst b/tutorials/scripting/gdscript/warnings/redundant_await.rst new file mode 100644 index 00000000000..6ba02fb4a3e --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/redundant_await.rst @@ -0,0 +1,25 @@ +REDUNDANT_AWAIT +=================== + +The warning message is: + +.. code-block:: none + + "await" keyword is unnecessary because the expression isn't a coroutine nor a signal. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Redundant Await`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/redundant_static_unload.rst b/tutorials/scripting/gdscript/warnings/redundant_static_unload.rst new file mode 100644 index 00000000000..d724c378d81 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/redundant_static_unload.rst @@ -0,0 +1,25 @@ +REDUNDANT_STATIC_UNLOAD +=========================== + +The warning message is: + +.. code-block:: none + + The "@static_unload" annotation is redundant because the file does not have a class with static variables. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Redundant Static Unload`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/return_value_discarded.rst b/tutorials/scripting/gdscript/warnings/return_value_discarded.rst new file mode 100644 index 00000000000..62764c75159 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/return_value_discarded.rst @@ -0,0 +1,41 @@ +RETURN_VALUE_DISCARDED +========================== + +The warning message is: + +.. code-block:: none + + The function "get_number()" returns a value that will be discarded if not used. + +The default warning level for this warning is **Ignore**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Return Value Discarded`. + +When this warning occurs +------------------------ + +This warning may appear if a method returns a value, but that value is not used in an expression or assigned to a variable: + +.. code-block:: + + func _ready(): + print("About to get a number...") + get_number() # Will give warning RETURN_VALUE_DISCARDED. + print("Got a number!") + + func get_number() -> int: + return 5 + +How to fix this warning +----------------------- + +Assign the returned value to a variable for use later. + +.. code-block:: + + func _ready(): + print("About to get a number...") + var num = get_number() + print("Got a number! It's %s" % num) + +However, some methods in Godot's APIs return values that are not necessary to store. As such, depending on the situation, it may make more sense to ignore this warning. + diff --git a/tutorials/scripting/gdscript/warnings/shadowed_global_identifier.rst b/tutorials/scripting/gdscript/warnings/shadowed_global_identifier.rst new file mode 100644 index 00000000000..f5830e0237b --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/shadowed_global_identifier.rst @@ -0,0 +1,42 @@ +SHADOWED_GLOBAL_IDENTIFIER +============================== + +The warning message is: + +.. code-block:: none + + The variable "char" has the same name as a built-in function. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Shadowed Global Identifier`. + +When this warning occurs +------------------------ + +This warning may appear when using a name for something that is already being used for a global identifier. Global identifiers include: + +* built-in functions (like ``char`` or ``convert``), +* native classes (like ``Node`` or ``Area2D``), +* global classes defined in another script file, +* and built-in types (like ``int`` or ``String``). + +When something is *shadowed*, its name is taken by something else, and as such, it can't be accessed anymore within that scope. For example, if you used the code: + +.. code-block:: + + var char = "A" # Will give warning SHADOWED_GLOBAL_IDENTIFIER. + +and later within this block of code, you wanted to convert an ``int`` ASCII/Unicode character point into a ``String`` (that's what the ``char()`` function does), you would be out of luck. + + +How to fix this warning +----------------------- + +Change the name of the identifier to something else that isn't the same as a global identifier. + +.. code-block:: + + var letter = "A" + +This code would not cause the warning to appear, since ``letter`` is not a global identifier. + diff --git a/tutorials/scripting/gdscript/warnings/shadowed_variable.rst b/tutorials/scripting/gdscript/warnings/shadowed_variable.rst new file mode 100644 index 00000000000..7c1fe24b895 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/shadowed_variable.rst @@ -0,0 +1,38 @@ +SHADOWED_VARIABLE +===================== + +The warning message is: + +.. code-block:: none + + The local variable "level" is shadowing an already-declared variable at line 3 in the current class. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Shadowed Variable`. + +When this warning occurs +------------------------ + +This warning may appear when giving something the same name as a variable previously defined in the class. + +.. code-block:: + + extends Node + + var level = 3 + + func _ready(): + # Will give warning SHADOWED_VARIABLE. + var level = 1 + print("Time for level %s" % level) + +In this example, the script class has a property ``level`` which can be accessed from its functions. However, at the first line of ``_ready()``, a new ``level`` variable is declared for that function specifically. After this declaration, any references to ``level`` will go to that version and not the shared variable for the class. This is called *shadowing*. + + +How to fix this warning +----------------------- + +Change the name to something that isn't being used by the class. For example, if receiving a warning about using the identifier ``level``, consider using something more descriptive like ``new_level``. + + + diff --git a/tutorials/scripting/gdscript/warnings/shadowed_variable_base_class.rst b/tutorials/scripting/gdscript/warnings/shadowed_variable_base_class.rst new file mode 100644 index 00000000000..a82536c4335 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/shadowed_variable_base_class.rst @@ -0,0 +1,36 @@ +SHADOWED_VARIABLE_BASE_CLASS +================================ + +The warning message is: + +.. code-block:: none + + The local variable "name" is shadowing an already-declared property in the base class "Node". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Shadowed Variable Base Class`. + +When this warning occurs +------------------------ + +This warning may appear when using a name for something that the script's base class already uses for something else. + +.. code-block:: + + extends Node + + func _ready(): + # Will give warning SHADOWED_VARIABLE_BASE_CLASS. + var name = "Bob" + print("Hi, my name is %s" % name) + +In this example, the ``Node`` class already defines ``name`` as the name associated with the node itself in the scene tree and editor. Before the ``var name`` declaration, writing ``name = "MyNode"`` would have set the node's name. After the declaration, though, the same line of code would not change it. In fact, after the ``var name``, the node's name can no longer be accessed within the ``_ready()`` function. This is called *shadowing*. + + +How to fix this warning +----------------------- + +Change the name to something that isn't being used by the base class. For example, if receiving a warning about using the identifier ``name``, consider using something more descriptive like ``char_name`` or ``item_name``. + + + diff --git a/tutorials/scripting/gdscript/warnings/standalone_expression.rst b/tutorials/scripting/gdscript/warnings/standalone_expression.rst new file mode 100644 index 00000000000..341997a2961 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/standalone_expression.rst @@ -0,0 +1,51 @@ +STANDALONE_EXPRESSION +========================= + +The warning message is: + +.. code-block:: none + + Standalone expression (the line may have no effect). + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Standalone Expression`. + +When this warning occurs +------------------------ + +This warning may appear when writing an expression that is not assigned to anything or passed to a function: + +.. code-block:: + + # Will give warning STANDALONE_EXPRESSION. + (5 * 3) + 2 + +This expression evaluates to the integer value ``17``, but after that, nothing happens to or with it. + +Note that the warning states the expression *may* have no effect, not that it *won't* have an effect. If the expression includes a function call that ends up doing more than just returning a value, the line itself ultimately will have an effect. + +.. code-block:: + + extends Node + + var counter = 0 + + func _ready(): + # Will give warning STANDALONE_EXPRESSION. + (5 * 3) + 2 * add_to_val(3) + + func add_to_val(val) -> int: + counter += 1 + return val + 2 + +Here, the line in ``_ready()`` receives the ``STANDALONE_EXPRESSION`` warning, even though the call to ``add_to_val()`` changes the ``counter`` property. The other math calculations won't have any effect. + +How to fix this warning +----------------------- + +If you've written an expression in your code, it's likely because you intended to use it for something. Make sure you're assigning it to a variable or passing it to a function. + +If you're certain that you don't need the expression for anything, remove it. Make sure to keep function calls that may have side effects (such as the ``add_to_val()`` call in the example above). + + + diff --git a/tutorials/scripting/gdscript/warnings/standalone_ternary.rst b/tutorials/scripting/gdscript/warnings/standalone_ternary.rst new file mode 100644 index 00000000000..1b949820f20 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/standalone_ternary.rst @@ -0,0 +1,53 @@ +STANDALONE_TERNARY +====================== + +The warning message is: + +.. code-block:: none + + Standalone ternary operator (the return value is being discarded). + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Standalone Ternary`. + +When this warning occurs +------------------------ + +This warning may appear when writing a ternary expression that is not being assigned to anything or passed into a function: + +.. code-block:: + + # Will give warning STANDALONE_TERNARY. + 3 if visible else 0 + +This ternary expression will return either ``3`` or ``0``, but that value won't carry over to anything or have any effect on the program. + +The warning may also appear when using a ternary operator to call different functions based on a condition: + +.. code-block:: + + # Will give warning STANDALONE_TERNARY. + chase_player() if can_see_player() else be_idle() + + + +How to fix this warning +----------------------- + +If the possible return values from the ternary expression are important, assign them to a variable so they aren't lost: + +.. code-block:: + + var number = 3 if visible else 0 + +For calling different functions as seen in the second example, consider splitting it into an if-else statement. While it takes a few more lines, they will be shorter and easier to read. + +.. code-block:: + + if can_see_player(): + chase_player() + else: + be_idle() + + + diff --git a/tutorials/scripting/gdscript/warnings/static_called_on_instance.rst b/tutorials/scripting/gdscript/warnings/static_called_on_instance.rst new file mode 100644 index 00000000000..24e730ae0bd --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/static_called_on_instance.rst @@ -0,0 +1,47 @@ +STATIC_CALLED_ON_INSTANCE +============================= + +The warning message is: + +.. code-block:: none + + The function "do_static_thing()" is a static function but was called from an instance. Instead, it should be directly called from the type: "MyClass.do_static_thing()". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Static Called on Instance`. + +When this warning occurs +------------------------ + +.. + It's been a problem with other pages too, but I worry this is getting a bit repetitive with the original warning message. + +This warning may appear when attempting to call a static function on an instance. + +.. code-block:: + + class MathFuncs: + static func subtract_two(val): + return val - 2 + + func _ready(): + var my_math_funcs = MathFuncs.new() + + # Will give warning STATIC_CALLED_ON_INSTANCE. + var result = my_math_funcs.subtract_two(5) + +When a function is *static*, it belongs to the class as a whole, not any one specific instance of the class. See :ref:`Static variables ` and :ref:`Static functions ` for more information. + +There is often no need to call a static function on an instance of a class. In the example above, the function ``subtract_two()`` will always perform the same operation; one instance of the ``MathFuncs`` class would never return a different value than another instance. As such, when the user tries to call ``subtract_two()`` from an instance of ``MathFuncs``, it suggests the user may not understand what the function does. + +How to fix this warning +----------------------- + +Don't use an instance of the class to access the function. Instead, use the class name itself: + +.. code-block:: + + var result = MathFuncs.subtract_two(5) + + + diff --git a/tutorials/scripting/gdscript/warnings/unassigned_variable.rst b/tutorials/scripting/gdscript/warnings/unassigned_variable.rst new file mode 100644 index 00000000000..1dbb569c02a --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unassigned_variable.rst @@ -0,0 +1,36 @@ +UNASSIGNED_VARIABLE +======================= + +The warning message is: + +.. code-block:: none + + The variable "my_var" is used before being assigned a value. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unassigned Variable`. + +When this warning occurs +------------------------ + +This warning may appear when attempting to use a variable that hasn't had a value assigned to it yet. + +.. code-block:: + + var my_var + print(my_var) + +By default, the variable will be ``null``. However, Godot considers this a warning because the user did not explicitly assign the value, and as such might be unaware of it. + +How to fix this warning +----------------------- + +Assign a value to the variable before including it in an expression or function call: + +.. code-block:: + + var my_var = 5 + print(my_var) + + + diff --git a/tutorials/scripting/gdscript/warnings/unassigned_variable_op_assign.rst b/tutorials/scripting/gdscript/warnings/unassigned_variable_op_assign.rst new file mode 100644 index 00000000000..c21235adf1e --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unassigned_variable_op_assign.rst @@ -0,0 +1,43 @@ +UNASSIGNED_VARIABLE_OP_ASSIGN +================================= + +The warning message is: + +.. code-block:: none + + The variable "my_counter" is modified with the compound-assignment operator "+=" but was not previously initialized. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unassigned Variable Op Assign`. + +When this warning occurs +------------------------ + +This warning may appear when attempting to use one of the compound-assignment operators, like ``+=``, ``-=``, ``*=``, or ``/=``, on a variable that hasn't had a value assigned to it yet. + +.. code-block:: + + var my_counter: int + my_counter += 1 + +Compound-assignment operators are a shorthand for performing a binary math operation and an assignment operation. The above example is equivalent to: + +.. code-block:: + + var my_counter: int + my_counter = my_counter + 1 + +Here, the variable ``my_counter`` hasn't been given a value at the time that the code attempts to use the value from ``my_counter`` in order to calculate the new value for it. + +How to fix this warning +----------------------- + +Assign an initial value to the variable before attempting to use a compound-assignment operator with it: + +.. code-block:: + + var my_counter: int = 0 + my_counter += 1 + + + diff --git a/tutorials/scripting/gdscript/warnings/unreachable_code.rst b/tutorials/scripting/gdscript/warnings/unreachable_code.rst new file mode 100644 index 00000000000..95da252a89a --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unreachable_code.rst @@ -0,0 +1,62 @@ +UNREACHABLE_CODE +==================== + +The warning message is: + +.. code-block:: none + + Unreachable code (statement after return) in function "_ready()". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unreachable Code`. + +When this warning occurs +------------------------ + +This warning may appear if there is code that can never be reached because all possible code paths before it would hit a ``return`` statement and leave a function: + +.. code-block:: + + func calculate_num(a): + if a > 5: + return 10 + else: + return 3 + + print("Returning zero") + return 0 + +In this function, there are only two ways the code can be run: + +* If ``a`` is greater than ``5``, the function will return ``10``, and the print statement will not be reached. +* If ``a`` is equal to or less than ``5``, the function will return ``3``, and again the print statement will not be reached. + +There is no way for code execution to get past the if-else block and to the print statement. + +How to fix this warning +----------------------- + +If you want the code marked with the warning to be run, modify your function's logic so that there is a path to that code. + +.. code-block:: + + func calculate_num(a): + if a > 5: + return 10 + elif a > 2: # Now this condition can be false, and the print statement can be reached. + return 3 + + print("Returning zero") + return 0 + +If the code with the warning isn't important, then remove it. + +.. code-block:: + + func calculate_num(a): + if a > 5: + return 10 + else: + return 3 + + diff --git a/tutorials/scripting/gdscript/warnings/unreachable_pattern.rst b/tutorials/scripting/gdscript/warnings/unreachable_pattern.rst new file mode 100644 index 00000000000..04d162e3ebf --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unreachable_pattern.rst @@ -0,0 +1,25 @@ +UNREACHABLE_PATTERN +======================= + +The warning message is: + +.. code-block:: none + + Unreachable pattern (pattern after wildcard or bind). + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unreachable Pattern`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/unsafe_call_argument.rst b/tutorials/scripting/gdscript/warnings/unsafe_call_argument.rst new file mode 100644 index 00000000000..279a09497c2 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unsafe_call_argument.rst @@ -0,0 +1,58 @@ +UNSAFE_CALL_ARGUMENT +======================== + +The warning message is: + +.. code-block:: none + + The argument 1 of the function "set_health()" requires the subtype "int" but the supertype "Variant" was provided. + +The default warning level for this warning is **Ignore**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unsafe Call Argument`. + +When this warning occurs +------------------------ + +This warning may appear when passing a value of a certain type to a function that is expecting a subtype instead. + +.. code-block:: + + var original_health = 10 + + func _ready(): + set_health(original_health) # Will give warning UNSAFE_CALL_ARGUMENT. + + func set_health(new_health: int): + current_health = new_health + +Because the type of ``original_health`` was not explicitly stated, it is inferred to be a ``Variant``. The ``Variant`` type is broader than the ``int`` type expected by the ``set_health()`` function's parameter, so it is possible that the variable ``original_health`` could be an incompatible type. + +This warning may also appear when other supertypes are passed in to functions: + +.. code-block:: + + @export var my_node: Node2D + + func _ready(): + make_node_invisible(my_node) # Will give warning UNSAFE_CALL_ARGUMENT. + + func make_node_invisible(node: Sprite2D): + node.visible = false + +Here, the ``make_node_invisible()`` function expects a ``Sprite2D`` to be passed to it, but in ``_ready()`` it is receiving a ``Node2D`` (a supertype/parent type) instead. + +How to fix this warning +----------------------- + +When passing a value to a function, ensure that the data type of the value is the same as or a subtype of the type the function expects. Often, this may involve explicitly stating the type of a variable to ensure it isn't implicitly a ``Variant``. + +.. code-block:: + + var original_health: int = 10 + + func _ready(): + set_health(original_health) + + func set_health(new_health: int): + current_health = new_health + diff --git a/tutorials/scripting/gdscript/warnings/unsafe_cast.rst b/tutorials/scripting/gdscript/warnings/unsafe_cast.rst new file mode 100644 index 00000000000..3e7c2eda241 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unsafe_cast.rst @@ -0,0 +1,25 @@ +UNSAFE_CAST +=============== + +The warning message is: + +.. code-block:: none + + Casting "Variant" to "%s" is unsafe. + +The default warning level for this warning is **Ignore**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unsafe Cast`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/unsafe_method_access.rst b/tutorials/scripting/gdscript/warnings/unsafe_method_access.rst new file mode 100644 index 00000000000..1bb17369c16 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unsafe_method_access.rst @@ -0,0 +1,25 @@ +UNSAFE_METHOD_ACCESS +======================== + +The warning message is: + +.. code-block:: none + + The method "do_thing()" is not present on the inferred type "Node" (but may be present on a subtype). + +The default warning level for this warning is **Ignore**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unsafe Method Access`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/unsafe_property_access.rst b/tutorials/scripting/gdscript/warnings/unsafe_property_access.rst new file mode 100644 index 00000000000..a3168a86513 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unsafe_property_access.rst @@ -0,0 +1,25 @@ +UNSAFE_PROPERTY_ACCESS +========================== + +The warning message is: + +.. code-block:: none + + The property "health" is not present on the inferred type "Node" (but may be present on a subtype). + +The default warning level for this warning is **Ignore**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unsafe Property Access`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/unsafe_void_return.rst b/tutorials/scripting/gdscript/warnings/unsafe_void_return.rst new file mode 100644 index 00000000000..6213684d991 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unsafe_void_return.rst @@ -0,0 +1,25 @@ +UNSAFE_VOID_RETURN +====================== + +The warning message is: + +.. code-block:: none + + The method "do_thing()" returns "void" but it's trying to return a call to "do_other_thing()" that can't be ensured to also be "void". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unsafe Void Return`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/untyped_declaration.rst b/tutorials/scripting/gdscript/warnings/untyped_declaration.rst new file mode 100644 index 00000000000..d085ec3b5ab --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/untyped_declaration.rst @@ -0,0 +1,27 @@ +UNTYPED_DECLARATION +======================= + +The warning message is: + +.. code-block:: none + + Function "_ready()" has no static return type. + + Variable "my_age" has no static type. + +The default warning level for this warning is **Ignore**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Untyped Declaration`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/unused_local_constant.rst b/tutorials/scripting/gdscript/warnings/unused_local_constant.rst new file mode 100644 index 00000000000..2a644b656b8 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unused_local_constant.rst @@ -0,0 +1,25 @@ +UNUSED_LOCAL_CONSTANT +========================= + +The warning message is: + +.. code-block:: none + + The local constant "MAX_VALUE" is declared but never used in the block. If this is intended, prefix it with an underscore: "_MAX_VALUE". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unused Local Constant`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/unused_parameter.rst b/tutorials/scripting/gdscript/warnings/unused_parameter.rst new file mode 100644 index 00000000000..0b9a5745c48 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unused_parameter.rst @@ -0,0 +1,25 @@ +UNUSED_PARAMETER +==================== + +The warning message is: + +.. code-block:: none + + The parameter "delta" is never used in the function "_process()". If this is intended, prefix it with an underscore: "_delta". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unused Parameter`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/unused_private_class_variable.rst b/tutorials/scripting/gdscript/warnings/unused_private_class_variable.rst new file mode 100644 index 00000000000..08c7a4b76cc --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unused_private_class_variable.rst @@ -0,0 +1,25 @@ +UNUSED_PRIVATE_CLASS_VARIABLE +================================= + +The warning message is: + +.. code-block:: none + + The class variable "_health" is declared but never used in the class. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unused Private Class Variable`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/unused_signal.rst b/tutorials/scripting/gdscript/warnings/unused_signal.rst new file mode 100644 index 00000000000..258ec3eb121 --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unused_signal.rst @@ -0,0 +1,25 @@ +UNUSED_SIGNAL +================= + +The warning message is: + +.. code-block:: none + + The signal "defeated" is declared but never explicitly used in the class. + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unused Signal`. + +When this warning occurs +------------------------ + +TODO + + +How to fix this warning +----------------------- + +TODO + + + diff --git a/tutorials/scripting/gdscript/warnings/unused_variable.rst b/tutorials/scripting/gdscript/warnings/unused_variable.rst new file mode 100644 index 00000000000..20d238c77bd --- /dev/null +++ b/tutorials/scripting/gdscript/warnings/unused_variable.rst @@ -0,0 +1,45 @@ +UNUSED_VARIABLE +=================== + +The warning message is: + +.. code-block:: none + + The local variable "counter" is declared but never used in the block. If this is intended, prefix it with an underscore: "_counter". + +The default warning level for this warning is **Warn**. +To modify it, set :ref:`Project Settings > Debug > GDScript > Warnings > Unused Variable`. + +When this warning occurs +------------------------ + +This warning may appear when a variable is declared, but never used before its function or scope ends: + +.. code-block:: + + extends CharacterBody2D + + func _process(delta): + var player_speed = 5.0 # Will give warning UNUSED_VARIABLE. + velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") + move_and_slide() + +In this example, the variable ``player_speed`` is presumably meant to control how fast the player moves. However, ``velocity`` never actually takes into account this speed variable; it only uses the result of ``Input.get_vector()``. It's likely that the programmer meant to include ``player_speed`` here somehow but forgot to write it. + +How to fix this warning +----------------------- + +If a variable is being marked as unused, you probably meant to use it somewhere but forgot to include it in the relevant statement. Following the example above, the likely solution would be to incorporate it into the calculation of ``velocity``: + +.. code-block:: + + velocity = player_speed * Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") + +If you're certain you don't need the variable for anything right now, but might need its value later on, prefix its name with an underscore (``_``) as the warning text suggests. Once you start referencing it elsewhere in the code, you can remove the underscore. + +.. code-block:: + + var _player_speed = 5.0 # Will not give warning UNUSED_VARIABLE. + +If you know you won't ever need the variable, then simply delete it. +