Get game screen size before object placement

Hello, I am having trouble with my objects that I use events to place (i.e. Change the X position of [object]: set to ScreenWidth()-20). It works perfectly when I preview the game, but as soon as I put it on a phone or tablet it changes the location because it has resized the screen.

My question is, is there a way to check the size of the screen the game has scaled to before it runs these events? Or am I going about this the wrong way?

As always, any advice would be greatly appreciated. These are my game property settings for scaling.

You really shouldn’t use the “Update resolution” checkbox unless you’re planning on adjusting your game’s visible area for infinite resolutions.

By using that checkbox, a phone with a 1080x1920 display will have your game assets be much smaller than a phone with a 720x1280 display.

That said, if you’re expecting that, then yes your assets will move around with that as well. You should avoid ScreenWidth()-20 as “-20” will be a different amount depending on the game resolution. I’d recommend using “CameraWidth(“YourGUILayerNameHere”,0)” instead of screenwidth, and then maybe do -(YourGUIObjectNameHere.Width()+ (CameraWidth(“YourGUILayerNameHere”,0)/20)) as the other part. This will mean you’ll start the GUI object past the screen (the entire width of the camera), then move it back the entire width of the GUI object, plus 1/20th of the screen size.

Let’s say my GUI Layer is named “GUI”, my Object is named “Score”.
This means it’d look like CameraWidth(“GUI”,0)-(Score.Width()+(CameraWidth(“GUI”,0)/20)).
If Score is 50 pixels wide, on a 720p screen (horizontal, so 1280x720), this would equate to: 1280-(50+(1280/20)), or 1280-(114), or 1166.

On a 1080p screen, it’d equate to 1920-(50+(1920/20)), or 1920-(146), or 1774.

If you DON’T want to adjust the resolution for each screen size, and instead want to just scale the assets up/down, uncheck that box. Any resolution that isn’t an integer scaling (1x, 2x, 3x, etc) of your default resolution will have black bars on the top/bottom, but otherwise everything will stay placed where you want them to.


Thank you for your explanation, that was very helpful. I am having problems with the last bit (CameraWidth(“YourGUILayerNameHere”,0)/20). It puts it close to where I want it, but most of it is still off screen. I’ve tried adjusting /20 to multiple other options (/40, /80, /120) but it has not worked for me. If I use CameraWidth(“UI”,0)-Pause.Width()+CameraWidth(“UI”,0)-20 then it puts it exactly where I would want it, except it’s on my parallax background.

In the following screenshot you can see what I mean, I’ve expanded the window.

I’ve figured it out!


Thanks again for your help Silver-Streak. You rock.

Glad to help.

I’d strongly recommend you wrap your equations. Right now your order of operations may be messed up (and you’re multiplying by 0, so you’re just saying “Pause.Width-20”).

So it’d be something like this CameraWidth(“UI”,0)-(Pause.Width()+(CameraWidth(“UI”,0)/20))

Never copy/paste from forum posts. Your " are formatted incorrectly. :smiley:

1 Like

I see what you mean! It worked just fine when I replaced ", but I did have to change /20 to /60. So, I achieved the same result doing it this way (CameraWidth(“UI”,0)-Pause.Width()+CameraWidth(“UI”,0)*0-20). What makes your way better? Will it stay where I want it on different screen sizes?

Yes. because you’re doing CameraWidth/60, you’re not using a “fixed” location. So if the resolution is 720p(1280 wide), it’d be 21.33~, if the resolution is 1080p (1920wide), it’d be 32, which is roughly the same location.

1 Like