Hello! I’m new to Godot . Like I’ve been using Godot for only 2 days new and I have been stuck in this error that I’ve been trying to fix for like the past 1 and a half hours .
Here is the code :
extends CharacterBody2D
var speed = 250
var ball
var Opponent
func _ready():
ball = get_parent().get_node(“Ball”)
@warning_ignore(“unused_parameter”)
func _physics_process(delta):
move_and_collide(Vector2(0,opponent_direction()*speed))
func opponent_direction():
if abs(ball.position-Opponent.position)<25:
if ball.position > position :
return 1
else : return -1
else:return 0
in the opponent_direction() function at ‘if abs(ball.position-Opponent.position)<25:’ it keep showing this yellow arrow and the code just doesn’t run . If anyone can help me out with this that would be amazing.
Also side note this is my first time using the Godot forms so I may not have properly attached the code or maybe I might not have properly pointed out the problem . So in that case my apologies.
Also it seems that all my if statements have lost their the little spaces that you get by pressing tab(I cant remember what they’re called) .
just know that they are there
Welcome aboard!
The “Nil” part of the error means that whatever variable you are trying to access “position” from isnt set to anything yet.
Does this help?
Also, when you post code, you can format that part lf your post as code and it makes it way more readable. One of the buttons does it, or you can format it by just typing triple quotes (or whatever it is called)
I did create a variable called Opponent
But you didn’t set it to anything, it’s empty, you need to set it just like you do ball
thanks
but dont understand what i can set it to.
i tried giving it vecter2.zero and bunch of other things i thought that could work but it didnt
could you suggest me anything to try out that might work
i did create a variable called opponent
I have no idea
you are the one who wrote the code, so you should know what the Opponent
should be, you need to set it to some node, the Opponent in your code
You creating it isn’t enough, the engine can’t guess what it is supposed to be, you need to set it to what the opponent is, like the node you are using
well basicly i am making this pong game by following this tutorial
and im just making the ball track the ball location and if the balls position goes below the opponent by a certain ammount the opponent will go down
and if the opposite happens the opponent will go up
but for somereason in the 17th line of my code it keeps showing me a yellow arrow and the game just breaks
Opponent
is never assigned to anything, you need to do this yourself, just set it like you do ball

1 Like
ohh thanks
but I cant really figure out what to assign it
could you help me out with that
Please link to the tutorial, there’s no information here on what Opponent
is supposed to be so we can’t help you
its a tutorial from 4 years ago
1 Like
Instead of “the game just breaks” you better post the actual error message. But it will be, most likely, “Invalid get index ‘position’ (on base ‘Nil’)”.
It happens because the variable Opponent is never initialized. You need to add the right way to locate the opponent node, something like Opponent = get_parent().get_node(“Opponent”) (this is just an example, the real code will probably be different).
By the way, instead of @warning_ignore(“unused_parameter”), you can simply add an underscore prefix to the unused parameters: func _physics_process(_delta):
1 Like
Where in the tutorial is Opponent
added, I can’t find that code, it’s just:
func get_opponent_direction():
if abs(ball.position.y - position.y) > 25:
if ball.position.y > position.y: return 1
else: return -1
I don’t know where you’re getting the Opponent
part from?
1 Like
My code was not working so i just tried different things and in that process I just made a variable called opponent
Then I’d suggest following the tutorial code
and see why that doesn’t work, adding new variables like this isn’t usually a solution
1 Like
This is likely the part that’s wrong to begin with, it should be:
if abs(ball.position.y - position.y) > 25:
Here’s the complete (converted) code, this should work:
extends CharacterBody2D
var speed = 250
var ball
func _ready():
ball = get_parent().get_node("Ball")
func _physics_process(_delta):
velocity = Vector2(0,get_opponent_direction()) * speed
move_and_slide()
func get_opponent_direction():
if abs(ball.position.y - position.y) > 25:
if ball.position.y > position.y: return 1
else: return -1
else: return 0
But for a good pong example code I’d suggest looking here, your tutorial is for 3.x and isn’t simple to convert to 4.x
2 Likes
Thanks a lot 
I did follow the code like in the tutorial but it didn’t work for me and I remember doing something similar to this but it didn’t work most likely cause I did it differently
Anyways thanks a lot
1 Like