Better Way To Check For Multiple Values In An Anon Function?

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!)
Post Reply
User avatar
22alpha22
Posts: 303
Joined: Fri Feb 21, 2014 5:04 pm
Graphics Processor: nVidia with Vulkan support
Location: Montana, USA

Better Way To Check For Multiple Values In An Anon Function?

Post by 22alpha22 »

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.

Digital ammo readout code:
Spoiler:
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.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49073
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Better Way To Check For Multiple Values In An Anon Funct

Post by Graf Zahl »

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.
User avatar
22alpha22
Posts: 303
Joined: Fri Feb 21, 2014 5:04 pm
Graphics Processor: nVidia with Vulkan support
Location: Montana, USA

Re: Better Way To Check For Multiple Values In An Anon Funct

Post by 22alpha22 »

Wow that works perfectly, thanks! :D
Post Reply

Return to “Scripting”