ZScript: Status Bar Questions

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

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!)
User avatar
Major Cooke
Posts: 8193
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: ZScript: Status Bar Questions

Post by Major Cooke »

AFADoomer, I might suggest doing it a bit differently. At any point, Graf could alter the function if he needs to fix a bug or something. If said changes doesn't affect parameter count, you won't have to change anything. (Yes I know you used DrawBar, and I used DrawString. Concept is the same because it affects both equally.)

Code: Select all

// Uniform scaling.
void DrawStringScaled(HUDFont font, String string, Vector2 pos, int flags = 0, int translation = Font.CR_UNTRANSLATED, double Alpha = 1., int wrapwidth = -1, int linespacing = 4, double scaling = 1.0)
{
    Vector2 OldScale = BaseSize, TempScale = BaseSize;
    Vector2 ScaledPos = pos;
    
    if (scaling == 0.0) //Why draw anything? Also to prevent division by 0.
        return;
    
    scaling = abs(scaling);
    
    // Thankfully, scaling up is directly that -- smaller screen coordinates and 
    // larger texture size which is automatically handled by the engine.
    bool sized = (scaling != 1.0);
    if (sized)
    {
        TempScale /= scaling;
        ScaledPos /= scaling;
    }
    
    // Change to the new scale, draw it, and then set it back.
    if (sized) SetSize(0, TempScale.X, TempScale.Y);
    DrawString(font, string, ScaledPos, flags, translation, Alpha, wrapwidth, linespacing);
    if (sized) SetSize(0, OldScale.X, OldScale.Y);
    
} 
This works for status bar sections, but I have yet to fully try it out on full screen huds which has a different coordinate system.
User avatar
Xaser
 
 
Posts: 10773
Joined: Sun Jul 20, 2003 12:15 pm

Re: ZScript: Status Bar Questions

Post by Xaser »

Bit of a bump, but this seems like the right thread for it.

Would someone mind giving a quick explanation of what all the DI_ flags do? I'm trying to translate an old ACS HUD to ZScript but I'm having trouble figuring out how to get the offsets to translate correctly.


[EDIT] Maybe if it helps to ask a more specific series of questions: I'm effectively trying to place a 640x480 virtual screen at the bottom-center of the HUD, then draw some images to it. Bonus points if I can also set it up (for translation's sake) such that:
  • (0, 0) is the top-left of the virtual screen
  • Image coordinates correspond to the top-left corner of the image.
[DOUBLE-EDIT] Well, I've discovered "drawOffset" and figured out how to achieve my own use case, though some of the deeper bits are still a bit of a mystery.

[YET-ANOTHER-EDIT] Is it possible to make DrawString respect line breaks?

[HOLY-EDIT-STREAK-BATMAN] Whew, I'm on a roll on answering my own questions. Looks like DrawString needs some sort of value greater than zero in the "wrapwidth" field or else wrapping won't work (since V_BreakLines never gets called). Seems weird -- wince the default for this parameter is -1, would it be reasonable to suggest a value of 0 to mean "respect line breaks, but don't use a wrap width"? From a quick glance, it looks like V_BreakLines may need a tweak to support zero, though.
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: ZScript: Status Bar Questions

Post by Gutawer »

I only really know about the offset flags, but I believe I can explain those pretty well, so here goes:

The DI_ITEM_ flags control where the drawing position of the image is. So, for example, DI_ITEM_LEFT_TOP means that the specified position should be treated as the top left of the image, meaning that it will be drawn diagonally down-right. DI_ITEM_CENTER, then, would mean to treat the position as the centre of the image, meaning that it gets drawn from the centre outwards. I hope that that makes sense.

The DI_SCREEN_ flags only work with a full screen HUD, and control which corner the image is drawn from, since fullscreen huds use corner offsets for drawing. So, for example, DI_SCREEN_LEFT_BOTTOM would mean to start drawing from the coordinate (0, screen size y), with a position of (8, -8) meaning to start drawing 8 pixels away from the bottom left corner. DI_SCREEN_RIGHT_BOTTOM would mean to start drawing from coordinate (screen size x, screen size y), and position (-8, -8) would mean to start drawing 8 pixels away from the bottom right corner. You can combine the two types of flags, but remember - DI_SCREEN_ is for fullscreen HUDs only.

