Raised This Month: $12 Target: $400
 3% 

Solved Check if player is within entity range.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
cheatscom
Junior Member
Join Date: Oct 2013
Location: I forget
Old 05-30-2020 , 07:58   Check if player is within entity range.
Reply With Quote #1

Hello,

I've been trying to create a script that will check if the client is within range of a specific entity, specifically a bomb plant zone within a map.

If they get within range of this entity I would like to be able to perform certain actions, but I'm having some trouble getting it to work. Essentially I want to compare [Player Position] with [Entity Position], where the entity is static in the map and player position will be dynamically updating.

Code:
decl Float:bombpos[3], Float:clientpos[3];
	
  new BombZone = CreateEntityByName("ddd_bomb_plant_zone");
  
  GetClientAbsOrigin(client, clientpos);
  GetEntPropVector(BombZone, Prop_Send, "ddd_bomb_plant_zone", bombpos);
		
    if(GetVectorDistance(clientpos, bombpos) < 100)
	 {
	   //Let me know that you're within range of this entity.
       PrintToChat(client, "In bomb zone");
     }
Notes:
1. "ddd_bomb_plant_zone" is an entity that already exists in the map.
2. There are multiple "ddd_bomb_plant_zone"(s) if that makes any difference.

What I'm currently doing is probably horribly wrong (please forgive me, this is all quite new stuff), any examples of how to add/change this and what I'm doing incorrectly would be much appreciated.

Thank you in advance for your time.

Last edited by cheatscom; 05-31-2020 at 05:43.
cheatscom is offline
Tilex
Junior Member
Join Date: May 2020
Old 05-30-2020 , 09:31   Re: Check if player is within entity range.
Reply With Quote #2

Hey there,

I haven't worked with props before, but I think you shouldn't be creating a prop with a name, if you are looking for a prop that already exists. Not sure, how the function deals with name ambiquity. Will it pick the closest prop with that name on the map? Or just the first it finds in memory? That would make a difference for sure.

Let's see where I can actually help here..

I can't see what calls your code here, so I'd suggest the following. You may want to trigger the code execution on player movement, which is caught by
PHP Code:
public Action OnPlayerRunCmd(int clientint &buttonsint &impulsefloat vel[3], float angles[3], int &weapon)
{
        
//your code here
        //or: to check movement, we could go further with this:
        
if(buttons & (IN_MOVELEFT IN_MOVERIGHT IN_FORWARD IN_BACK))
        {
             
//code, when player presses movement keys
        
}
    return 
Plugin_Continue;

this may require the following defines somewhere at the top of your script. See: http://docs.sourcemod.net/api/index....d=file&id=47&)
PHP Code:
#define IN_ATTACK (1 << 0)
#define IN_JUMP (1 << 1)
#define IN_DUCK (1 << 2)
#define IN_FORWARD (1 << 3)
#define IN_BACK (1 << 4)
#define IN_USE (1 << 5)
#define IN_CANCEL (1 << 6)
#define IN_LEFT (1 << 7)
#define IN_RIGHT (1 << 8)
#define IN_MOVELEFT (1 << 9)
#define IN_MOVERIGHT (1 << 10) 
Be aware, that this code gets executed several times a second. Maybe you only want to execute it, when a player enters or leaves the area. In that case you probably want to use boolean flags.

Globally define
PHP Code:
bool g_playerInZone[MAXPLAYERS+1
then:
PHP Code:
if(GetVectorDistance(clientposbombpos) < 100)
{
    if(!
g_playerInZone[client])
    {
        
//do something once, when the player enters the zone
        
g_playerInZone[client] = true;
        
PrintToChat(client"Player entered bombzone");
    }
    
//do something very often while a player is in the zone
    //...
}
else
{
    if(
g_playerInZone[client])
    {
        
g_playerInZone[client] = false;
        
PrintToChat(client"Player left bombzone");
    }

Tilex is offline
cheatscom
Junior Member
Join Date: Oct 2013
Location: I forget
Old 05-30-2020 , 10:24   Re: Check if player is within entity range.
Reply With Quote #3

Thank you for the information!

I probably should have posted the entire code rather than a snippet... adding it below with your additions + comments:

PHP Code:
#pragma semicolon 1

#include <sourcemod>
#include <sdktools>

#define IN_ATTACK2 (1 << 11)

public Action OnPlayerRunCmd(int clientint &buttonsint &impulsefloat vel[3], float angles[3], int &weapon)
{
    
bool g_playerInZone[MAXPLAYERS+1]; 
    new class = 
GetEntProp(clientProp_Send"m_iPlayerClass");
    
    
int BombZone FindEntityByClassname(BombZone"ddd_bomb_plant_zone");
    
  
decl Float:bombpos[3], Float:clientpos[3];
  
  
GetClientAbsOrigin(clientclientpos);
  
GetEntPropVector(BombZoneProp_Send"m_vecOrigin"bombpos);
  
  if(
GetVectorDistance(clientposbombpos) < 500 && (buttons IN_ATTACK2) == IN_ATTACK2 && class == 8//If client is less than 500 units from 
  
{
        
//do something once, when the player enters the zone
        
PrintToChat(client"Player entered bombzone"); //Message to test if distance check is working
       //do something very often while a player is in the zone
       //...
  
}
  else if(
GetVectorDistance(clientposbombpos) > 500 && (buttons IN_ATTACK2) == IN_ATTACK2 && class == 8//If client is less than 500 units from 
     
{
        
PrintToChat(client"Player left bombzone"); //Another message to test if distance check is working
     
}
    
    
// Must return Plugin_Continue to let the changes be processed.
    
return Plugin_Continue;


This does compile and works with 1 "ddd_bomb_plant_zone" on the map, whereby if you get near to it and press mouse2 you get a message saying "Player entered bombzone", 100x better than the "Exception reported" spam I was getting earlier, so cheers.

I'm probably being dumb, but how is 'g_playerInZone' meant to be used? It seems to define but I'm not sure it ever has a value besides 'MAXPLAYERS+1'.

I think now I just need some way of recording all the "ddd_bomb_plant_zone"(s) on map load, storing their vector locations and then using all of them as a distance comparison to the client somehow. Or maybe there's a simpler way, I will do some more testing.

Last edited by cheatscom; 05-30-2020 at 12:03.
cheatscom is offline
andi67
Veteran Member
Join Date: Mar 2007
Location: Somewhere near you!!!
Old 05-30-2020 , 15:02   Re: Check if player is within entity range.
Reply With Quote #4

Well you could also loop through the entitys get the bombzones and attach a trigger_multiple and than check if a player is entering the zone.....
__________________
Waiting for HL3,Day of Defeat3 ,but will it ever come? So I'm gonna play COD WW2.>>>>SM_SKINCHOOSER<<<<
>>You need Models for DODS/CSS/CSGO , than click here!!!<<
andi67 is offline
cheatscom
Junior Member
Join Date: Oct 2013
Location: I forget
Old 05-31-2020 , 05:42   Re: Check if player is within entity range.
Reply With Quote #5

Quote:
Originally Posted by andi67 View Post
Well you could also loop through the entitys get the bombzones and attach a trigger_multiple and than check if a player is entering the zone.....
Pretty much what I ended up doing.

Using a while((ent = FindEntityByClassname(ent, "ddd_bomb_plant_zone")) on map load to cycle through and hook triggers.

Thanks all for the assistance, much appreciated.
cheatscom is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:49.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode