In the Pong tutorial i get Invalid operands 'Vector2' and 'int' in operator '

mikethiemmikethiem Posts: 4Member
extends Node2D

var screen_size
var pad_size
var ball_direction = Vector2(-1.0, 0.0)
const INITIAL_BALL_SPEED =80 
const PAD_SPEED = 150 
var ball_speed = INITIAL_BALL_SPEED
func _ready():
    screen_size = get_viewport_rect().size
    pad_size = get_node("left paddle").get_texture().get_size()
    set_process(true)
func _process(delta):
    var ball_pos = get_node("ball").get_pos()
    var left_rect = Rect2(get_node("left paddle").get_pos() - pad_size*0.5, pad_size)
    var right_rect = Rect2(get_node("rightpaddle").get_pos()- pad_size*0.5, pad_size)
    ball_pos += ball_direction *ball_speed*delta

    if (( ball_pos < 0 and ball_direction.y < 0) or (ball_pos.y > screen_size and ball_direction.y > 0)):
        ball_direction.y = -ball_direction.y
    if (( left_rect.has_point(ball_pos) and ball_direction.x < 0 ) or (right_rect.has_point(ball_pos) and ball_direction.x > 0)):
        ball_direction.x = -ball_direction.x
        ball_direction.y = randf()*2.0 -1
        ball_direction = ball_direction.normalized()
        ball_speed *=1.1

    if (ball_pos.x < 0 or ball_pos.x > screen_size.x):
        ball_pos = screen_size*0.5
        ball_speed = INITIAL_BALL_SPEED
        ball_direction = Vector2(-1.0, 0.0)
    get_node('ball').set_pos(ball_pos)

    var left_pos = get_node("left paddle").get_pos()

    if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")):
        left_pos.y += - PAD_SPEED*delta
    if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")):
        left_pos.y += PAD_SPEED*delta
    get_node("left paddle").set_pos(left_pos)

    var right_pos = get_node("rightpaddle").get_pos()

    if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")):
        right_pos.y += -PAD_SPEED * delta
    if (right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")):
        right_pos.y += PAD_SPEED * delta

    get_node("rightpaddle").set_pos(right_pos)

Answers

  • vnenvnen Posts: 31Admin Godot Developer

    Please tell which line has the error.

  • MegalomaniakMegalomaniak Posts: 998Admin

    yeah, getting the full error message would help, as of now I'm just going to take a stab in the dark and say that there's something wrong with ball_direction.x = -ball_direction.x mayhaps. now I'll go crawl back under mah rock for now.

  • Forks4SwordsForks4Swords Posts: 1Member

    I'm having the same issue as well. Whenever I go to test, the window pops up, then goes unresponsive. There are no errors in the error tab, but the debug has a line like so

    it does not specify which line is in error
    This is my first time using Godot and I'm running it on Windows 8.1 64bit

  • krayonkrayon Posts: 1Member

    The issue is with line 16:
    if (( ball_pos < 0 and ball_direction.y < 0) or (ball_pos.y > screen_size and ball_direction.y > 0)):

    The error is because ball_pos is a Vector2D but you are comparing to an int. Additionally, screen_size isn't an int either. Changing the line to this should work:
    if (( ball_pos.y < 0 and ball_direction.y < 0) or (ball_pos.y > screen_size.height and ball_direction.y > 0)):

  • VegaKHVegaKH Posts: 8Member

    @Forks4Swords said:
    I'm having the same issue as well. Whenever I go to test, the window pops up, then goes unresponsive. There are no errors in the error tab, but the debug has a line like so !
    it does not specify which line is in error
    This is my first time using Godot and I'm running it on Windows 8.1 64bit

    I know this answer is late, but in case anyone else runs into similar issues:

    First, I should mention that it DOES tell you which line caused the error. It is in the "Stack Frames" box at the bottom where it says:
    0- res://game.gd:57

    In this case, this is telling you that the error is in that file on line 57. And the error is the same one that Krayon gives the fix for above, you forgot to reference the y component of the position. So you need to change line 57:

    if (right_pos > 0 and Input.is_action_pressed("right_move_up")): #INCORRECT!

    to this:

    if(right_pos.y > 0 and Input.is_action_pressed("right_move_up")): #CORRECT!

Sign In or Register to comment.