Posted November 21, 20177 yr 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?
November 22, 20177 yr 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.