There's also DI_TEXT_ALIGN_, but those are pretty self explanatory.

About the other DI_ flags, I have no idea how those work, so I can't explain those, but I hope this helps at least a bit, heh.
User avatar
Xaser
 
 
Posts: 10773
Joined: Sun Jul 20, 2003 12:15 pm

Re: ZScript: Status Bar Questions

Post by Xaser »

Thanks there! I kinda vaguely had an idea of the DI_ITEM_* options, but wasn't mentally making the leap on DI_SCREEN since I'm used to the default SBARINFO story of "negative means start from bottom/right" -- which I suppose is implied by "DI_SCREEN_AUTO" (the default).
User avatar
Major Cooke
Posts: 8193
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: ZScript: Status Bar Questions

Post by Major Cooke »

Thankfully this isn't rocket science like menus are. :P It's otherwise quite similar to sbarinfo but you can do a lot better and cleaner code with it. Even fancy effects like render style and alpha setting if you want, or colors, etc.

If you're really feeling adventurous, or outright insane, you could put the weapon drawing in there too. Not recommended though.
Blue Shadow
Posts: 5018
Joined: Sun Nov 14, 2010 12:59 am

Re: ZScript: Status Bar Questions

Post by Blue Shadow »

Is there a way I can draw the slot number of the weapon currently being equipped? I tried LocateWeapon(), but I ran into a scope issue (I think) when calling it. This is how I used it:

Code: Select all

bool nch_weaponfound;
int nch_weaponslot;

[nch_weaponfound, nch_weaponslot] = CPlayer.weapons.LocateWeapon(CPlayer.ReadyWeapon.GetClass()); 
The error says:

Code: Select all

Readonly struct on left hand side of LocateWeapon not allowed
User avatar
Major Cooke
Posts: 8193
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: ZScript: Status Bar Questions

Post by Major Cooke »

Status bars are UI only, and 'weapons' is defined within the player which is PLAY scoped. What you can try to do though is invoke a WeaponSlots struct within the status bars like this:

Code: Select all

WeaponSlots w;
int i1, i2;
bool weaponFound;

Class<Weapon> wep = CPlayer.ReadyWeapon.GetClass();
if (wep)
{
     [weaponFound, i1, i2] = w.LocateWeapon(wep);
}
Note, this is untested. Chances are it may not work, in which case the only other option is to inject an inventory item into the player that automatically changes its own amount with DoEffect()'s override (performing the LocateWeapon function as needed), grab the amount and go from there. That can be given on WorldSpawned event.
Blue Shadow
Posts: 5018
Joined: Sun Nov 14, 2010 12:59 am

Re: ZScript: Status Bar Questions

Post by Blue Shadow »

weapons is part of the PlayerInfo struct, the same struct ReadyWeapon is part of. Yet, ReadyWeapon can be used in a ui scope without problems (I know ReadyWeapon is not a struct).

As for the solution, I had already tried something like that, and the engine crashed with a "tried to access address 00000..." error or something (I can't really remember the details).
Major Cooke wrote:Note, this is untested. Chances are it may not work, in which case the only other option is to inject an inventory item into the player that automatically changes its own amount with DoEffect()'s override (performing the LocateWeapon function as needed), grab the amount and go from there. That can be given on WorldSpawned event.
I thought we are past the inventory hacks... If this is the only solution, then I might as well stick with the ACS-SBARINFO-DECORATE combo. :?
User avatar
Major Cooke
Posts: 8193
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: ZScript: Status Bar Questions

Post by Major Cooke »

Blue Shadow wrote:I thought we are past the inventory hacks... If this is the only solution, then I might as well stick with the ACS-SBARINFO-DECORATE combo. :?
Terrible idea IMO. Face it, there will be hacks even with zscript. Shit happens, but at least ZScript helps minimize the need. You can't do anything about it unless you ask for a feature suggestion (provided Graf doesn't [No] it). This is just... LESS of a hack, by far. And it's a lot less likely to interfere with other mods if you seek compatibility.

