Error Getting Width of Randomly Spawning Obstacles

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ERJstrife

I keep getting the error message, ‘Can’t get index “width” on base “Vector2”’ from the following code:

func go_init_position():
var init_position = Vector2()
init_position.x = get_viewport_rect().size.width + PIPE_WIDTH/2
init_position.y = rand_range(0+OFFSET_Y, get_viewport_rect().size.height-GROUND_HEIGHT-OFFSET_Y)

Is there something I am missing or another way to code this?

additional information:

I’ve been following a four year old Flappy Bird tutorial and so far there have been issues changing some code to function in 3.2, so this might be a similar case. The link to the video is here Flappy Bird Godot Engine Tutorial and the code is implemented at the time 35:39

:bust_in_silhouette: Reply From: exuin

The problem is here: get_viewport_rect().size.width and get_viewport_rect().size.height. get_viewport_rect().size has the properties x and y now, not width and height. So you should have this:

init_position.x = get_viewport_rect().size.x + PIPE_WIDTH/2
init_position.y = rand_range(0+OFFSET_Y, get_viewport_rect().size.y-GROUND+HEIGHT-OFFSET_Y)

Thank you so much for the response and clear explanation, this has helped me immensely; cheers!

ERJstrife | 2020-09-20 22:25