[ZScript] "PowerupTime" in StatusBar

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
Ac!d
Posts: 351
Joined: Tue Apr 02, 2019 5:13 am

[ZScript] "PowerupTime" in StatusBar

Post by Ac!d »

I want to turn this line of SBARINFO

Code: Select all

DrawNumber 4, TINYFONT, Grey, PowerupTime("Radsuit"), WhenNotZero/*FillZeros*/, -10, 50;
in ZScript.

How do I do that ?
User avatar
Player701
 
 
Posts: 1710
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: [ZScript] "PowerupTime" in StatusBar

Post by Player701 »

Well, this isn't easy to do in one line.

NB: You can use either an SBARINFO-based HUD or a ZScript-based HUD, not both at the same time.

First of all, you need to have created a HUDFont instance. This is generally done in the Init method of your HUD class. Since you're drawing a number, the font should be monospaced (the spacing value is usually derived from the width of the character representing the digit "0").

Code: Select all

class MyHud : BaseStatusBar
{
    private HUDFont _tinyFont;
    
    override void Init()
    {
        Super.Init();
        
        ...
        
        // Get the Font instance representing the font.
        Font fnt = "TINYFONT";

        // Create a HUDFont instance for use with the HUD drawing API.
        _tinyFont = HUDFont.Create(fnt, fnt.GetCharWidth("0"), Mono_CellLeft);
    }
}
Note that gzdoom.pk3 does not appear to contain a font called TINYFONT, so I assume you have defined it via FONTDEFS.

Next, somewhere in the Draw method of your HUD class, you should check if the player actually has a radiation suit equipped. This is done by checking the player's inventory for PowerIronFeet. If the powerup is present, you can convert its remaining time from tics to seconds and draw the resulting value:

Code: Select all

override void Draw(int state, double TicFrac)
{
    ...
    
    // Let's see if the player has a radiation suit.
    let radSuit = Powerup(CPlayer.mo.FindInventory('PowerIronFeet'));
    
    if (radSuit != null)
    {
        // Get the amount of seconds left (tics / ticrate).
        int secondsLeft = int(Ceil(double(radSuit.EffectTics) / GameTicRate));

        // Draw the resulting value.
        DrawString(_tinyFont, FormatNumber(secondsLeft, 4), (-10, -50), DI_TEXT_ALIGN_RIGHT|DI_NOSHADOW, Font.CR_GREY);
    }
}
The coordinates may be different compared to SBARINFO, but I'm not 100% sure of that since I haven't worked with it for a very long time.
Post Reply

Return to “Scripting”