Godot's pong tutorial isnt working or im just bad cant tell

GreyAngelKingGreyAngelKing Posts: 1Member

So as far as i can tell ive done everything right. ive hand typed all the code, all my sprite nodes are children of the 2dGame node. all my Greater Thans and Less Thans are pointed the correct way. and still when i boot up the game nothing moves the ball doesn't move the paddles don't move when i press the assigned buttons. and its just genuinely getting frustrating, anyone have an idea of why nothing moves? http://imgur.com/a/8C28M full code lines for anyone wanting to take a look

Answers

  • spesknightspesknight Posts: 4Member

    Did you put the set_process(true) in the _ready() function?

  • spesknightspesknight Posts: 4Member

    Sorry, but I can't see your images.

  • VegaKHVegaKH Posts: 8Member

    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.

Sign In or Register to comment.