How to find a weapon's ammo type?

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
Baysha
Posts: 20
Joined: Sat Feb 24, 2018 5:57 pm

How to find a weapon's ammo type?

Post by Baysha »

Hi. I'm trying to create a dynamic ammo spawner that spawns the ammo type of the closest weapon. I got the code for looking through all the weapons in the map and picking the closest one working, but whenever I try to find the weapon's ammo type, I get the error "Left side of Weapon is not a struct or class." The code works if I try to spawn the weapon directly. How do I fix this?

This DOES NOT work

Code: Select all

Class Level1Ammo : Actor
{
	States
	{
	Spawn:
	TNT1 A 0 NODELAY
	{
		ThinkerIterator WeaponFinder = ThinkerIterator.Create("DoomWeapon");
		Actor mo;
		Actor ClosestWeapon;
		Array <actor> MapWeapons;
		
		while (mo = DoomWeapon(WeaponFinder.Next()))
		{
			if(mo.GetClass().Weapon.Ammotype)
			{
				MapWeapons.Push(mo);
			}
		}
		
		if(MapWeapons.size()>0)
		{
			For (int i=0; i < MapWeapons.size(); i++)
			{
				let CurrentWeapon = MapWeapons[i];
				
				if (ClosestWeapon && Distance2D(CurrentWeapon)<Distance2D(ClosestWeapon))
				{
				ClosestWeapon=CurrentWeapon;
				}
				else if (!ClosestWeapon)
				{
				ClosestWeapon=CurrentWeapon;
				}
			}
		}
				
		if(ClosestWeapon)
		{
		A_SpawnItemEX(ClosestWeapon.GetClass().Weapon.AmmoType.GetClass(),0,0,0,0,0,0,0,SXF_NOCHECKPOSITION);
		}
		else
		{
		A_SpawnItemEX("Clip",0,0,0,0,0,0,0,SXF_NOCHECKPOSITION);
		}
	}
	Stop;
	}
}
However, this code, which spawns the weapon class itself, DOES work

Code: Select all

Class Level1Ammo : Actor
{
	States
	{
	Spawn:
	TNT1 A 0 NODELAY
	{
		ThinkerIterator WeaponFinder = ThinkerIterator.Create("DoomWeapon");
		Actor mo;
		Actor ClosestWeapon;
		Array <actor> MapWeapons;
		
		while (mo = DoomWeapon(WeaponFinder.Next()))
		{
			MapWeapons.Push(mo);
		}
		
		if(MapWeapons.size()>0)
		{
			For (int i=0; i < MapWeapons.size(); i++)
			{
				let CurrentWeapon = MapWeapons[i];
				
				if (ClosestWeapon && Distance2D(CurrentWeapon)<Distance2D(ClosestWeapon))
				{
				ClosestWeapon=CurrentWeapon;
				}
				else if (!ClosestWeapon)
				{
				ClosestWeapon=CurrentWeapon;
				}
			}
		}
				
		if(ClosestWeapon)
		{
		A_SpawnItemEX(ClosestWeapon.GetClass(),0,0,0,0,0,0,0,SXF_NOCHECKPOSITION);
		}
		else
		{
		A_SpawnItemEX("Clip",0,0,0,0,0,0,0,SXF_NOCHECKPOSITION);
		}
	}
	Stop;
	}
}
Post Reply

Return to “Scripting”