improving my armor regen code

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
lubba127
Posts: 6
Joined: Tue Aug 17, 2021 2:43 pm
Graphics Processor: nVidia (Modern GZDoom)

improving my armor regen code

Post by lubba127 »

hi! I've been working on a doom wad for a while that includes some acs and decorate code that basically gives the player a halo-esk energy shield. a problem I ran into though is that whenever the player takes armor damage, there's no sound that plays to help indicate that you've taken a hit, unlike taking damage to your health.

I would like to clarify that this is not my code, I borrowed it from this thread: viewtopic.php?f=3&t=53312&hilit=regenerating+armor

Code: Select all

script 4 ENTER
{
//Change this to control the cap.
int ARMORCAP = 100;

//Health and Armor when script (re)starts.
int OldHealth = GetActorProperty(0, APROP_Health);
int Armor = CheckInventory("shields");

//Prevents armor giving if you're dead.
if (OldHealth > 0)
{
   //Waits 5 seconds.
   delay(35*5);

   //Health 5 seconds later.
   int NewHealth = GetActorProperty(0, APROP_Health);

   //Checks if you are taking damage and, if not, regenerates until you do.
   while (NewHealth == OldHealth && Armor < ARMORCAP)
   {
	   GiveInventory("ArmorRegenBonus", 1);
	   delay(14);
	   NewHealth = GetActorProperty(0, APROP_Health); //damage checker
   }
}

delay(5);
restart;
}
is there anything I can do to this code that would play a sound once the player takes armor damage?
Jarewill
 
 
Posts: 1855
Joined: Sun Jul 21, 2019 8:54 am

Re: improving my armor regen code

Post by Jarewill »

Untested, but you can try adding this line:

Code: Select all

   //Checks if you are taking damage and, if not, regenerates until you do.
   while (NewHealth == OldHealth && Armor < ARMORCAP)
   {
      GiveInventory("ArmorRegenBonus", 1);
      delay(14);
      NewHealth = GetActorProperty(0, APROP_Health); //damage checker
   }
   If(Armor && Armor < ARMORCAP){PlaySound(0,"shield_hit");} //<--- This line in this spot
}
However because of how the script is setup, it most likely won't play at the exact moment the player is hit.
lubba127
Posts: 6
Joined: Tue Aug 17, 2021 2:43 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: improving my armor regen code

Post by lubba127 »

Jarewill wrote:Untested, but you can try adding this line:

Code: Select all

   //Checks if you are taking damage and, if not, regenerates until you do.
   while (NewHealth == OldHealth && Armor < ARMORCAP)
   {
      GiveInventory("ArmorRegenBonus", 1);
      delay(14);
      NewHealth = GetActorProperty(0, APROP_Health); //damage checker
   }
   If(Armor && Armor < ARMORCAP){PlaySound(0,"shield_hit");} //<--- This line in this spot
}
However because of how the script is setup, it most likely won't play at the exact moment the player is hit.
i edited the code a bit (i just added Chan_Body and the name of the sound) but even then it doesn't play any sounds. i tried something similar to this before, only adding in one Armor instead of the two, and it gave me the same result of nothing.

i'll play around with it for a bit longer, and if you have anything to add please do. thank you for your help so far!
Jarewill
 
 
Posts: 1855
Joined: Sun Jul 21, 2019 8:54 am

Re: improving my armor regen code

Post by Jarewill »

Okay, I was messing with the script a bit and here's the code that's working:
Spoiler:
Also assuming your armor protects the player fully (as you said the hurt sounds don't play) I added an armor check alongside the health check.
And also, the sounds still lag a lot and don't play most of the time, but this is the closest I can get it to working without rewriting the script fully.

And also, don't forget to recompile the script. I had it happen a lot to me where I thought my code was not working and it was only because I forgot to recompile the script.
lubba127
Posts: 6
Joined: Tue Aug 17, 2021 2:43 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: improving my armor regen code

Post by lubba127 »

wow! that works really well actually. it does lag or just not play sometimes, but its good enough for me!

edit: i did a few more tests, and i still can't figure out how to get the sound working whilst keeping the script structure intact. if you have any ideas that would involve changing the script entirely I'm totally open to it, but if you're done with this code that's totally fine. thank you for your continued help, and sorry for the edits I've made to this reply (if you saw them).
Jarewill
 
 
Posts: 1855
Joined: Sun Jul 21, 2019 8:54 am

Re: improving my armor regen code

Post by Jarewill »

I tried taking a crack at rewriting the script.
Here it is:
Spoiler:
lubba127
Posts: 6
Joined: Tue Aug 17, 2021 2:43 pm
Graphics Processor: nVidia (Modern GZDoom)

Re: improving my armor regen code

Post by lubba127 »

Jarewill wrote:I tried taking a crack at rewriting the script.
Here it is:
Spoiler:
this script is perfect. it works as intended. thank you so much for your help! if you don't mind, I'm gonna incorporate this into my wad with credits to you. if credits aren't enough though, feel free to tell me what else you'd want me to do 8-)
Cheater87
Posts: 63
Joined: Tue Oct 31, 2017 7:57 pm

Re: improving my armor regen code

Post by Cheater87 »

I'd love to have a wad version of this.
Post Reply

Return to “Scripting”