OnPlayerUpdate: Difference between revisions

From NPC for VCMP 0.4 Servers
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 6: Line 6:
retvals=This function does not return any specific values.|
retvals=This function does not return any specific values.|
example=<source lang="lua">
example=<source lang="lua">
function OnPlayerUpdate( playerId, updateType )
//The following is taken from an npcscript. NPC cries "Oh no", when attacked with a 'Melee weapon'
function OnPlayerUpdate( playerid, updateType )
{
{
if(updateType == PLAYERUPDATE_NORMAL || updateType == PLAYERUPDATE_AIMING)
{
if( (GetPlayerKeys(playerid) & 576)== 576 ) //Fire key is pressed.
{
local mypos = GetMyPos();
local plrangle = GetPlayerFacingAngle( playerid );
local plrpos = GetPlayerPos( playerid );
local _angle = atan2( -(mypos.x - plrpos.x ), mypos.y - plrpos.y );
if( _angle < 0 && plrangle > 0 )_angle+=2*PI;
else if( plrangle <0 && _angle > 0 )_angle-=2*PI;
if( abs( _angle - plrangle ) < PI/2 ) //take PI/2. 0 means straight to npc
{
//player is facing npc
local weapon = GetPlayerArmedWeapon( playerid );
if( weapon >=0 && weapon <= 11 )
{
local range = 5 ;
if( GetDistanceFromMeToPoint( plrpos ) <= range )
{
SendChat( "Oh! no. Stop" );
}
}
}
}
}
}
}
</source>
</source>
}}
}}

Latest revision as of 18:05, 7 December 2022


Description:
This function is called when the packet containing a players data arrives from the server.


Parameters:

( playerid, updateType)

playerId : The ID of the player
updateType : The updateType


Return Values:
This function does not return any specific values.


Example

//The following is taken from an npcscript. NPC cries "Oh no", when attacked with a 'Melee weapon'
function OnPlayerUpdate( playerid, updateType )
{
	if(updateType == PLAYERUPDATE_NORMAL || updateType == PLAYERUPDATE_AIMING)
	{
		if( (GetPlayerKeys(playerid) & 576)== 576 ) //Fire key is pressed.
		{
			local mypos = GetMyPos();
			local plrangle = GetPlayerFacingAngle( playerid );
			local plrpos = GetPlayerPos( playerid );
			local _angle = atan2( -(mypos.x - plrpos.x ), mypos.y - plrpos.y );
			if( _angle < 0 && plrangle > 0 )_angle+=2*PI;
			else if( plrangle <0 && _angle > 0 )_angle-=2*PI;
			if( abs( _angle - plrangle ) < PI/2 ) //take PI/2. 0 means straight to npc
			{
				//player is facing npc
				local weapon = GetPlayerArmedWeapon( playerid );
				if( weapon >=0 && weapon <= 11 )
				{
					local range = 5 ;
					if( GetDistanceFromMeToPoint( plrpos ) <= range )
					{
						SendChat( "Oh! no. Stop" );
					}
				}
			}
		}		
	}
}