ScreenSize always returns 0

Godot Version

4.3

Question

I am just getting started with Godot and have downloaded the Godot Engine - .NET package. I am working through the “Your first 2D game” tutorial using C# and am running into an issue updating the position of the Player.

In short, there is code like so:

Position = new Vector2(
   x: Mathf.Clamp(Position.X, 0, ScreenSize.X),
   y: Mathf.Clamp(Position.Y, 0, ScreenSize.Y)
);

But it locks the player in the upper-left corner. If I use GD.Print statements I can see that both ScreenSize.X and ScreenSize.Y always return 0. If I comment out the above then I am able to move the player around the screen, but (obviously) it can meander off-screen since there is no longer any clamping.

Did I not setup the project correctly? I believe I followed the instructions precisely, setting the Project Settings to a Viewport width and height of 480 and 720, respectively, and the stretch mode to canvas_items and keep.

Is there some other setting needed to get it to report non-zero values when using C#? Or some additional library I need to install or something?

Thanks

I think you are looking for;

GetViewportRect().Size

1 Like

This is it exactly, thank you!

1 Like