Thread: Weapon not working

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    77

    Weapon not working

    Hello guys, please help. Why weapon script is not working?
    Code:
    enum PILA_e{
    	PILA_IDLE = 0,
    	PILA_DRAW,
    	PILA_DRAW_EMPTY,
    	PILA_ATTACK_S,
    	PILA_ATTACK_L,
    	PILA_ATTACK_E,
    	PILA_RELOAD,
    	PILA_SLASH1,
    	PILA_SLASH2,
    	PILA_SLASH3,
    	PILA_SLASH4,
    	PILA_EMPTY
    };
    
    
    class weapon_pila : ScriptBasePlayerWeaponEntity
    {
    	bool m_IsPullingBack = false;
    	int m_iSwing;
    	TraceResult m_trHit;
    	
    	void Spawn()
    	{
    		Precache();
    		g_EntityFuncs.SetModel( self, self.GetW_Model( "models/metalarena/weapons/w_pila.mdl") );
    		self.m_iClip			= -1;
    		self.m_flCustomDmg		= self.pev.dmg;
    
    
    		self.FallInit();// get ready to fall down.
    	}
    
    
    	void Precache()
    	{
    		self.PrecacheCustomModels();
    
    
    		g_Game.PrecacheModel( "models/metalarena/weapons/v_pila.mdl" );
    		g_Game.PrecacheModel( "models/metalarena/weapons/w_pila.mdl" );
    		g_Game.PrecacheModel( "models/metalarena/weapons/p_pila.mdl" );
    
    
    		g_SoundSystem.PrecacheSound( "weapons/chainsaw_draw.wav" );
    		g_SoundSystem.PrecacheSound( "weapons/chainsaw_hit1.wav" );
    		g_SoundSystem.PrecacheSound( "weapons/chainsaw_idle.wav" );
    		g_SoundSystem.PrecacheSound( "weapons/chainsaw_slash1.wav" );
    	}
    
    
    	bool GetItemInfo( ItemInfo& out info )
    	{
    		info.iMaxAmmo1		= -1;
    		info.iMaxAmmo2		= -1;
    		info.iMaxClip		= WEAPON_NOCLIP;
    		info.iSlot			= 0;
    		info.iPosition		= 5;
    		info.iWeight		= 0;
    		return true;
    	}
    
    
    	bool Deploy()
    	{
    		bool bResult;
    		{
    			bResult = self.DefaultDeploy ( self.GetV_Model( "models/metalarena/weapons/v_pila.mdl" ), self.GetP_Model( "models/metalarena/weapons/p_pila.mdl" ), PILA_DRAW, "wrench" );
    
    
    			m_pPlayer.m_szAnimExtension = "crowbar";
    
    
    			float deployTime = 1.15;
    			self.m_flTimeWeaponIdle = self.m_flNextPrimaryAttack = self.m_flNextSecondaryAttack = g_Engine.time + deployTime;
    			return bResult;
    		}
    	}
    
    
    	void PrimaryAttack()
    	{
    		if( !m_IsPullingBack )
    		{
    			// We don't want the player to break/stop the animation or sequence.
    			m_IsPullingBack = true;
    			
    			// We are pulling back our spear
    			switch ( g_PlayerFuncs.SharedRandomLong( m_pPlayer.random_seed, 0, 1 ) )
    			{
    				case 0: self.SendWeaponAnim( PILA_SLASH1, 0, 0 ); break;
    				case 1: self.SendWeaponAnim( PILA_SLASH2, 0, 0 ); break;
    			}
    
    
    			m_pPlayer.m_szAnimExtension = "wrench";
    			
    			// Lets wait for the 'heavy smack'
    			SetThink( ThinkFunction( this.DoHeavyAttack ) );
    			self.pev.nextthink = g_Engine.time + 0.0;
    
    
    			g_SoundSystem.EmitSound( m_pPlayer.edict(), CHAN_WEAPON, "weapons/chainsaw_slash1.wav", 1, ATTN_NORM );
    		}
    	}
    	
    	void DoHeavyAttack()
    	{
    		HeavySmack();
    	}
    	
    	void Smack()
    	{
    		g_WeaponFuncs.DecalGunshot( m_trHit, BULLET_PLAYER_CROWBAR );
    	}
    	
    	bool HeavySmack()
    	{
    		TraceResult tr;
    		
    		bool fDidHit = false;
    
    
    		Math.MakeVectors( m_pPlayer.pev.v_angle );
    		Vector vecSrc	= m_pPlayer.GetGunPosition();
    		Vector vecEnd	= vecSrc + g_Engine.v_forward * 64;
    
    
    		g_Utility.TraceLine( vecSrc, vecEnd, dont_ignore_monsters, m_pPlayer.edict(), tr );
    
    
    		if ( tr.flFraction >= 1.0 )
    		{
    			g_Utility.TraceHull( vecSrc, vecEnd, dont_ignore_monsters, head_hull, m_pPlayer.edict(), tr );
    			if ( tr.flFraction < 1.0 )
    			{
    				// Calculate the point of intersection of the line (or hull) and the object we hit
    				// This is and approximation of the "best" intersection
    				CBaseEntity@ pHit = g_EntityFuncs.Instance( tr.pHit );
    				if ( pHit is null || pHit.IsBSPModel() == true )
    					g_Utility.FindHullIntersection( vecSrc, tr, tr, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX, m_pPlayer.edict() );
    				vecEnd = tr.vecEndPos;	// This is the point on the actual surface (the hull could have hit space)
    			}
    		}
    
    
    		if ( tr.flFraction >= 1.0 )
    		{
    			self.m_flNextPrimaryAttack = g_Engine.time + 0.5;
    			// play wiff or swish sound
    			// player "shoot" animation
    			m_pPlayer.SetAnimation( PLAYER_ATTACK1 );
    		}
    		else
    		{
    			// hit
    			fDidHit = true;
    			
    			// The entity we hit
    			CBaseEntity@ pEntity = g_EntityFuncs.Instance( tr.pHit );
    
    
    			// player "shoot" animation
    			m_pPlayer.SetAnimation( PLAYER_ATTACK1 ); 
    
    
    			// AdamR: Custom damage option
    			float flDamage = 125;
    			if ( self.m_flCustomDmg > 0 )
    				flDamage = self.m_flCustomDmg;
    			// AdamR: End
    
    
    			g_WeaponFuncs.ClearMultiDamage();
    			if ( self.m_flNextPrimaryAttack + 1 < g_Engine.time )
    			{
    				// first swing does full damage
    				pEntity.TraceAttack( m_pPlayer.pev, flDamage, g_Engine.v_forward, tr, DMG_CLUB );  
    			}
    			else
    			{
    				// subsequent swings do 50% (Changed -Sniper) (Half)
    				pEntity.TraceAttack( m_pPlayer.pev, flDamage * 0.5, g_Engine.v_forward, tr, DMG_CLUB );  
    			}	
    			g_WeaponFuncs.ApplyMultiDamage( m_pPlayer.pev, m_pPlayer.pev );
    
    
    			//m_flNextPrimaryAttack = gpGlobals->time + 0.5;
    
    
    			// play thwack, smack, or dong sound
    			float flVol = 1.0;
    			bool fHitWorld = true;
    
    
    			if( pEntity !is null )
    			{
    				self.m_flNextPrimaryAttack = g_Engine.time + 0.5;
    				
    				/*
    					TODO: Is entity electrecuted?
    						If true
    							Play 'Electrecuted' animation and sound
    							Player is already getting hurt from the trigger
    						If false
    							Stabby stab
    				*/
    
    
    				if( pEntity.Classify() != CLASS_NONE && pEntity.Classify() != CLASS_MACHINE && pEntity.BloodColor() != DONT_BLEED )
    				{
    	// aone
    					if( pEntity.IsPlayer() == true )		// lets pull them
    					{
    						pEntity.pev.velocity = pEntity.pev.velocity + ( self.pev.origin - pEntity.pev.origin ).Normalize() * 120;
    					}
    	// end aone
    					// play thwack or smack sound
    					g_SoundSystem.EmitSound( m_pPlayer.edict(), CHAN_WEAPON, "weapons/chainsaw_hit1.wav", 1, ATTN_NORM );
    					m_pPlayer.m_iWeaponVolume = 128; 
    					
    					if( pEntity.IsAlive() == false )
    					{
    						SetThink( ThinkFunction( this.NoPulling ) );
    						self.pev.nextthink = g_Engine.time + 0.5;
    						return true;
    					}
    					else
    						flVol = 0.1;
    
    
    					fHitWorld = false;
    				}
    			}
    
    
    			// play texture hit sound
    			// UNDONE: Calculate the correct point of intersection when we hit with the hull instead of the line
    
    
    			if( fHitWorld == true )
    			{
    				float fvolbar = g_SoundSystem.PlayHitSound( tr, vecSrc, vecSrc + ( vecEnd - vecSrc ) * 2, BULLET_PLAYER_CUSTOMDAMAGE );
    				
    				self.m_flNextPrimaryAttack = g_Engine.time + 0.5;
    				
    				// override the volume here, cause we don't play texture sounds in multiplayer, 
    				// and fvolbar is going to be 0 from the above call.
    
    
    				fvolbar = 1;
    
    
    				// also play crowbar strike
    				g_SoundSystem.EmitSoundDyn( m_pPlayer.edict(), CHAN_WEAPON, "weapons/chainsaw_hit1.wav", fvolbar, ATTN_NORM, 0, 98 + Math.RandomLong( 0, 3 ) );
    			}
    
    
    			m_pPlayer.m_iWeaponVolume = int( flVol * 512 ); 
    		}
    		
    		// Lets wait until we can attack again
    		SetThink( ThinkFunction( this.NoPulling ) );
    		self.pev.nextthink = g_Engine.time + 0.5;
    		
    		return fDidHit;
    	}
    
    
    	void NoPulling()
    	{
    		// We are no longer pulling back
    		m_IsPullingBack = false;
    	}
    
    
    }
    
    
    string GetWeaponName_PILA()
    {
    	return "weapon_pila";
    }
    
    
    void RegisterWeapon_PILA()
    {
    	g_CustomEntityFuncs.RegisterCustomEntity( "weapon_pila", GetWeaponName_PILA() );
    	g_ItemRegistry.RegisterWeapon( GetWeaponName_PILA(), "wpn" );
    }

  2. #2
    func_vehicle enthusiaist w00tguy123's Avatar
    Join Date
    Dec 2006
    Location
    U.S. West
    Posts
    1,597

    Re: Weapon not working

    The API changed in 5.11 so m_pPlayer no longer exists.

    To get the player entity you have to do something like this now:

    Code:
    cast<CBasePlayer@>(self.m_hPlayer.GetEntity())
    Better to put that in a function or member variable instead of just doing a Find&Replace.
    Love,
    w00tguy

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    77

    Re: Weapon not working

    Quote Originally Posted by w00tguy123 View Post
    The API changed in 5.11 so m_pPlayer no longer exists.

    To get the player entity you have to do something like this now:

    Code:
    cast<CBasePlayer@>(self.m_hPlayer.GetEntity())
    Better to put that in a function or member variable instead of just doing a Find&Replace.
    Could you help me? Log Error: ERROR: "CBasePlayer::m_rgAmmo: index is invalid!"

  4. #4
    200 MB Angelscript log file KernCore's Avatar
    Join Date
    Apr 2016
    Location
    Brazil
    Posts
    269

    Re: Weapon not working

    You can declare a pointer m_pPlayer in the weapon's class.
    Code:
    private CBasePlayer@ m_pPlayer = null;
    later on you can assign the pointer to the player itself in bool AddToPlayer( CBasePlayer@ pPlayer )

    Sample code from CoF's Axe:
    Code:
    bool AddToPlayer( CBasePlayer@ pPlayer )
    	{
    		if( BaseClass.AddToPlayer( pPlayer ) )
    		{
    			@m_pPlayer = pPlayer;
    			NetworkMessage cofaxe( MSG_ONE, NetworkMessages::WeapPickup, pPlayer.edict() );
    				cofaxe.WriteLong( g_ItemRegistry.GetIdForName("weapon_cofaxe") );
    			cofaxe.End();
    			return true;
    		}
    		return false;
    	}
    Quote Originally Posted by join10 View Post
    Could you help me? Log Error: ERROR: "CBasePlayer::m_rgAmmo: index is invalid!"
    m_pPlayer.m_rgAmmo( self.m_iPrimaryAmmoType, m_pPlayer.m_rgAmmo( self.m_iPrimaryAmmoType ) - 1 )
    Use this if you're trying to make calculations with the primary ammo, m_iSecondaryAmmoType should work just fine with the secondary ammo.

  5. #5
    Registered User
    Join Date
    Apr 2016
    Posts
    77

    Re: Weapon not working

    Quote Originally Posted by KernCore View Post
    You can declare a pointer m_pPlayer in the weapon's class.
    Code:
    private CBasePlayer@ m_pPlayer = null;
    later on you can assign the pointer to the player itself in bool AddToPlayer( CBasePlayer@ pPlayer )

    Sample code from CoF's Axe:
    Code:
    bool AddToPlayer( CBasePlayer@ pPlayer )
    	{
    		if( BaseClass.AddToPlayer( pPlayer ) )
    		{
    			@m_pPlayer = pPlayer;
    			NetworkMessage cofaxe( MSG_ONE, NetworkMessages::WeapPickup, pPlayer.edict() );
    				cofaxe.WriteLong( g_ItemRegistry.GetIdForName("weapon_cofaxe") );
    			cofaxe.End();
    			return true;
    		}
    		return false;
    	}


    m_pPlayer.m_rgAmmo( self.m_iPrimaryAmmoType, m_pPlayer.m_rgAmmo( self.m_iPrimaryAmmoType ) - 1 )
    Use this if you're trying to make calculations with the primary ammo, m_iSecondaryAmmoType should work just fine with the secondary ammo.
    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •