Topic category: User side tutorials
I have seen a lot of people asking for this and I recently figured it out myself. So making a bar in an overlay is actually relatively easy with custom code. By following this tutorial, you can make a bar based on health. You can change the health values to whatever values you see fit.
Step 1: Make your bar.png and your barframe.png
- My Bar
- My Bar Frame
Step 2:Make your overlay and place your bar/frame where you would like it to go, as well as adding your conditional statement for the bar to appear. (You can always edit the position later in the code need be, which I didn't do for the tutorial but I highly recommend doing it to get the positioning right for all scales until mcreator fixes the way it handles scaling in screens.)
Step 3: Open your OverlayNameOverlay.java and declare 2 doubles and an integer under the eventHandler class declaration. Four this tutorial the 2 doubles will be your health, and max health. Your integer will always be the bar length and it will be equal to one double divided by the other to get the ratio for the bar.
Under This:
if (event.getType() == RenderGameOverlayEvent.ElementType.ALL) {
Declare your variables.
Variable Declarations (My Example):
double health = (entity.getHealth());
double maxHealth = (entity.getMaxHealth());
int barlength=(int)Math.round((86)*(health/maxHealth));
Replace the "86" with the initial length of your bar.png.
Step 4: Find the lines where your pictures are set them to be the lines below, replacing values where you need to for your positions, and pictures
Render Bar Code:
if (true) { Minecraft.getInstance().font.draw(event.getMatrixStack(), "" + (int)health, posX - 12, posY + 77, -12829636); //Gets Health as Integer next to the bar RenderSystem.setShaderTexture(0, new ResourceLocation("tester:textures/barframe.png")); Minecraft.getInstance().gui.blit(event.getMatrixStack(), posX + 0, posY + 77, 0, 0, 90, 11, 90, 11); //Renders Bar Frame RenderSystem.setShaderTexture(0, new ResourceLocation("tester:textures/bartest.png")); Minecraft.getInstance().gui.blit(event.getMatrixStack(), posX + 2, posY + 79, 0, 0, barlength, 7, 86, 7); //Renders Bar of length "barlength" }
Replace the positions except for "barlength" with your values.
Step 5(Optional): I Suggest adding the following statement at the begining of the Overlay, but it is not required. under the eventHandler class declaration
if (entity == null) return;
Note: Mcreator has some issues with the default placement of overlays and guis when it comes to scaling. Until this is fixed to get the proper spot for each scale requires different custom code that you can find in another tutorial. This is only how to make the bar.
Actually, in retrospect, it would probably work a bit better to overlay the overlay over the GUI instead of doing it in a GUI as overlay's are a little easier to make dynamic. GUI's tend to be difficult to make something dynamic.
Thank You,
ill give it a try
How Would I Get My Global Variable In This The Scope Is Player_Lifetime and the type is number (Int)
Hello,
I fiquired Out how to use custom variables in the overlay
and the declaring of the variables didnt work the way shown but they need to be under bottom "}"
and im not sure how to show you or anyone else wanting to use global variables but here is the copy of my overlay.java
line 44 is were the custom code begins
https://pastebin.com/weByP3V3
Hey, this would be pretty helpful for what I'm doing. Is there a way to not use custom code to lock the elements or could this be implemented within custom code within a procedure?
Only asking because I am thinking of adding some plugins and don't want to set anything in stone before I finalise my mod features.
@Fragant No, you need the code to make it work I've looked into it
Hey just revisiting this after a while and looking at this once again, wondering if you know how to make a vertical bar as well, to have to sort of fill from down to up. Tried positioning it but ended up making it upside down or something.
@Space_Mex you forgot to tell people to import modVariables. That's the key
Hi, I know this was a while ago but I am trying it in Mcreator 2023.2 and it doesn't work, it specifically says cannot find symbol for all the lines you said to write
@AkiraYukiharu What do I type in to import modVariables?