[Tutorial] How To Use Global Variables In Overlay/.java Files

Started by Space_Mex on

Topic category: User side tutorials

Joined Jul 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[Tutorial] How To Use Global Variables In Overlay/.java Files

Hello Today Ill Be Demonstrating How To Use Global Variables In Mcreator.

    I'm Assuming You Know How To Lock You Mod Files 

    First We'll Need To Import Our Variables Should Look Like This

    Case Matters ModName Means Camel Casing

    Example Of Camel Casing:

    CropsPlus

    "import <namespace>.<modname>.network.<ModName>ModVariables;"

    For Me It Looks Like:

    "import me.spacemex.cropsplus.network.CropsPlusModVariables;"

    Now We Need To Declare The Variables Under A Constructer That You'll Be Editing/Adding Your Variables To 

    To Do This You Must Know Some Basic Java.

    For This Example I'll Be Using Doubles

    To Declare We Must Type The Following:
    This Would Be How To Get A Variable Named Water That Is A Number Global Variable

    "double water = (entity.getCapability(<ModName>ModVariables.PLAYER_VARIABLES_CAPABILITY, null)
    .orElse(new <ModName>ModVariables.PlayerVariables())).Water"

    This Is What Mine Would Look Like:

    "double water = (entity.getCapability(CropsPlusModVariables.PLAYER_VARIABLES_CAPABILITY, null)
     .orElse(new CropsPlusModVariables.PlayerVariables())).Water;"

And This Is How To Add Global Variables To Mcreator Via Editing The .java File

    Here Are Some Extras :

    How To Make A Percentage From Global Variables In An Overlay:
    To Learn To Make A Progress Bar Please Read This By Rikurob
    He's A Very Nice Person
    So With Out Any More Delay Lets Make A Percentage 

    So We'll Need A Min Of Two Number Variables 

    "//Declaring Global Variables

    double carbs = (entity.getCapability(CropsPlusModVariables.PLAYER_VARIABLES_CAPABILITY, null)
    .orElse(new CropsPlusModVariables.PlayerVariables())).Carbs;

    double MaxCarbs = (entity.getCapability(CropsPlusModVariables.PLAYER_VARIABLES_CAPABILITY, null)
    .orElse(new CropsPlusModVariables.PlayerVariables())).maxCarbs;"

    This Shown Below Is The Basic Formula For Percentage
    Be Careful Java Follows PEMDAS
    Percentage = (x/y)*100
    Which In Our Case Would Be
    Carbs = x
    MaxCarbs = y 

    "//Calc The Percentage
     //Formula percent = (amount/max)*100

     //Below Declares Our Percent As A Double 
      double carbpercent = (carbs/MaxCarbs)*100;"

      Now That We Declared Our Percentage Formula 
      We Can Add It To A Text In Our Overlay 
      We'll Be Adding It To A Text Label 
      Basically Our Percentage Is Going To Show Up As 100.0%
      But Keep In Mind You Can Do The Same This With Floats And Ints I Just Prefer Doubles 

      "Minecraft.getInstance().font.draw(event.getMatrixStack(), carbpercent + "%" , posX + 53, posY + 1, -12829636);"

Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Nice tutorial! May I suggest…
Sat, 09/03/2022 - 03:16

Nice tutorial! May I suggest not using headers for all of the text that isn't not code though?

Joined Jul 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thank You, Yeah Sorry Bout…
Sat, 09/03/2022 - 21:48

Thank You, Yeah Sorry Bout That ill Change it