All Questions



If statement direct to variable types

Is there a place in the Godot docs that show how variable types can be used in "if", "if not", "if is"? I am kinda lost on when to use these and what they will return.

  • Nathan Lovato replied

    To check for a value's type, you use the is keyword:

    if node is KinematicBody2D:

    To negate that, you need to put the not before the expression.

    if not node is KinematicBody2D:
  • a
    astory replied

    0, false, an empty string(""), null, and an empty array([ ]) all  return false, everything else will return true.
    if target:
     is checking against null
    if target_list:
     is checking against an empty list

    not just always flips your true to a false so say if you want to only do some thing if the array is empty, instead of having to type

    if target_list:
        # run code

    you can type

    if not target_list:
        # run code