[Script Help] Percentage in Bar Form

Arch

Legions Developer
So, with the help of NightHawk (Thanks a bunch), I've been working on turning the ammo values into percentages and then into bars that eat away as you use ammo. I've got them in the percentages and currently display on the HUD as digits. The top being weapon ammo, bottom being item (grenade) ammo.

Problem.png


I took a look at how the coresHud.gui displays this so I tried using new GuiProgressCtrl and linking it to weaponammoProgress (which sets the value of the percentage of ammo left) . But it doesn't seem to do anything. Is there a way to make this work or lay it out in a different way?
Code:
$weaponammoValue = (100- mCeil(((%maxammo - %ammo)/%maxammo)*100));
 
weaponammoProgress.setValue("<just:center>" @ ($weaponammoValue @ "%"));

Code:
        new GuiProgressCtrl(weaponammoProgress)
        {
            maxlength = "1024";
            margin = "0 0 0 0";
            padding = "0 0 0 0";
            isContainer = "1";
            profile = "CoreCooldownProfile";
            horizsizing = "center";
            vertsizing = "bottom";
            position = "0 100";
            extent = "202 7";
            minextent = "100 20";
            cansave = "1";
            visible = "1";
            canSaveDynamicFields = "0";
               
            new GuiBitmapCtrl()
            {
                isContainer = "1";
                bitmap = "mods/autoexec/Sync/AmmoBar/top.png";
                profile = "SyncTransBG";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "202 7";
                position = "0 0";
            };
 

Volt Cruelerz

Legions Developer
Well, it should work just fine. A progressbar will display anything between 0 and 1 and currently you're displaying between 0 and 100. So change ammo assignment line to...

Code:
$weaponammoValue = 1-(%maxammo - %ammo)/%maxammo;
then the following line becomes...
Code:
weaponammoProgress.setValue($weaponammoValue);
Then the progress bar should function properly (unless there's an error elsewhere).
 

Arch

Legions Developer
Well, it should work just fine. A progressbar will display anything between 0 and 1 and currently you're displaying between 0 and 100. So change ammo assignment line to...

Code:
$weaponammoValue = 1-(%maxammo - %ammo)/%maxammo;
then the following line becomes...
Code:
weaponammoProgress.setValue($weaponammoValue);
Then the progress bar should function properly (unless there's an error elsewhere).
Unfortunately, this didn't work.
 

Volt Cruelerz

Legions Developer
how about you try using setValue() to set it to 1 regardless of ammo. If it doesn't work, that means that it's broken elsewhere.
 

Arch

Legions Developer
how about you try using setValue() to set it to 1 regardless of ammo. If it doesn't work, that means that it's broken elsewhere.
Well, that didn't work either. I think something's wrong with the progress bar control. Is there an alternative way of getting the wanted outcome?
 

Volt Cruelerz

Legions Developer
Ask someone better at gui's than myself... Rockey and Disaster are probably the most likely.

Come to think of it, another example you could look at is Rockey's charged throw script that is now in the game itself.
 

Triad

Legions Developer
You need add the element to the GameHud if you are not defining it within the game HUD in gameHud.gui.

GameHud.add(weaponammoProgress);

I recommend just defining it within the GameHud rather than calling GameHud.add().
 

Arch

Legions Developer
You need add the element to the GameHud if you are not defining it within the game HUD in gameHud.gui.

GameHud.add(weaponammoProgress);

I recommend just defining it within the GameHud rather than calling GameHud.add().
So I need to add GameHud.add() for this line? Or do you mean that I need to add one for the control(s), which I do.
Code:
weaponammoProgress.setValue("<just:center>" @ ($weaponammoValue @ "%"));

If you want us to check your code beyond giving general suggestions, you'll need to post the rest of your code.

Code:
package AmmoBarPack
{
    // This is where Weapon Ammo is tracked.
 
    function clientCmdSetWeaponAmmo(%weapon, %ammo)
    {
        parent::clientCmdSetWeaponAmmo(%weapon, %ammo);
        if($WeaponMaxAmmo[%weapon]<=%ammo)
        {
            $WeaponMaxAmmo[%weapon] = %ammo;
        }
    }
     
    function clientCmdSetActiveWeaponAmmo(%ammo)
    {
        parent::clientCmdSetActiveWeaponAmmo(%ammo);
        if($PlayerIsAlive==1)
        {
            if($WeaponMaxAmmo[$ActiveWeapon] >=500){%maxammo = 500;}
            else if ($WeaponMaxAmmo[$ActiveWeapon]<=20){%maxammo = 20;}
            else{%maxammo = $weaponMaxAmmo[$ActiveWeapon];}
   
            $weaponammoValue = (100- mCeil(((%maxammo - %ammo)/%maxammo)*100));
            //$weaponammoValue = 1 - ((%maxammo - %ammo)/%maxammo);
   
            if(%ammo >= 0)
            {
                //echo($weaponammoValue @ "%");
               // weaponammoProgress.setValue("<just:center>" @ ($weaponammoValue @ "%"));
                weaponammoProgress.setValue(1);
            }
        }
    }
 
    // This is where Item Ammo is tracked. In other words, Grenade Count
   
    function clientCmdSetItemAmmo(%item, %ammo)
    {
        parent::clientCmdSetItemAmmo(%item, %ammo);
        if($ItemMaxAmmo[%item]<=%ammo)
        {
            $ItemMaxAmmo[%item] = %ammo;
            echo($ItemMaxAmmo[%item]);
        }
    }
 
    function clientCmdSetActiveItemAmmo(%ammo)
    {
        parent::clientCmdSetActiveItemAmmo(%ammo);
        if($PlayerIsAlive==1)
        {
            %maxammo = 5;
            $itemammoValue = (100 - mCeil(((%maxammo - %ammo)/%maxammo)*100));
   
            if(%ammo >= 0)
            {
                //echo($itemValue @ "%");
                itemammoProgress.setValue("<just:center>" @ ($itemammoValue @ "%"));
            }
        }
    }
 
    function AmmoGui()
    {
        new GuiControl(AmmoGui)
        {
            isContainer = "1";
            profile = "SyncMenuBG";
            horizsizing = "center";
            vertsizing = "bottom";
            extent = "50 30";
            position = "0 50";
            visible = "1";
        };
       
            /*new GuiMLTextCtrl(weaponammoProgress)
            {
                profile = "SpeedDisplayProfile";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "50 10";
                position = "0 0";
            };
       
            new GuiMLTextCtrl(itemammoProgress)
            {
                profile = "SpeedDisplayProfile";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "50 10";
                position = "0 15";
            };*/
       
        new GuiProgressCtrl(weaponammoProgress)
        {
            maxlength = "1024";
            margin = "0 0 0 0";
            padding = "0 0 0 0";
            isContainer = "1";
            profile = "CoreCooldownProfile";
            horizsizing = "center";
            vertsizing = "bottom";
            position = "0 100";
            extent = "202 7";
            minextent = "100 20";
            cansave = "1";
            visible = "1";
            canSaveDynamicFields = "0";
         
            new GuiBitmapCtrl()
            {
                isContainer = "1";
                bitmap = "mods/autoexec/Sync/AmmoBar/top.png";
                profile = "SyncTransBG";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "202 7";
                position = "0 0";
            };
        };
        GameHud.add(AmmoGui);
    }
 
    function HudOverlay::AddHudElements(%this)
    {
        parent::AddHudElements(%this);
   
        AmmoGui();
   
        // Since we have the ammo displayed as bars, why not remove the default display(s).
        %index = %this.addHudElement();
        %this.setHudElementPosition(%index, 0, -500);
        %this.setHudElementPositionAnchor(%index, "HCenter", "VCenter");
        %this.setHudElementText(%index, "", AmmoDisplayProfile);
        %this.setHudElementParent(%index, $HudOverlay::BaseContainer);
        $HudElement::WeaponAmmoDisplay = %index;
   
        %index = %this.addHudElement();
        %this.setHudElementPosition(%index, 0, -500);
        %this.setHudElementPositionAnchor(%index, "HCenter", "VCenter");
        %this.setHudElementText(%index, "", AmmoDisplayProfile);
        %this.setHudElementParent(%index, $HudOverlay::SyncBG1);
        $HudElement::ItemAmmoDisplay = %index;
    }
};
activatepackage(AmmoBarPack);
 

Volt Cruelerz

Legions Developer
Tried this just now with some modifications and I got the box to show up but the bar won't fill. I'll take a more detailed look at it later...
 

Triad

Legions Developer
This works for me.

Code:
        new GuiProgressCtrl(weaponammoProgress)
        {
            maxlength = "1024";
            margin = "0 0 0 0";
            padding = "0 0 0 0";
            isContainer = "1";
            profile = "CoreCooldownProfile";
            horizsizing = "center";
            vertsizing = "bottom";
            position = "0 100";
            extent = "202 7";
            minextent = "100 20";
            cansave = "1";
            visible = "1";
            canSaveDynamicFields = "0";
               
            new GuiBitmapCtrl()
            {
                isContainer = "1";
                bitmap = "mods/autoexec/Sync/AmmoBar/top.png";
                profile = "SyncTransBG";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "202 7";
                position = "0 0";
            };
        };
        
        new GuiControl(AmmoGui)
        {
            isContainer = "1";
            profile = "SyncMenuBG";
            horizsizing = "center";
            vertsizing = "bottom";
            extent = "50 30";
            position = "0 50";
            visible = "1";
        };
         
            new GuiMLTextCtrl(weaponammoProgress)
            {
                profile = "SpeedDisplayProfile";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "50 10";
                position = "0 0";
            };
         
            new GuiMLTextCtrl(itemammoProgress)
            {
                profile = "SpeedDisplayProfile";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "50 10";
                position = "0 15";
            };
         
        new GuiProgressCtrl(weaponammoProgress)
        {
            maxlength = "1024";
            margin = "0 0 0 0";
            padding = "0 0 0 0";
            isContainer = "1";
            profile = "CoreCooldownProfile";
            horizsizing = "center";
            vertsizing = "bottom";
            position = "0 100";
            extent = "202 7";
            minextent = "100 20";
            cansave = "1";
            visible = "1";
            canSaveDynamicFields = "0";
           
            new GuiBitmapCtrl()
            {
                isContainer = "1";
                bitmap = "mods/autoexec/Sync/AmmoBar/top.png";
                profile = "SyncTransBG";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "202 7";
                position = "0 0";
            };
        };
        
        GameHud.add(weaponammoProgress);
        GameHud.add(AmmoGui);

package AmmoBarPack
{
    // This is where Weapon Ammo is tracked.
 
    function clientCmdSetWeaponAmmo(%weapon, %ammo)
    {
        parent::clientCmdSetWeaponAmmo(%weapon, %ammo);
        if($WeaponMaxAmmo[%weapon]<=%ammo)
        {
            $WeaponMaxAmmo[%weapon] = %ammo;
        }
    }
       
    function clientCmdSetActiveWeaponAmmo(%ammo)
    {
        parent::clientCmdSetActiveWeaponAmmo(%ammo);
        if($PlayerIsAlive==1)
        {
            if($WeaponMaxAmmo[$ActiveWeapon] >=500){%maxammo = 500;}
            else if ($WeaponMaxAmmo[$ActiveWeapon]<=20){%maxammo = 20;}
            else{%maxammo = $weaponMaxAmmo[$ActiveWeapon];}
     
            $weaponammoValue = 1 - ((%maxammo - %ammo)  /%maxammo);
     
            if(%ammo >= 0)
            {
                //echo($weaponammoValue @ "%");
                weaponammoProgress.setValue($weaponammoValue);
            }
        }
    }
 
    // This is where Item Ammo is tracked. In other words, Grenade Count
     
    function clientCmdSetItemAmmo(%item, %ammo)
    {
        parent::clientCmdSetItemAmmo(%item, %ammo);
        if($ItemMaxAmmo[%item]<=%ammo)
        {
            $ItemMaxAmmo[%item] = %ammo;
            echo($ItemMaxAmmo[%item]);
        }
    }
 
    function clientCmdSetActiveItemAmmo(%ammo)
    {
        parent::clientCmdSetActiveItemAmmo(%ammo);
        if($PlayerIsAlive==1)
        {
            %maxammo = 5;
            $itemammoValue = 1 - ((%maxammo - %ammo) / %maxammo);
     
            if(%ammo >= 0)
            {
                //echo($itemValue @ "%");
                itemammoProgress.setValue($itemammoValue);
            }
        }
    }
 
    function HudOverlay::AddHudElements(%this)
    {
        parent::AddHudElements(%this);
     
        // Since we have the ammo displayed as bars, why not remove the default display(s).
        %this.setHudElementVisible($HudElement::WeaponAmmoDisplay, false);
        %this.setHudElementVisible($HudElement::ItemAmmoDisplay, false);
    }
};
activatepackage(AmmoBarPack);
 

Arch

Legions Developer
This works for me.

Code:
        new GuiProgressCtrl(weaponammoProgress)
        {
            maxlength = "1024";
            margin = "0 0 0 0";
            padding = "0 0 0 0";
            isContainer = "1";
            profile = "CoreCooldownProfile";
            horizsizing = "center";
            vertsizing = "bottom";
            position = "0 100";
            extent = "202 7";
            minextent = "100 20";
            cansave = "1";
            visible = "1";
            canSaveDynamicFields = "0";
           
            new GuiBitmapCtrl()
            {
                isContainer = "1";
                bitmap = "mods/autoexec/Sync/AmmoBar/top.png";
                profile = "SyncTransBG";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "202 7";
                position = "0 0";
            };
        };
     
        new GuiControl(AmmoGui)
        {
            isContainer = "1";
            profile = "SyncMenuBG";
            horizsizing = "center";
            vertsizing = "bottom";
            extent = "50 30";
            position = "0 50";
            visible = "1";
        };
     
            new GuiMLTextCtrl(weaponammoProgress)
            {
                profile = "SpeedDisplayProfile";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "50 10";
                position = "0 0";
            };
     
            new GuiMLTextCtrl(itemammoProgress)
            {
                profile = "SpeedDisplayProfile";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "50 10";
                position = "0 15";
            };
     
        new GuiProgressCtrl(weaponammoProgress)
        {
            maxlength = "1024";
            margin = "0 0 0 0";
            padding = "0 0 0 0";
            isContainer = "1";
            profile = "CoreCooldownProfile";
            horizsizing = "center";
            vertsizing = "bottom";
            position = "0 100";
            extent = "202 7";
            minextent = "100 20";
            cansave = "1";
            visible = "1";
            canSaveDynamicFields = "0";
       
            new GuiBitmapCtrl()
            {
                isContainer = "1";
                bitmap = "mods/autoexec/Sync/AmmoBar/top.png";
                profile = "SyncTransBG";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "202 7";
                position = "0 0";
            };
        };
     
        GameHud.add(weaponammoProgress);
        GameHud.add(AmmoGui);
 
package AmmoBarPack
{
    // This is where Weapon Ammo is tracked.
 
    function clientCmdSetWeaponAmmo(%weapon, %ammo)
    {
        parent::clientCmdSetWeaponAmmo(%weapon, %ammo);
        if($WeaponMaxAmmo[%weapon]<=%ammo)
        {
            $WeaponMaxAmmo[%weapon] = %ammo;
        }
    }
   
    function clientCmdSetActiveWeaponAmmo(%ammo)
    {
        parent::clientCmdSetActiveWeaponAmmo(%ammo);
        if($PlayerIsAlive==1)
        {
            if($WeaponMaxAmmo[$ActiveWeapon] >=500){%maxammo = 500;}
            else if ($WeaponMaxAmmo[$ActiveWeapon]<=20){%maxammo = 20;}
            else{%maxammo = $weaponMaxAmmo[$ActiveWeapon];}
 
            $weaponammoValue = 1 - ((%maxammo - %ammo)  /%maxammo);
 
            if(%ammo >= 0)
            {
                //echo($weaponammoValue @ "%");
                weaponammoProgress.setValue($weaponammoValue);
            }
        }
    }
 
    // This is where Item Ammo is tracked. In other words, Grenade Count
 
    function clientCmdSetItemAmmo(%item, %ammo)
    {
        parent::clientCmdSetItemAmmo(%item, %ammo);
        if($ItemMaxAmmo[%item]<=%ammo)
        {
            $ItemMaxAmmo[%item] = %ammo;
            echo($ItemMaxAmmo[%item]);
        }
    }
 
    function clientCmdSetActiveItemAmmo(%ammo)
    {
        parent::clientCmdSetActiveItemAmmo(%ammo);
        if($PlayerIsAlive==1)
        {
            %maxammo = 5;
            $itemammoValue = 1 - ((%maxammo - %ammo) / %maxammo);
 
            if(%ammo >= 0)
            {
                //echo($itemValue @ "%");
                itemammoProgress.setValue($itemammoValue);
            }
        }
    }
 
    function HudOverlay::AddHudElements(%this)
    {
        parent::AddHudElements(%this);
 
        // Since we have the ammo displayed as bars, why not remove the default display(s).
        %this.setHudElementVisible($HudElement::WeaponAmmoDisplay, false);
        %this.setHudElementVisible($HudElement::ItemAmmoDisplay, false);
    }
};
activatepackage(AmmoBarPack);
Are two progressctrls needed?

Edit: Seems to work without that extra one on top. Thanks a bunch. :D
 

Volt Cruelerz

Legions Developer
When I tried copying that over to my file, Legions wouldn't even load. It would create a corresponding .dso file, but that's it. No log file, no legions. :(
 

Triad

Legions Developer
Are two progressctrls needed?
No, I just changed things up a bit when I was working on it.
When I tried copying that over to my file, Legions wouldn't even load. It would create a corresponding .dso file, but that's it. No log file, no legions. :(
I just stuck it in gameHud.gui rather than it's own file when I was testing it. If there are more problems I can take a look.
 

RockeyRex

Legions Developer
Eh... Another non tested look at the code.
Code:
package AmmoBarPack
{
    // This is where Weapon Ammo is tracked.
 
    function clientCmdSetWeaponAmmo(%weapon, %ammo)
    {
        parent::clientCmdSetWeaponAmmo(%weapon, %ammo);
        if($WeaponMaxAmmo[%weapon]<=%ammo)
        {
            $WeaponMaxAmmo[%weapon] = %ammo;
        }
    }
 
    function clientCmdSetActiveWeaponAmmo(%ammo)
    {
        parent::clientCmdSetActiveWeaponAmmo(%ammo);
        if ($PlayerIsAlive==1)
        {
            if ($WeaponMaxAmmo[$ActiveWeapon] >=500){%maxammo = 500;}
            else if ($WeaponMaxAmmo[$ActiveWeapon]<=20){%maxammo = 20;}
            else { %maxammo = $weaponMaxAmmo[$ActiveWeapon];}
 
            $weaponammoValue = mCeil((%maxammo - %ammo)/%maxammo);
 
            if (%ammo < 0)
            {
              $weaponammoValue = 0;
            }
            weaponammoProgress.setValue($weaponammoValue);
        }
    }
 
    // This is where Item Ammo is tracked. In other words, Grenade Count
 
    function clientCmdSetItemAmmo(%item, %ammo)
    {
        parent::clientCmdSetItemAmmo(%item, %ammo);
        if($ItemMaxAmmo[%item]<=%ammo)
        {
            $ItemMaxAmmo[%item] = %ammo;
            echo($ItemMaxAmmo[%item]);
        }
    }
 
    function clientCmdSetActiveItemAmmo(%ammo)
    {
        parent::clientCmdSetActiveItemAmmo(%ammo);
        if($PlayerIsAlive==1)
        {
            %maxammo = 5;
            $itemammoValue = (100 - mCeil(((%maxammo - %ammo)/%maxammo)*100));
 
            if(%ammo >= 0)
            {
                //echo($itemValue @ "%");
                itemammoProgress.setValue("<just:center>" @ ($itemammoValue @ "%"));
            }
        }
    }
 
    function AmmoGui()
    {
        new GuiControl(AmmoGui)
        {
            isContainer = "1";
            profile = "SyncMenuBG";
            horizsizing = "center";
            vertsizing = "bottom";
            extent = "50 30";
            position = "0 50";
            visible = "1";
   
            /*new GuiMLTextCtrl(weaponammoProgress)
            {
                profile = "SpeedDisplayProfile";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "50 10";
                position = "0 0";
            };
   
            new GuiMLTextCtrl(itemammoProgress)
            {
                profile = "SpeedDisplayProfile";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "50 10";
                position = "0 15";
            };*/
   
        new GuiProgressCtrl(weaponammoProgress)
        {
            maxlength = "1024";
            margin = "0 0 0 0";
            padding = "0 0 0 0";
            isContainer = "1";
            profile = "CoreCooldownProfile";
            horizsizing = "center";
            vertsizing = "bottom";
            position = "0 100";
            extent = "202 7";
            minextent = "100 20";
            cansave = "1";
            visible = "1";
            canSaveDynamicFields = "0";
     
            new GuiBitmapCtrl()
            {
                isContainer = "1";
                bitmap = "mods/autoexec/Sync/AmmoBar/top.png";
                profile = "SyncTransBG";
                horizsizing = "right";
                vertsizing = "bottom";
                extent = "202 7";
                position = "0 0";
            };
        };
    };
        GameHud.add(AmmoGui);
    }
 
    function HudOverlay::AddHudElements(%this)
    {
        parent::AddHudElements(%this);
 
        AmmoGui();
 
        // Since we have the ammo displayed as bars, why not remove the default display(s).
        $HudElement::WeaponAmmoDisplay.setHudElementPosition(%index, 0, -500);
        $HudElement::ItemAmmoDisplay.setHudElementPosition(%index, 0, -500);
 
    }
};
activatepackage(AmmoBarPack);

I'll take a better look when I get home... If I remember... To go home...
 

Arch

Legions Developer
Wait... WTF is this? I don't think you can create textured progress bars like this.
Yeah, the bitmap does nothing but cover the progress bar. I just made a custom profile and used that instead of the bitmap. I thought it might work with the bitmap due to the coresHud using a bitmap over a ProgressCtrl. Unfortunately, it doesn't.
 
Top