0 votes

I saw this here.

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

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.

in Engine by (12 points)

1 Answer

0 votes

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.

by (28,876 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected]tengine.org with your username.