|Topic was automatically imported from the old Question2Answer platform.
I saw this here.
But it seems to not be relevant to my case.
var _size = GetViewPort().Size;
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.
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.