Help with 2d game

4.3

I was trying the 2d game but I have ALOT of errors I will show you my code and if you can give me your code that would be amazing!

type or paste code here
extends Area2D

@export var speed = 400 # How fast the player will move (pixels/sec).
var screen_size # Size of the game window.
func _ready():
	screen_size = get_viewport_rect().size
	
	func _process(delta):
	var velocity = Vector2.ZERO # The player's movement vector.
	if Input.is_action_pressed("move_right"):
		velocity.x += 1
	if Input.is_action_pressed("move_left"):
		velocity.x -= 1
	if Input.is_action_pressed("move_down"):
		velocity.y += 1
	if Input.is_action_pressed("move_up"):
		velocity.y -= 1

	if velocity.length() > 0:
		velocity = velocity.normalized() * speed
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()
		
		position += velocity * delta
position = position.clamp(Vector2.ZERO, screen_size)

if velocity.x != 0:
	$AnimatedSprite2D.animation = "walk"
	$AnimatedSprite2D.flip_v = false
	# See the note below about the following boolean assignment.
	$AnimatedSprite2D.flip_h = velocity.x < 0
elif velocity.y != 0:
	$AnimatedSprite2D.animation = "up"
	$AnimatedSprite2D.flip_v = velocity.y > 0
	
	if velocity.x < 0:
	$AnimatedSprite2D.flip_h = true
else:
	$AnimatedSprite2D.flip_h = false
	hide()
	
  signal hit

func _on_body_entered(body: Node2D) -> void:
	 func _on_body_entered(body):
	hide() # Player disappears after being hit.
	hit.emit()
	# Must be deferred as we can't change physics properties on a physics callback.
	$CollisionShape2D.set_deferred("disabled", true)
	
   func start(pos):
	position = pos
	show()
	$CollisionShape2D.disabled = false

What are the errors?

Make sure to paste code with proper formatting

my errors are:

Line 8:Expected indented block after lambda declaration.
Line 8:Expected end of statement after expression, found “var” instead.
Line 8:Standalone lambdas cannot be accessed. Consider assigning it to a variable.
Line 26:Unexpected “Identifier” in class body.
Line 26:Unexpected “if” in class body.
Line 28:Unexpected “Indent” in class body.
Line 29:Unexpected “$” in class body.
Line 30:Unexpected “$” in class body.
Line 32:Expected end of file.

So GDScript is a whitespace sensative programming language. This means how you indent (tab) your code affects how it runs, consequently if you indent in a way that the computer cannot make sense of, you will get errors on otherwise good code.

For example in your paste

	# indented once, part of the _ready() function
	# therefor the `if` only runs on _ready()
	if velocity.length() > 0:
		# indented twice, part of the `if`
		# only runs when the condition `velocity.length() > 0` is met
		velocity = velocity.normalized() * speed
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()
		
		# Here's a logical error, indented twice
		# therefor only adding velocity if velocity is zero
		# since it's indented to be part of the `else:`
		position += velocity * delta

# Here's a syntax error, completely unindented, so it's outside
# of the function `_ready()`, but code like this can't run for
# no reason (inside the class body) only new variable definitions. 
position = position.clamp(Vector2.ZERO, screen_size)

[/quote]

Check out this GDScript interactive tutorial

2 Likes

Hey I tried removing the spaces but it still shows me errors

It’s not about removing the spaces, it’s about having the correct amount. You need to both add and remove tabs in certain places.

Since you have mixed spaces and tabs I have to ask if you copied this code from somewhere else without understanding it?

Here’s what I think you want, notice the functions are all defined without indentation, in the “class body”, same with signal and some variables; while other statements are at least indented once.

extends Area2D

@export var speed = 400 # How fast the player will move (pixels/sec).
var screen_size # Size of the game window.

func _ready():
	screen_size = get_viewport_rect().size

func _process(delta):
	var velocity = Vector2.ZERO # The player's movement vector.
	if Input.is_action_pressed("move_right"):
		velocity.x += 1
	if Input.is_action_pressed("move_left"):
		velocity.x -= 1
	if Input.is_action_pressed("move_down"):
		velocity.y += 1
	if Input.is_action_pressed("move_up"):
		velocity.y -= 1

	if velocity.length() > 0:
		velocity = velocity.normalized() * speed
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()

	position += velocity * delta
	position = position.clamp(Vector2.ZERO, screen_size)

	if velocity.x != 0:
		$AnimatedSprite2D.animation = "walk"
		$AnimatedSprite2D.flip_v = false
		# See the note below about the following boolean assignment.
		$AnimatedSprite2D.flip_h = velocity.x < 0
	elif velocity.y != 0:
		$AnimatedSprite2D.animation = "up"
		$AnimatedSprite2D.flip_v = velocity.y > 0

		if velocity.x < 0:
		$AnimatedSprite2D.flip_h = true
	else:
		$AnimatedSprite2D.flip_h = false
		hide()

signal hit

func _on_body_entered(body: Node2D) -> void:
	hide() # Player disappears after being hit.
	hit.emit()
	# Must be deferred as we can't change physics properties on a physics callback.
	$CollisionShape2D.set_deferred("disabled", true)

func start(pos):
	position = pos
	show()
	$CollisionShape2D.disabled = false

I copied it from the 2d game tutorial I did not copy it anywhere else

in that case it would be best if you find some time and invest it in the GDScript tutorial for beginners. there you will understand the fundamental things you should know if you want to work with GDScript and not just copy-paste it :+1:t2:

:godot:

2 Likes

ok do you suggest any tutorial?

1 Like

thank you

3 Likes