# Render.get\_window\_width() vs. GUI width

**URL:** <https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229>\
**Category:** Questions\
**Created:** [March 23, 2019, 11:15pm UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229 "2019-03-23T23:15:44Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 23, 2019, 11:15pm UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/1 "2019-03-23T23:15:45Z")

</div>

I want to set a GUI node to the real screen width. `render.get_window_width()` returns 1080 on my Android test device, which is much wider that the GUI.

When testing locally by changing the window size it works fine.

How can I get a window size that I can use in a GUI on device?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 24, 2019, 5:58am UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/2 "2019-03-24T05:58:28Z")

</div>

Are you saying that render.get\_window\_width() returns the wrong value? What is the value you have in game.project, what value does the function return, and what is the actual width?

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 24, 2019, 9:24am UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/3 "2019-03-24T09:24:09Z")

</div>

> [@britzl](#):
>
> Are you saying that render.get\_window\_width() returns the wrong value? What is the value you have in game.project, what value does the function return, and what is the actual width?

Well, render.get\_window\_width() returns a value that doesn’t seem to match the GUI.

I have set the game project width to 414. The function returns width 1080 on device. Not sure about the actual width, is there a way to get the actual width of a GUI?

I thought this might be relevant, but can’t get it to work:

> [@Get real size of GUI node](https://forum.defold.com/t/get-real-size-of-gui-node/167/5):
>
> Please forgive me if this has been answered somewhere else, but this is something I’m also having trouble with. When in stretch mode the width and height values are correct, but what about zoom and fit?

ps. I am actually using Orthographic camera.get\_window\_size(), which in turn uses render.get\_window\_width(). That shouldn’t matter, right?

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 24, 2019, 9:34am UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/4 "2019-03-24T09:34:39Z")

</div>

The thing that confuses me about this is that it works exactly as it should locally, but it doesn’t work on device.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 24, 2019, 6:34pm UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/5 "2019-03-24T18:34:16Z")

</div>

> [@totebo](#):
>
> is there a way to get the actual width of a GUI?

No, not really. You can get the size of individual nodes, but that can change based on adjust mode.

> [@totebo](#):
>
> I am actually using Orthographic camera.get\_window\_size(), which in turn uses render.get\_window\_width(). That shouldn’t matter, right?

No, that shouldn’t matter.

> [@totebo](#):
>
> The thing that confuses me about this is that it works exactly as it should locally, but it doesn’t work on device.

The html template will resize the canvas so that it fills out the entire screen width while maintaining aspect ratio. 1080 sounds like a screen width that you could encounter on a device. Which device are you testing on?

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 24, 2019, 6:36pm UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/6 "2019-03-24T18:36:22Z")

</div>

> [@britzl](#):
>
> 1080 sounds like a screen width that you could encounter on a device. Which device are you testing on?

It’s a OnePlus 6T and a native Android app. I know I’ve been hanging on about the Facebook Instant HTML5 game, but this is a separate project. 🙂

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 24, 2019, 7:32pm UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/7 "2019-03-24T19:32:07Z")

</div>

I created a minimal version of the project, and I’m getting nowhere. I’ll post it here as a sign of defeat, for now: [StretchNodeToScreenWidth.zip](https://defold-forum-bucket.nbg1.your-objectstorage.com/original/2X/8/83cfa37ca23ca80a080d2204ea479f3f067125e4.zip) (227.8 KB)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 24, 2019, 8:49pm UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/8 "2019-03-24T20:49:35Z")

</div>

> [@totebo](#):
>
> It’s a OnePlus 6T and a native Android app

The 6T has a resolution of 1080 x 2280. So, render.get\_window\_width() is correct. And window.set\_listener() should fire when orientation changes, but you will not be able to use it on app start to detect screen size. render.get\_window\_width() is the way to get the size on app start.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 24, 2019, 9:15pm UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/9 "2019-03-24T21:15:19Z")

</div>

> [@britzl](#):
>
> The 6T has a resolution of 1080 x 2280. So, render.get\_window\_width() is correct.

Yes, I figured that was the case. I’ve yet to figure out how to translate this figure to be usable with the GUI, though.

Actually, that’s not entirely true - if I set the GUI’s “Adjust Reference” to “Disabled” the size correlates to the GUI. With the huge drawback of having to code all the alignment manually. It may be that I can redesign the game input mechanics themselves, to avoid this problem!

> [@britzl](#):
>
> render.get\_window\_width() is the way to get the size on app start.

I guess I’d have to modify the render script for this? I was hoping to use Orthographic, a tool I adore. 🙂

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 25, 2019, 5:10am UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/10 "2019-03-25T05:10:33Z")

</div>

> [@totebo](#):
>
> I’ll post it here as a sign of defeat, for now: [StretchNodeToScreenWidth.zip](https://forum-defold-uploads.b-cdn.net/original/2X/8/83cfa37ca23ca80a080d2204ea479f3f067125e4.zip) (227.8 KB)

If you set all three nodes to stretch you’ll at least get something that stretches as you’d expect:

 ![56](https://forum-defold-uploads.b-cdn.net/original/2X/8/880512d1aa5bd056097bca341d0e5a39da09ece2.png)  
 ![00](https://forum-defold-uploads.b-cdn.net/original/2X/0/06f5e2b6d870337785e74dbc8458a799404bcf8a.png)  
 ![05](https://forum-defold-uploads.b-cdn.net/original/2X/9/966f10a81f1b559e042f2185c94ccf427b2e6f47.png)

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 25, 2019, 8:13am UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/11 "2019-03-25T08:13:51Z")

</div>

That’s clever! Is there any way to use the scale9, so the visual buttons are not deformed? That would solve it.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 25, 2019, 8:55am UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/13 "2019-03-25T08:55:57Z")

</div>

Sure, 9 slice is supported.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 25, 2019, 9:57am UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/14 "2019-03-25T09:57:19Z")

</div>

When I test, slice 9 objects are deformed, rather than retaining the corners nicely. How would I get them to stretch nicely when they’re stretched by Defold (rather than me setting the size manually)?

 ![01](https://forum-defold-uploads.b-cdn.net/original/2X/6/6c9841013ee4a18bc83ac4b38354b262745ff3e1.png)  
 ![09](https://forum-defold-uploads.b-cdn.net/original/2X/7/7d34badc520d86e31d54e1e937282fa3bc8cb2fb.png)  
 ![32](https://forum-defold-uploads.b-cdn.net/original/2X/b/b8b6ecec93edf87da4a68fddf0de4e0612d03556.png)

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 27, 2019, 9:37am UTC](https://forum.defold.com/t/render-get-window-width-vs-gui-width/47229/15 "2019-03-27T09:37:17Z")

</div>

I’ve added a feature request for this chestnut: [Use Slice 9 when GUI node's Adjust Mode is set to Stretch](https://forum.defold.com/t/use-scale-9-when-gui-nodes-adjust-mode-is-set-to-stretch/47536)
