Jump to content

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


MSpace-Dev

Recommended Posts

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?

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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