Detect screen dimensions in [C#]

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

I saw this here.
https://forum.godotengine.org/234/how-to-get-the-screen-dimensions-in-gdscript?show=31224

But it seems to not be relevant to my case.
I tried

var _size = GetViewPort().Size;

and

var _size = OS.WindowSize;

both give the wrong answer.

In my particular use case I want to spawn a pellet of food at a random location on the screen. When I run the above code in a windows end point I get the desired behavior but when I run the above code in a browser endpoint it will output larger numbers than expected and spawn the pellets outside of the screen.

:bust_in_silhouette: Reply From: Zylann

GetViewport().Size should be the right one if you want the size of the client rect of the game window in pixels. If it’s still not the case in HTML export, that might be a bug.

Otherwise if you want this in terms of game world coordinates (which might not map exactly pixels on the monitor, if you used a stretch mode), you could place two nodes as rectangle anchors in your scene and determine the random position between them. This way it should guarantee a correct position because it’s inside the same world you spawn nodes, and won’t be dependent on screen, pixels, stretch, zoom etc. You can also use a ReferenceRect node to do this.