Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.11.2] Extending GuiScreen returns 0 for this.width and this.height

Featured Replies

Posted

So, I am trying to get the width and height of my current screen size, by extending GuiScreen. Though, for some reason, it's returning 0 for this.width and this.height. Yet when I made an Openable GUI extend GuiScreen, it returns the correct values. I need the screen width and height to put my custom health bar in the bottom right of the screen. I've tried to call initGui() and setWorldAndResolution() to see if they'd get the width and height, but with no avail.

 

public class GuiOverlay extends GuiScreen {
    private final ResourceLocation bar = new ResourceLocation(Reference.MODID, "textures/blocks/hp_bar.png");

    private final int bar_width = 102, bar_height = 8, hp_height = 6;
    private int hp_width = 100;

    @SubscribeEvent
    public void renderOverlay(RenderGameOverlayEvent event){
        Minecraft mc = Minecraft.getMinecraft();
        mc.renderEngine.bindTexture(bar);
        
	Utils.getLogger().info("Width: " + this.width); // returns 0
        Utils.getLogger().info("Height: " + this.height); // returns 0

        drawTexturedModalRect(width - bar_width, height - bar_height, 0, 0, bar_width, bar_height); // Off the screen due to width/height = 0
        drawTexturedModalRect(width - bar_width, height - bar_height, 1, bar_height + 1, hp_width, hp_height); // Off the screen due to width/height = 0

        mc.renderEngine.bindTexture(ICONS);
    }
}

Any ideas?

If you follow the call hierarchy for the width and height fields you'll see that the main way they are set is with the Minecraft#updateDisplay() method which calls the Minecraft#checkWindowResize() method which calls the Minecraft#resize() method which calls the GuiScreen#onGuiResize() method which calls the GuiScreen#setWorldAndResolution().

 

I think your GUI should get the benefit of that automatically if you register it and open it properly. But if you wanted to force it then the setWorldAndResolution() method should do it assuming you pass the right size (scaled display size) to it.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.