[ZScript] Dynamic Attack Speed

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
Virathas
Posts: 254
Joined: Thu Aug 10, 2017 9:38 am

[ZScript] Dynamic Attack Speed

Post by Virathas »

Hey everyone!

Before I complete reinventing the wheel, I would like to ask if there is a "simple" way to add dynamic speed for player attacks. Originally, i used a ton of "A_SetTics" that called an ACS script that calculated the amount of tics to adjust. The obvious problem was the rounding error, even a slight increase to the speed would either give no bonus or extreme bonus.

As such, i wanted to move away from the A_SetTics + ACS combo.

The question is, is there an "easy" way to do so in ZScript?

The best i got to (before i discontinued in favor of working on other content), is an inventory item that controls frames of the currently equipped weapon:

Code: Select all

	override void DoEffect()
	{
		Super.DoEffect();
		if (Owner)
		{
			let psp = Owner.player.FindPSprite(PSP_WEAPON);
			let pp = Owner;
			if (psp)
			{
		int CurSpeed = LastSpeed + GetSpeedMod();
		CurSpeed = CurSpeed - 100;
		while (CurSpeed <= -100 || CurSpeed >= 100)
		{
		if (CurSpeed <= -100)
		{
			psp.Tics++;
			pp.Tics++;
			CurSpeed = CurSpeed + 100;
		}
		if (CurSpeed >= 100)
		{
			psp.Tics--;
			if (pp.InStateSequence(pp.CurState, pp.ResolveState("Missile")))
			{
			if (pp.Tics > 0)
			pp.Tics--;
			}
			CurSpeed = CurSpeed - 100;
			if (!psp.Tics) psp.SetState(psp.CurState.NextState);
		}
		}
		LastSpeed = CurSpeed;
			}
		}
	}
	
	virtual int GetSpeedMod()
	{
		int BaseSpeedMod = 100;
		if (Owner.CountInv("SlowSpellEffect"))
		{
			BaseSpeedMod = BaseSpeedMod - 250;
		}
		if (Owner.CountInv("PowerHaste"))
		{
			BaseSpeedMod = BaseSpeedMod + 250;
		}
		if (BaseSpeedMod < -75)
		{
			BaseSpeedMod = -75;
		}
		
		return BaseSpeedMod;
	}
(The code was loosely based on the built-in DoubleFiringSpeed powerup)

Is this a good way? Is there a better way? Will the code conflict with A_SetTics? Would i be able to get information to "not adjust" the time of a specific frame?
Rey65
Posts: 1
Joined: Wed Feb 15, 2023 1:37 am
Contact:

Re: [ZScript] Dynamic Attack Speed

Post by Rey65 »

Hello,

It's good that you're looking to improve your dynamic speed adjustment for player attacks. ZScript is a great tool for this, and your approach of using an inventory item to control the frame rate of the player's equipped weapon seems like a good approach. Another possibility would be to use machine learning algorithms in an AI to optimize the speed adjustment based on the player's behavior or other relevant factors. This could potentially lead to more accurate and efficient speed adjustments without the need for manual adjustments. As for your specific code, I'm not familiar with how it might conflict with A_SetTics, but it's always a good idea to thoroughly test your code and look for any unexpected interactions.

Overall, it sounds like you're on the right track, and I hope this helps !
User avatar
stainedofmind
Posts: 213
Joined: Sun Sep 01, 2019 10:59 am

Re: [ZScript] Dynamic Attack Speed

Post by stainedofmind »

You might want to take a look at how ToxicFrog implemented the Rapid Fire powerup for Gun Bonsai. If I'm not mistaken, it will do what you're looking for.
Post Reply

Return to “Scripting”