The problem with your script is in the indentation, which is important in GDScript (like Python.) I will try to explain what I mean:
func Example()
#everything indented to this level is inside the function
if (some_variable > 0):
#everything indented to this level is inside the IF condition
var myNode = get_node("myNode")
some_variable =1 #This line is OUTSIDE of the IF condition, but still INSIDE the function
#If we go back to this indent level, we are outside of that function.
So your code looks something like this:
func _process(delta):
var ball_pos = get_node("ball").get_pos()
var left_rect = Rect2( get_node("left").get_pos() - pad_size*0.5, pad_size )
var right_rect = Rect2( get_node("right").get_pos() - pad_size*0.5, pad_size )
ball_pos += direction * ball_speed * delta
if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)):
direction.y = -direction.y
#NEXT LINE STARTS THE PROBLEM, IT IS STILL INSIDE THE PREVIOUS IF
if ((left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0)):
direction.x = -direction.x
direction.y = randf()*2.0 - 1
direction = direction.normalized()
ball_speed *= 1.1
#NOW IT GETS WORSE, WE ARE NESTED IN ANOTHER IF
if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
ball_pos = screen_size*0.5
ball_speed = INITIAL_BALL_SPEED
direction = Vector2(-1, 0)
So to fix it, you have to decrease your indent after the end of every IF condition. So the above would be corrected to:
func _process(delta):
var ball_pos = get_node("ball").get_pos()
var left_rect = Rect2( get_node("left").get_pos() - pad_size*0.5, pad_size )
var right_rect = Rect2( get_node("right").get_pos() - pad_size*0.5, pad_size )
ball_pos += direction * ball_speed * delta
if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)):
direction.y = -direction.y
#SAME INDENT LEVEL AS PREVIOUS IF
if ((left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0)):
direction.x = -direction.x
direction.y = randf()*2.0 - 1
direction = direction.normalized()
ball_speed *= 1.1
#SAME INDENT LEVEL
if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
ball_pos = screen_size*0.5
ball_speed = INITIAL_BALL_SPEED
direction = Vector2(-1, 0)
In the Godot script editor, the TAB character is displayed as two right carets like this: >>
Count the number of TABs to know what indent level you are at.