Variables becoming "Nil" for no reason

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rarefact

I have been working on a code to reflect a kinematic body, but for some reason my direction variable is showing up to as “Nil” and is starting an error. Here is my code:

extends KinematicBody2D
Var speed
Var a
Var b
Var x
Var c
Var collision
Var _direction
Var velocity
Var d
Func _ready():
    Pass
Func _process(delta):
    Var a = Vector2(2, 5)
    Var b = Vector2()
    Var x = 3
    Var y = 1
    Var speed = 1000
    Var d = 0
    If d == 0:
          Var _direction = Vector2(2, 5).rotated(-180)
           Var velocity = _direction * speed
    Move_and_collide(velocity * delta)

    b.x = x
    b.y = y
    Var c = a + b
    Var collision = Move_and_collide(velocity * delta)
    If collision:
         Var reflect = collision.remainder.bounce(collision.normal)
         d = d + 1
        Velocity = velocity.bounce(collision.normal)
         move_and_collide(velocity * delta)

I am very inexperienced in this feild, so please let me know of an other problems I have as well. Thanks in advance

In future, please format your code so that it is readable. I attempted to add formatting, but you also have somehow introduced a bunch of capitalization all over the place. I recommend editing your post and re-pasting your code directly from Godot. Use the “Code Sample” button (it looks like {} ) at the top of the editing window.

kidscancode | 2019-02-24 03:15

:bust_in_silhouette: Reply From: kidscancode

At the top of your code, you are declaring your variables as class variables, meaning they are accessible in all functions. You don’t need to use var again when assigning values to them in process(). In other words, you only need var the first time you use a variable, not every time you use it.

You are also doing many unnecessary things. Example:

var d = 0
if d == 0:

Why are you testing if d is 0 when you just set it to 0? Since you’re resetting it to 0 every frame, it’s not going to have any other value.

Thank you! That d = 0 code was just so I could test if the system could count variables in conditional statements

Rarefact | 2019-02-26 13:14