I found a solution for "Invalid type in function 'clamp' in base 'Vector2'."

Hi there!

I’m using Godot Version 4.1.2 stable,
and the line
global_position = global_position.clamp(Vector2(0,0),screen_size)
gave me the error
Invalid type in function 'clamp' in base 'Vector2'. Cannot convert argument 2 from Rect2 to Vector2.

I then looked up Rect2 (https://docs.godotengine.org/en/stable/classes/class_rect2.html) and found the fitting property: end .
So I added .end behind screen_size and then it worked flawlessly :slight_smile: .

That last line looks now like this:
global_position = global_position.clamp(Vector2(0,0),screen_size.end)

Best regards, and i like this course :slight_smile:

I am also using Godot v4.1.2 stable and with this code get no error:

var screen_size = get_viewport_rect().size
global_position = global_position.clamp(Vector2(0,0), screen_size)

nice bit of digging there, yep when we declared the
var screen_size = get_viewport_rect()

we add the .size to the end of it to get the Vector2 size of the screen.
var screen_size = get_viewport_rect().size

since the Rect2 type at its base contains two Vector2 values, one for position and one for size which it can also interpolate the .end value from the two.

would you have missed the .size when declaring the screen_size variable by accident?

var screen_size = get_viewport_rect().size
global_position = global_position.clamp(Vector2(0,0), screen_size)

glad you found a workaround, awesome stuff :slight_smile:

… I just checked and found out: OboShape you are right…

would you have missed the .size when declaring the screen_size variable by accident?

I had indeed missed that .size at the end of the screen_size variable declaration…

and yes, I’m glad too I could find that workaround - kudos to the excellent Godot documentation, and the fact you can ctrl-click on a word in script editor to open the matching documentation page for it :slight_smile:

2 Likes

Privacy & Terms