Regarding the script in the tutorial project : Pong

redbunnyredbunny Posts: 14Member

Hi. Sorry to keep bothering you guys. I am new to programming. I am learning the Simple 2D game project called Pong in the tutorial. From what I understand, the _process() function is some kind of loop that run for every second(please correct me if I am wrong. I thought I read about this in the previous chapters but everything is very difficult to understand for me and I cannot remember clearly). If that is the case, then the script in the sample project of Pong below is very confusing, and very inefficient in my understanding:

func _process(delta):
# Get ball position and pad rectangles
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)

Is it correct to say that the coder declares ball_pos,left_rect,and right_rect with the var in front of them for every second the loop is run? If that is the case, does it mean the code has to allocate new var every second and then discard them at the end of the _process()? Should not the three of them be declared outside of this function? I find this very wrong practice(pardon my bad english). Am I missing something here? Thanks.


Tags :

Best Answer

  • redbunnyredbunny Posts: 14
    edited October 2017 Accepted Answer

    I moved all three of the ball_pos, left_rect,and right_rect var declarations outside of _process() and into the top of the script, I assume there is the global space. And the project ran fine. Guess I will stick to declaring all var in the global space.

Answers

  • redbunnyredbunny Posts: 14Member
    edited October 2017 Accepted Answer

    I moved all three of the ball_pos, left_rect,and right_rect var declarations outside of _process() and into the top of the script, I assume there is the global space. And the project ran fine. Guess I will stick to declaring all var in the global space.

Sign In or Register to comment.