Set the region_rect of a Sprite via code

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

I want that the x value of a region rect of a sprite is set to the x position of a KinematicBody2d divided by 10 plus 52 and the same with the y value of the region rect.

I tried this:

get_node("Sprite").region_rect = 
Rect2(get_node("KinematicBody2d").position.x / 10 + 52, 
      get_node("KinematicBody2d").position.y / 10 + 52)
#In the real code this is all in one row

But I got this error:

Invalid type in ‘Rect2’ constructor. Cannot convert argument 1 from Nil to Vector2.

How can I fix that?
Thanks for answers.

:bust_in_silhouette: Reply From: arthurZ

I think the syntax is this:

Rect2 ( float x, float y, float width, float height )


Rect2 ( Vector2 position, Vector2 size )

:bust_in_silhouette: Reply From: deaton64

Hi, a Rect2 has two Vector2s, you’ve only got one.
So you either need to specify another Vector2 for the size, or just set the sprites region_rect position