Big problem with "if" in Zscript

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
ZoomSlayer2
Posts: 10
Joined: Wed Oct 05, 2022 6:34 am

Big problem with "if" in Zscript

Post by ZoomSlayer2 »

Ok so i want to create a firemode switch for my gun, only using zscript, not "A_JumpIf" or stuff like this, and in the "AltFire" part, the code simply ignore the condition and keep the same variable value, even if it's not the one that should be correct, and so, he always go to "Burst firemode":

Code: Select all

Class MicroUzi : Weapon 
{

	int firemode;

  Default
  {
  obituary "%o was peppered by %k's micro-uzi.";
  radius 20;
  height 16;
  attacksound "MicroUzi/Fire";
  inventory.pickupmessage "You got the micro-uzi!";
  weapon.kickback 15;
  weapon.ammotype "LA";
  weapon.ammouse 1;
  weapon.ammogive 40;
  weapon.ammotype2 "LA";
  weapon.ammouse2 1;
  weapon.ammogive2 0;
  Decal "BulletChip";
  Scale 0.75;
  }
  states
  {
  Ready:
    MUZI A 1 
    {
		A_Raise(12);
		invoker.firemode=0;
    }
    loop;
  Deselect: 
    MUZI A 1 A_Lower(20);
    loop;
  Select:
    MUZI A 1 A_Raise(12);
    loop;
  Fire:
    MUZI A 0
	{
		if (invoker.firemode=1)
			return ResolveState("BurstFire");
		return ResolveState(null);
	}
    MUZI B 2 A_FireBullets (6, 4, -1, 16, "BulletPuff");
	MUZI A 0 A_Recoil(0.4);	
    MUZI A 0 A_GunFlash;
    MUZI A 1 A_SetPitch(pitch-0.25);
    MUZI B 1 A_ReFire;
    Goto Ready;
  BurstFire:
    MUZI A 0
    {
		if (invoker.firemode=0)
			return ResolveState("Fire");
		return ResolveState(null);
	}
	MUZI B 2 A_FireBullets (6, 4, -1, 16, "BulletPuff");
	MUZI A 0 A_Recoil(0.4);	
    MUZI A 0 A_GunFlash;
    MUZI A 1 A_SetPitch(pitch-0.25);
	MUZI B 2 A_FireBullets (6, 4, -1, 16, "BulletPuff");
	MUZI A 0 A_Recoil(0.4);	
    MUZI A 0 A_GunFlash;
    MUZI A 1 A_SetPitch(pitch-0.25);
	MUZI B 2 A_FireBullets (6, 4, -1, 16, "BulletPuff");
	MUZI A 0 A_Recoil(0.4);	
    MUZI A 0 A_GunFlash;
    MUZI A 1 A_SetPitch(pitch-0.25);
	MUZI A 9;
    MUZI B 1 A_ReFire;
    Goto Ready;
  AltFire:
	MUZI A 0
	{
		if (invoker.firemode=0)
		    invoker.firemode=1;
			Console.Printf("Burst firemode");
			return ResolveState("Ready");
		invoker.firemode=0;
		Console.Printf("Auto firemode");
		return ResolveState("Ready");
	}
	Goto Ready;
  Flash:
    TNT1 A 2 bright A_Light1;
    TNT1 A 2 bright A_Light2;
    TNT1 A 0 bright A_Light0;
    stop;
  Spawn:
    MUZP A -1;
    stop;
  }
}
Can someone tell me where is my error and how i could resolve that ?
Jarewill
 
 
Posts: 1855
Joined: Sun Jul 21, 2019 8:54 am

Re: Big problem with "if" in Zscript

Post by Jarewill »

Your if checks uses the assignment operator (=) instead of the comparison operator (==).
Also you should use {brackets} for checks, otherwise only the first function will be included in the it.

Here's an example with your Altfire state:
Spoiler:
Your Fire states will also need to be updated as they also have the same error in their if checks.

Also using A_JumpIf in ZScript is still a valid solution.
ZoomSlayer2
Posts: 10
Joined: Wed Oct 05, 2022 6:34 am

Re: Big problem with "if" in Zscript

Post by ZoomSlayer2 »

OK it works, thank you a lot
ZoomSlayer2
Posts: 10
Joined: Wed Oct 05, 2022 6:34 am

Re: Big problem with "if" in Zscript

Post by ZoomSlayer2 »

But, actually, i have another problem:
The weapon is in autofire mode by default, so the switching is very fast and it's not practical at all.
I know there exist the flag "+WEAPON.NOAUTOFIRE", but it acts for all the gun behaviour.
So, how i could put a "NOAUTOFIRE" (not obligatory a flag) condition only for the altifre ?
Jarewill
 
 
Posts: 1855
Joined: Sun Jul 21, 2019 8:54 am

Re: Big problem with "if" in Zscript

Post by Jarewill »

The way I usually solve that problem is with a dummy state like this:

Code: Select all

	Nope:
		TNT1 A 1 A_WeaponReady(WRF_NOFIRE);
		TNT1 A 0 A_ReFire("Nope");
		TNT1 A 0 A_ClearReFire();
		Goto Ready;
If you make your Altfire state jump to it instead of Ready it will prevent extremely rapid mode switching.
User avatar
Player701
 
 
Posts: 1710
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Big problem with "if" in Zscript

Post by Player701 »

ZoomSlayer2 wrote: Tue Oct 25, 2022 1:53 pm I know there exist the flag "+WEAPON.NOAUTOFIRE", but it acts for all the gun behaviour.
Note that contrary to the popular belief, +WEAPON.NOAUTOFIRE does not prevent the weapon from being fired continuously by holding the trigger. Instead, this flag is used to prevent the weapon from firing as soon as it gets selected, if the trigger button is held. For example, this flag is enabled for the vanilla rocket launcher, so that you don't accidentally blow yourself up if you switch to it without releasing the fire button.
ZDoom Wiki wrote:Does not fire when selected automatically and the fire button is still pressed. Used to prevent dangerous weapons from firing accidentally.
upd: However, NOAUTOFIRE can be used to disable contiunuous firing if either the call to A_ReFire is missing or A_ClearReFire is called before returning to the ready state. But it is more of a side effect and not the original intention of this flag.
Post Reply

Return to “Scripting”