+1 vote

So I just started learning how to code and decided to use a tutorial to help me get started, but even though I copied the code almost 1-1 with few exceptions that would only be cosmetic changes not actual changes to the code, I'm getting an error when trying to use "if"

this is my code

extends KinematicBody2D

var curHp: int = 40
var maxHp: int = 60
var moveSpeed : int = 250
var damage : int = 10

var cash : int = 30

var curLevel : int = 0
var curXP : int = 0
var xpToNextLevel : int = 50
var xpToLevelIncreaseRate : float = 1.2

var interactDist : int = 70

var vel : Vector2 = Vector2()
var facingDir : Vector2 = Vector2 ()

onready var rayCast = get_node("RayCast2D")

func physicsprocess (delta):

vel = Vector2()

# inputs

if Input. isactionpressed("moveup"):
vel.y-=1
facingDir = Vector2(0, -1)
if Input. is
actionpressed("movedown"):
vel.y-=1
facingDir = Vector2(0, 1)
if Input. isactionpressed("moveleft"):
vel.x-=1
facingDir = Vector2(-1, 0)
if Input. is
actionpressed("moveright"):
vel.y+=1
facingDir = Vector2(1, 0)

vel = vel.normalized()

move the player

moveandslide(vel * moveSpeed)

Godot version 3.3
in Engine by (18 points)

1 Answer

0 votes

The first problem is that your code is impossible to read because when you pasted it in here, it lost all formatting. There is a "Code Sample" button when you're posting - use it to preserve your code's formatting.

At a guess, I'd say your error is because the if is not indented correctly, so it's not part of the _physics_process() function like it should be. vel = Vector2() is indented correctly, which is why it doesn't trigger the error.

by (20,677 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.