Is there a way to reference AmmoType1/AmmoType2 when using A_TakeInventory/A_GiveInventory?

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!)
milkFiend
Posts: 27
Joined: Tue Nov 21, 2023 10:30 pm
Preferred Pronouns: No Preference
Operating System Version (Optional): Windows 11
Graphics Processor: Intel (Modern GZDoom)

Is there a way to reference AmmoType1/AmmoType2 when using A_TakeInventory/A_GiveInventory?

Post by milkFiend »

To clarify:
My plan is to make a weapon base with predefined state sequences, including a predefined reload state.
I want to be able to inherit from this weapon base as to not have to constantly copy/paste identical states.
In the reload state, rather than having to specify the ammo itself, I'd rather reference the AmmoType1/AmmoType2 properties.
My initial guess was to do "A_TakeInventory(Weapon.AmmoType2, 1)" but that doesn't seem to work.

Would I have to create a function for this to work the way I'd like it to?

At first I expected to be able to use "A_TakeInventory(invoker.AmmoType1, 1);" but it seems I'm not able to directly read the value that I want to.
User avatar
Player701
 
 
Posts: 1693
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: Is there a way to reference AmmoType1/AmmoType2 when using A_TakeInventory/A_GiveInventory?

Post by Player701 »

milkFiend wrote: Thu Nov 21, 2024 9:42 pmA_TakeInventory(invoker.AmmoType1, 1)
This should suffice, as long as you're using ZScript and not DECORATE. If it doesn't work for you, could you please provide a small runnable example to replicate the problem?
milkFiend
Posts: 27
Joined: Tue Nov 21, 2023 10:30 pm
Preferred Pronouns: No Preference
Operating System Version (Optional): Windows 11
Graphics Processor: Intel (Modern GZDoom)

Re: Is there a way to reference AmmoType1/AmmoType2 when using A_TakeInventory/A_GiveInventory?

Post by milkFiend »

Player701 wrote: Fri Nov 22, 2024 12:00 am
milkFiend wrote: Thu Nov 21, 2024 9:42 pmA_TakeInventory(invoker.AmmoType1, 1)
This should suffice, as long as you're using ZScript and not DECORATE. If it doesn't work for you, could you please provide a small runnable example to replicate the problem?
Of course, the full script I was using is as follows:

Code: Select all

Class PistolVM : Weapon
{
	protected name modelName, AmmoClip, AmmoReserve;
	property modelFile : modelName;	//used to append model files in subsequent weapons that inherit from this one
	property AmmoClip : AmmoClip;		//used to append AmmoType1
	property AmmoReserve : AmmoReserve;	//used to append AmmoType2
	
	Default
	{
		PistolVM.modelFile 'vm_arms.iqm';
		Weapon.SlotNumber 2;
		+WEAPON.AMMO_OPTIONAL
		+DECOUPLEDANIMATIONS
		+WEAPON.CHEATNOTWEAPON
	}
	
	States
	{
		Spawn:
			MSVN A 0 NoDelay 
				{
					A_ChangeModel("", 1, "models/viewmodels", invoker.modelName);
					A_SetAnimation("VMPistol_Idle", 25);
				}
			MSVN A 1;
			Loop;

		Ready:
			MSVN A 0 
				{
					A_SetAnimation("VMPistol_Idle", 25, flags:SAF_LOOP);
				}
			MSVN A 50 A_WeaponReady;
			Loop;

		Select:
			MSVN A 0 
				{
					A_ChangeModel("", 1, "models/viewmodels", invoker.modelName);
					A_SetAnimation("VMPistol_Idle", 25);
				}
			MSVN A 1 A_Raise;
			Loop;

		Deselect:
			MSVN A 0 
				{
					A_SetAnimation("VMPistol_Idle", 25);
				}
			MSVN A 1 A_Lower;
			Loop;

		Fire:
			TNT1 A 0 A_JumpIfNoAmmo("Reload");
			MSVN A 1 A_SetAnimation("VMPistol_Fire", 25);
			MSVN A 19
				{
					A_FireProjectile("BulletProjectile", angle:frandom(-1,2.5), spawnofs_xy:12, spawnheight:2, pitch:frandom(-1,1));
					A_StartSound("GUNSHOT", CHAN_WEAPON);
				}
			MSVN A 0;
			Goto Ready;

		Reload:	//the problem starts here, possibly in IsReloading aswell
			TNT1 A 0 A_JumpIfInventory(invoker.AmmoType1, 8, 2);
			TNT1 A 0 A_JumpIfInventory(invoker.AmmoType2, 1, "IsReloading");
			GoTo Ready;

		IsReloading:
			MSVN A 45 A_SetAnimation("VMPistol_Reload", 25);
			MSVN A 25 
				{
					A_TakeInventory(invoker.AmmoType2, 1);
					A_SetInventory(invoker.AmmoType1, 8);
				}	
			MSVN A 0;
			Goto Ready;
	}
}