Funny thing is, DamNums uses an inventory hack. Still works perfectly good.
User avatar
Xaser
 
 
Posts: 10773
Joined: Sun Jul 20, 2003 12:15 pm

Re: ZScript: Status Bar Questions

Post by Xaser »

@Blue Shadow: This looks like an oversight worth bugreporting. ZDoom normally blocks methods from being called on structs that are read-only (which CPlayer.weapons is when you're in the UI context), unless they're marked 'const'. Looks to me that "LocateWeapon" is safe to mark as 'const', but it isn't in gzdoom.pk3 (yet).

I really ought to get a development environment set up on this machine again. It's probably easier to patch, test, and PR this than it is to write about it. :P
User avatar
Major Cooke
Posts: 8193
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: ZScript: Status Bar Questions

Post by Major Cooke »

So I'm having some difficulty with status bars and I believe these were present since the beginning, though I never noticed because I never played on those resolutions. 1600x900 is my default.

Here's my sbar code which is more or less a straight port from sbarinfo to zscript itself.

Does anyone see me doing anything wrong here and why the huds stick out over the edges? I'm trying to figure this out without resorting to enabling full screen offsets, I even tried enabling force scale by adding DI_FORCESCALE to the imageAlignment variable. Nope.
User avatar
cotton_disciple
Posts: 55
Joined: Fri Jul 03, 2015 4:22 am
Location: Russian Federation

Re: ZScript: Status Bar Questions

Post by cotton_disciple »

Does anyone know how to use DrawPowerups? I can't see anything...
User avatar
wildweasel
Posts: 21706
Joined: Tue Jul 15, 2003 7:33 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): A lot of them
Graphics Processor: Not Listed

Re: ZScript: Status Bar Questions

Post by wildweasel »

Okay, so, I'm currently in the process of learning ZScript to replace the increasingly-complicated HUD in ww-cola3 with something more sane, primarily to have a nicer way of doing stuff like this through a simple 4-line function instead of 30-some InInventory checks. What I'm presently trying to figure out, though: is there a way to make a ZScript HUD completely ignore the "Stretch HUD to aspect ratio" option? I'm planning to make this work like SBARINFO's FullscreenOffsets, and all of my HUD graphics are designed for an uncorrected aspect ratio, not the 1.2:1-corrected Doom aspect.

[edit] Actually, I have another, much larger question. Since I already have the one function I needed to make in ZScript for this HUD, I'm looking to do some porting of the existing SBARINFO to ZScript. As an absolute noob to ZScript in general, what are some tips you could give to make this a smoother process going into ZScript? Here is the existing SBARINFO for reference:
Spoiler:
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia

Re: ZScript: Status Bar Questions

Post by Matt »

I found it easier to just recreate everything from scratch without any consideration for preserving the original aesthetic (though keeping most elements generally in more or less the same place), but I was already working with ZScript for a few months by then and was already a lot more comfortable with writing ZScript than even reading SBARINFO.
User avatar
wildweasel
Posts: 21706
Joined: Tue Jul 15, 2003 7:33 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): A lot of them
Graphics Processor: Not Listed

Re: ZScript: Status Bar Questions

Post by wildweasel »

Vaecrius wrote:I found it easier to just recreate everything from scratch without any consideration for preserving the original aesthetic (though keeping most elements generally in more or less the same place), but I was already working with ZScript for a few months by then and was already a lot more comfortable with writing ZScript than even reading SBARINFO.
Placement is essentially a non-issue, since it's fairly easy to guess which numbers are the on-screen coordinates. It's functionality that I'm having problems with. Key indicators, the inventory box, bars, that sort of thing. I have no idea how to do any of those things in ZScript right now.

Return to “Scripting”