How do I make a variable and use it in an "if" statement?

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

An extremely basic question I know. And one that should be incredibly easy to find the answer to. Yet here I am after a couple hours of looking it up from every conceivable angle. Let me run you through my problem.

Im trying to make a simple double jump on a simple 2D platformer. Just to get a feel for godot and GDScript. So Ive got a variable named “dbj” for double jump. When my player object is touching the ground, dbj is to be set to 1. When Im not touching the ground, if the jump button is pressed and dbj still equals 1, Im allowed to jump again. And then dbj = 0 until I touch the ground again. Simple.

When I put that into code, as soon as I typed

if dbj = 1

I got the error “unexpected assign”

So I began to wonder if I had established my variable correctly. And honestly I still dont know.

So in short, how do I make a variable and use it in an “if” statement.

:bust_in_silhouette: Reply From: Xrayez

You missed another = sign in conditional (I forget to type it out as well sometimes):

if dbj == 1:
    # do stuff

Single = is used to assign variables, and == is used to compare for equality.

You can use != operator for checking whether two variables are not equal as well.

Thanks for the answer but Ive run into another problem. I put the extra = in there but now when I try to run it, it says "Script inherits from native type ‘Reference’, so it cant be used in object of type: Staticbody2D.

This is the code snippet.

if is_on_floor():
	dbj = 1
	if Input.is_action_just_pressed("ui_up"):
		move.y = -JUMP
elif (dbj == 1):
	if Input.is_action_just_pressed("ui_up"):
			move.y = -JUMP
			dbj = 0

EDIT: Nevermind, I’m dumb. Accidentally put a script in one of the walls. Didnt register that the error said STATIC body instead of kinematic body until I wrote it out. Problem solved.

Xy | 2018-09-14 14:22

Do you have this line at the top of your script?

extends Reference

If not, the script will inherit Reference by default.

Your node seems to be of type StaticBody2D. But according to your code, you actually need to change your character type to KinematicBody2D, so you need to put this line at the top of the script instead and change the node type:

extends KinematicBody2D

Xrayez | 2018-09-14 14:31