[TUTORIAL] Percentage Based Bar In Overlay Tutorial - Health Bar Example

Started by Rikurob on

Topic category: User side tutorials

Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[TUTORIAL] Percentage Based Bar In Overlay Tutorial - Health Bar Example
Mon, 08/22/2022 - 14:34 (edited)

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

Image - My Bar

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

Edited by Rikurob on Mon, 08/22/2022 - 14:34
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Actually, in retrospect, it…
Wed, 08/31/2022 - 23:25

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.

Joined Jul 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thank You,  ill give it a…
Thu, 09/01/2022 - 17:50

Thank You, 

ill give it a try 

Joined Jul 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How Would I Get My Global…
Thu, 09/01/2022 - 18:43

How Would I Get My Global Variable In This The Scope Is Player_Lifetime and the type is number (Int)

Joined Jul 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hello, I fiquired Out how to…
Thu, 09/01/2022 - 20:43

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 "}" 

 

@SubscribeEvent(priority = EventPriority.HIGHEST)
    public static void eventHandler(RenderGameOverlayEvent.Pre event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.ALL) {
            int w = event.getWindow().getGuiScaledWidth();
            int h = event.getWindow().getGuiScaledHeight();
            int posX = w / 2;
            int posY = h / 2;
            Level _world = null;
            double _x = 0;
            double _y = 0;
            double _z = 0;
            Player entity = Minecraft.getInstance().player;
            if (entity != null) {
                _world = entity.level;
                _x = entity.getX();
                _y = entity.getY();
                _z = entity.getZ();
    
            }

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

Joined Jan 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hey, this would be pretty…
Sun, 10/09/2022 - 23:12

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.

Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@Fragant No, you need the…
Fri, 10/21/2022 - 16:23

@Fragant No, you need the code to make it work I've looked into it

Joined Jan 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hey just revisiting this…
Wed, 01/18/2023 - 13:21

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.

Joined Aug 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@Space_Mex you forgot to…
Thu, 09/14/2023 - 13:59

@Space_Mex you forgot to tell people to import modVariables. That's the key

Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hi, I know this was a while…
Thu, 02/08/2024 - 19:45

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

Joined Jul 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@AkiraYukiharu What do I…
Tue, 05/21/2024 - 20:52

@AkiraYukiharu What do I type in to import modVariables?