//Pistol Magazine class, unimportant, i think
Class PistolMag : Ammo
{
	Default
	{
		Inventory.Amount 0;
		Inventory.MaxAmount 0;
		+INVENTORY.IGNORESKILL
	}
	
	States
	{
		Spawn:
			MSVN A 1;
			Loop;
	}
}
Just in case, I'll include the IPK3 if you'd like to look at it in the Google Drive link. (sorry, i wish i knew how to link files)
https://drive.google.com/file/d/1K1skT ... p=sharing

Honestly, I was fully prepared to create an action function to use in the reloading sequence but that hasn't proved much more successful for me.

Also: please ignore "AmmoClip" and "AmmoReserve", they're left over from the action function I tried making
User avatar
Player701
 
 
Posts: 1693
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: Is there a way to reference AmmoType1/AmmoType2 when using A_TakeInventory/A_GiveInventory?

Post by Player701 »

First of all, please note that I cannot debug this because the sprites are missing.

What I can see is that you don't have ammo types defined for your weapon (Weapon.AmmoType1 and Weapon.AmmoType2 respectively). Since you haven't done that, the corresponding fields are set to null, and of course A_TakeInventory et al. aren't going to work... because there is nothing to take or give.

If you do have a weapon where those properties are correctly set, and it still isn't working right - please provide the code, preferably one I can run out of the box. Your link doesn't work: it says I don't have access to the file.

P.S. You might also consider using this if you want reloadable weapons.
milkFiend
Posts: 27
Joined: Tue Nov 21, 2023 10:30 pm
Preferred Pronouns: No Preference
Operating System Version (Optional): Windows 11
Graphics Processor: Intel (Modern GZDoom)

Re: Is there a way to reference AmmoType1/AmmoType2 when using A_TakeInventory/A_GiveInventory?

Post by milkFiend »

Player701 wrote: Fri Nov 22, 2024 12:59 am First of all, please note that I cannot debug this because the sprites are missing.

What I can see is that you don't have ammo types defined for your weapon (Weapon.AmmoType1 and Weapon.AmmoType2 respectively). Since you haven't done that, the corresponding fields are set to null, and of course A_TakeInventory et al. aren't going to work... because there is nothing to take or give.

If you do have a weapon where those properties are correctly set, and it still isn't working right - please provide the code, preferably one I can run out of the box. Your link doesn't work: it says I don't have access to the file.

P.S. You might also consider using this if you want reloadable weapons.
Thank you, I'll look into the reloading system provided and see if I can save myself a headache lol

Also, my apologies for the link, I forgot to open access. This should work now:
https://drive.google.com/file/d/1K1skT ... p=sharing

In the IPK3, the script in question is under "scripts/viewmodels/vm_pistolGuide.txt"
I'm trying to use the "PistolVM" weapon as a base for other weapons to use inheritance

Again, sorry for making this one a little harder than it needed to be, and I appreciate the guidance.
User avatar
Player701
 
 
Posts: 1693
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support

Re: Is there a way to reference AmmoType1/AmmoType2 when using A_TakeInventory/A_GiveInventory?

Post by Player701 »

Thank you for fixing the link.

I can see that you're calling DepleteAmmo(true, true), the first argument in this case means to use alternate ammo. But that won't work as your weapon does not have an alt-fire state, and furthermore, your AmmoUse2 is set to 0, so it wouldn't have had any effect anyway.

You should use A_TakeInventory(invoker.AmmoType2, 1) instead. I just tried this myself, and it worked. Or, you can simply subtract the amount directly, e.g. invoker.Ammo2.Amount--.
milkFiend
Posts: 27
Joined: Tue Nov 21, 2023 10:30 pm
Preferred Pronouns: No Preference
Operating System Version (Optional): Windows 11
Graphics Processor: Intel (Modern GZDoom)

Re: Is there a way to reference AmmoType1/AmmoType2 when using A_TakeInventory/A_GiveInventory?

Post by milkFiend »

Player701 wrote: Fri Nov 22, 2024 3:33 am Thank you for fixing the link.

I can see that you're calling DepleteAmmo(true, true), the first argument in this case means to use alternate ammo. But that won't work as your weapon does not have an alt-fire state, and furthermore, your AmmoUse2 is set to 0, so it wouldn't have had any effect anyway.

You should use A_TakeInventory(invoker.AmmoType2, 1) instead. I just tried this myself, and it worked. Or, you can simply subtract the amount directly, e.g. invoker.Ammo2.Amount--.
Honestly, as well as the "clip" was working, I couldn't get past the infinite "reserve ammo" issue, so I ended up going with RRWM reloading system.

The only thing is: gzdoom keeps telling me that I "Cannot use non-action function here" wherever I use A_ReloadableWeaponReady or A_ReloadFullClip in a state
I've tried inserting those functions into an anonymous function, but the error becomes "Function is incompatible with current context"

does it matter that I'm trying to call these functions in zscript and not a decorate script?
milkFiend
Posts: 27
Joined: Tue Nov 21, 2023 10:30 pm
Preferred Pronouns: No Preference
Operating System Version (Optional): Windows 11
Graphics Processor: Intel (Modern GZDoom)

Re: Is there a way to reference AmmoType1/AmmoType2 when using A_TakeInventory/A_GiveInventory?

Post by milkFiend »

my bad, just found out about the states(weapon) solution in another thread

Return to “Scripting”