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!)
I've created a working digital ammo display for one of my weapons, it is rendered to the weapon via overlays and tracks ammo available in the current magazine. The magazine holds 30 rounds and there are two looping overlays that track the ammo, one for the single digits, and one for the tens. Each overlay will render the correct digit that matches the amount of ammo in the mag. For example, say you have 19 rounds in the mag, the first overlay sees that we are in the tens and will render a 1 to left, the second overlay sees that we have 9 and will render a 9 to the right. This all works correctly as it should and looks good in game, but I can't help but feel the way the code is written could be done better.
MagazineTrackerA.Inactive:
TNT1 A 1
Stop
MagazineTrackerA.Active:
TNT1 A 1
{
If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 30)
{
Return State(4);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) >= 20)
{
Return State(3);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) >= 10)
{
Return State(2);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) > 0)
{
Return State(1);
}
Else
{
Return State("MagazineTrackerA.Inactive");
}
}
Loop
DIGF A 1 Bright
Loop
DIGF B 1 Bright
Loop
DIGF C 1 Bright
Loop
DIGF D 1 Bright
Loop
MagazineTrackerB.Inactive:
TNT1 A 1
Stop
MagazineTrackerB.Active:
TNT1 A 1
{
If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 30 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 20 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 10)
{
Return State(1);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 29 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 19 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 9)
{
Return State(10);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 28 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 18 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 8)
{
Return State(9);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 27 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 17 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 7)
{
Return State(8);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 26 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 16 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 6)
{
Return State(7);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 25 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 15 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 5)
{
Return State(6);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 24 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 14 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 4)
{
Return State(5);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 23 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 13 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 3)
{
Return State(4);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 22 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 12 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 2)
{
Return State(3);
}
Else If (CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 21 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 11 || CountInv("AlphaSubmachinegunMagazine",AAPTR_DEFAULT) == 1)
{
Return State(2);
}
Else
{
Return State("MagazineTrackerB.Inactive");
}
}
Loop
DIGF A 1 Bright
Loop
DIGF B 1 Bright
Loop
DIGF C 1 Bright
Loop
DIGF D 1 Bright
Loop
DIGF E 1 Bright
Loop
DIGF F 1 Bright
Loop
DIGF G 1 Bright
Loop
DIGF H 1 Bright
Loop
DIGF I 1 Bright
Loop
DIGF J 1 Bright
Loop
The part I'm not really happy about is the second overlay, MagazineTrackerB.Active. It is responsible for checking for the correct single digit and thus I have check for every possible combination a single digit could appear in, 9, 19, 29 for example. Is there a way I could list every possibility to check against without having to write the whole check out every time using || operators? The way it is currently written works but could get very tedious with larger magazines or god forbid, triple or larger digits.
If you want the last digit of a number use "number % 10". If you want the second digit, use "(number / 10) % 10", for the third one "(number / 100) % 10" and so on.