Project issues while making pong

Godot Version

4.2.2

Question

having problems with my script and game. I’m trying to make pong.
my issues are the ball knocking the paddle back when the ball collides with it (im using character body 2d for the paddles) and whenever the ball make contact with the either of the collision shapes i set up on the right and left i receive an error.

whats supposed to happen is the ball should reset but i get a "Invaild set Index ‘global_postion’ " error instead. bellow i linked my script nodes and video that i am following along with. any help is appreciated

video:

here are my nodes
image

here is my game, player and ball script in that order

game : extends Node2D

func _on_top_body_entered(body):
print(“out”)
body.direction.y *= -1

func _on_bottom_body_entered(body):
print(“out”)
body.direction.y *= -1

func _on_left_body_entered(body):
body.queue_free()
var e = preload(“res://ball.tscn”).instantiate()
add_child(e)
e.global_postion = Vector2(576,320)

func _on_right_body_entered(body):
body.queue_free()
var e = preload(“res://ball.tscn”).instantiate()
add_child(e)
e.global_postion = Vector2(576,320)

player :
extends CharacterBody2D

const SPEED = 300.0
@export var side = ‘p1’

func _physics_process(delta):
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction

if side == 'p1':
	direction = Input.get_axis("up", "down")
else:
	direction = get_axis("Lup", "Ldown")
if direction:
	velocity.y = direction * SPEED
else:
	velocity.y = move_toward(velocity.y, 0, SPEED)


move_and_slide()

func get_axis (up, down):
if Input.is_key_pressed(KEY_I): return -1
elif Input.is_key_pressed(KEY_K): return 1

balll:
extends CharacterBody2D

const SPEED = 300.0
var direction = Vector2.ZERO

func _ready():
direction.y = [1-1].pick_random()
direction.x = [1,-1].pick_random()

func _physics_process(delta):

if direction:
	velocity = direction * SPEED
else:
	velocity = velocity.move_toward(Vector2.ZERO, SPEED)

move_and_slide()

global_position, you are missing an i

2 Likes

thanks. do you know whats causing the other issues ?

The ball script should proabably use move_and_collide right? I see no reason for it to push the paddles but I also see no reason for it to bounce off the paddle

1 Like

i wasnt aware i can try it after i fix the index issue i cant load my game until its fixed

Once i took the var direction out of the _physical_process(delta):
the error became a invalid index for ‘X’ any ideas on what to do ?

Make sure to format your code, it’s hard go through it, if you can paste the relevant line with the error that would help too

I believe the invalid .x is from direction? but I don’t see anything wrong with it in your original post, maybe an update to the code is in order?

1 Like

i thought the same thing but its okay i made a post on reddit hopefully ill get an answer and ill post an update soon.

also thanks for posting about the formatting, ill be sure to do it in the future