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

Solved How to Find Nearest Object & Entity (Help please)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 09-06-2019 , 10:36   How to Find Nearest Object & Entity (Help please)
Reply With Quote #1

Hello,
Sorry, My English is Bad

how to find a nearest entity?
please help, i try a make this but not worked

thanks

Code:
public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:vel[3], Float:angles[3])
{
	if(IsValidClient(client))
	{
		if(IsFakeClient(client))
		{
			if(IsPlayerAlive(client))
			{
				int AmmoPack = FindEntityByClassname(AmmoPack, "item_ammopack_medium");
				int Ammo = Entity_GetClosest(client, AmmoPack);
				if(Ammo != -1)
				{
					// CODE
				}
			}
		}
	}
}


stock Entity_GetClosest(int client, int entity)
{
	decl Float:clientOrigin[3], Float:entityOrigin[3];
	
	GetEntPropVector(client, Prop_Data, "m_vecOrigin", clientOrigin);
	GetEntPropVector(entity, Prop_Data, "m_vecOrigin", entityOrigin);
	
	clientOrigin[2] += 50.0;
	
	new Float:near = -1.0;
	new nearestentity = -1;
	
	new Float:distance;
	distance = GetVectorDistance(clientOrigin, entityOrigin);
	
	if (IsClientInGame(client) && IsPlayerAlive(client) && IsValidEntity(entity))
	{
		if(IsValidClient(client))
		{
        	if (distance < near || near == -1.0)
			{
				near = distance;
				nearestentity = entity;
			}
		}
    }
	return nearestentity;
}

Last edited by EfeDursun125; 09-07-2019 at 10:18.
EfeDursun125 is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 09-06-2019 , 15:54   Re: How to Find Nearest Object & Entity (Help please)
Reply With Quote #2

You may be able to use some of the code from the talented Pelepoika. He has a plugin called TF2 Rollermine Spawner that uses a stock similar to what you are doing. His plugin spawns electric balls that follow and hit nearby players.

Link to TF2 Rollermine Spawner: https://forums.alliedmods.net/showthread.php?p=2370316
PC Gamer is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 09-06-2019 , 16:15   Re: How to Find Nearest Object & Entity (Help please)
Reply With Quote #3

Quote:
Originally Posted by PC Gamer View Post
You may be able to use some of the code from the talented Pelepoika. He has a plugin called TF2 Rollermine Spawner that uses a stock similar to what you are doing. His plugin spawns electric balls that follow and hit nearby players.

Link to TF2 Rollermine Spawner: https://forums.alliedmods.net/showthread.php?p=2370316
but this plugin finding nearest players i need a player find a nearest entity
EfeDursun125 is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 09-06-2019 , 16:17   Re: How to Find Nearest Object & Entity (Help please)
Reply With Quote #4

Quote:
Originally Posted by PC Gamer View Post
You may be able to use some of the code from the talented Pelepoika. He has a plugin called TF2 Rollermine Spawner that uses a stock similar to what you are doing. His plugin spawns electric balls that follow and hit nearby players.

Link to TF2 Rollermine Spawner: https://forums.alliedmods.net/showthread.php?p=2370316
maybe i can fix
EfeDursun125 is offline
Dr.Doctor
AlliedModders Donor
Join Date: Feb 2017
Location: Hong Kong
Old 09-07-2019 , 00:40   Re: How to Find Nearest Object & Entity (Help please)
Reply With Quote #5

Try this function
PHP Code:
int GetNearestEntity(int clientchar[] classname)
{
    
//Get the first entity
    
int nearestEntity FindEntityByClassname(entityclassname);
    if (!
IsValidEntity(nearestEntity))
    {
        return -
1;
    }
    
    
float clientVecOrigin[3], entityVecOrigin[3];
    
GetEntPropVector(clientProp_Data"m_vecOrigin"clientVecOrigin);
    
GetEntPropVector(nearestEntityProp_Data"m_vecOrigin"entityVecOrigin);
    
    
//Get the distance between the first entity and client
    
float distancenearestDistance GetVectorDistance(clientVecOriginentityVecOrigin);
    
    
//Find all the entity and compare the distances
    
int entity = -1;
    while (
entity FindEntityByClassname(entityclassname) != -1)
    {
        
GetEntPropVector(entityProp_Data"m_vecOrigin"entityVecOrigin);
        
distance GetVectorDistance(clientVecOriginentityVecOrigin);
        
        if (
distance nearestDistance)
        {
            
nearestEntity entity;
            
nearestDistance distance;
        }
    }
    
    return 
nearestEntity;

You can find the nearest "item_ammopack_medium" like this
PHP Code:
int ammopack GetNearestEntity(client"item_ammopack_medium");

if (
IsValidEntity(ammopack))
{
    
//YOUR CODE

__________________
Dr.Doctor is offline
zipcore
Veteran Member
Join Date: Mar 2010
Location: m_flZipcore
Old 09-07-2019 , 05:12   Re: How to Find Nearest Object & Entity (Help please)
Reply With Quote #6

PHP Code:
public int GetNearestEntity(int clientchar[] classname)
{
    
int nearestEntity = -1;
    
float clientVecOrigin[3], entityVecOrigin[3];
    
    
//Get the distance between the first entity and client
    
float distancenearestDistance = -1.0;
    
    
//Find all the entity and compare the distances
    
int entity = -1;
    while ((
entity FindEntityByClassname(entityclassname)) != -1)
    {
        
GetEntPropVector(entityProp_Data"m_vecOrigin"entityVecOrigin);
        
distance GetVectorDistance(clientVecOriginentityVecOrigin);
        
        if (
distance nearestDistance || nearestDistance == -1.0)
        {
            
nearestEntity entity;
            
nearestDistance distance;
        }
    }
    
    return 
nearestEntity;

Simplyfied code and fixed errors ;)
__________________
zipcore is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 09-07-2019 , 09:37   Re: How to Find Nearest Object & Entity (Help please)
Reply With Quote #7

Quote:
Originally Posted by Dr.Doctor View Post
Try this function
PHP Code:
int GetNearestEntity(int clientchar[] classname)
{
    
//Get the first entity
    
int nearestEntity FindEntityByClassname(entityclassname);
    if (!
IsValidEntity(nearestEntity))
    {
        return -
1;
    }
    
    
float clientVecOrigin[3], entityVecOrigin[3];
    
GetEntPropVector(clientProp_Data"m_vecOrigin"clientVecOrigin);
    
GetEntPropVector(nearestEntityProp_Data"m_vecOrigin"entityVecOrigin);
    
    
//Get the distance between the first entity and client
    
float distancenearestDistance GetVectorDistance(clientVecOriginentityVecOrigin);
    
    
//Find all the entity and compare the distances
    
int entity = -1;
    while (
entity FindEntityByClassname(entityclassname) != -1)
    {
        
GetEntPropVector(entityProp_Data"m_vecOrigin"entityVecOrigin);
        
distance GetVectorDistance(clientVecOriginentityVecOrigin);
        
        if (
distance nearestDistance)
        {
            
nearestEntity entity;
            
nearestDistance distance;
        }
    }
    
    return 
nearestEntity;

You can find the nearest "item_ammopack_medium" like this
PHP Code:
int ammopack GetNearestEntity(client"item_ammopack_medium");

if (
IsValidEntity(ammopack))
{
    
//YOUR CODE

Quote:
Originally Posted by zipcore View Post
PHP Code:
public int GetNearestEntity(int clientchar[] classname)
{
    
int nearestEntity = -1;
    
float clientVecOrigin[3], entityVecOrigin[3];
    
    
//Get the distance between the first entity and client
    
float distancenearestDistance = -1.0;
    
    
//Find all the entity and compare the distances
    
int entity = -1;
    while ((
entity FindEntityByClassname(entityclassname)) != -1)
    {
        
GetEntPropVector(entityProp_Data"m_vecOrigin"entityVecOrigin);
        
distance GetVectorDistance(clientVecOriginentityVecOrigin);
        
        if (
distance nearestDistance || nearestDistance == -1.0)
        {
            
nearestEntity entity;
            
nearestDistance distance;
        }
    }
    
    return 
nearestEntity;

Simplyfied code and fixed errors ;)
thanks i testing now!!
EfeDursun125 is offline
Dr.Doctor
AlliedModders Donor
Join Date: Feb 2017
Location: Hong Kong
Old 09-08-2019 , 06:13   Re: How to Find Nearest Object & Entity (Help please)
Reply With Quote #8

Quote:
Originally Posted by zipcore View Post
PHP Code:
public int GetNearestEntity(int clientchar[] classname)
{
    
int nearestEntity = -1;
    
float clientVecOrigin[3], entityVecOrigin[3];
    
    
//Get the distance between the first entity and client
    
float distancenearestDistance = -1.0;
    
    
//Find all the entity and compare the distances
    
int entity = -1;
    while ((
entity FindEntityByClassname(entityclassname)) != -1)
    {
        
GetEntPropVector(entityProp_Data"m_vecOrigin"entityVecOrigin);
        
distance GetVectorDistance(clientVecOriginentityVecOrigin);
        
        if (
distance nearestDistance || nearestDistance == -1.0)
        {
            
nearestEntity entity;
            
nearestDistance distance;
        }
    }
    
    return 
nearestEntity;

Simplyfied code and fixed errors ;)
@zipcore

You forgot
PHP Code:
GetEntPropVector(clientProp_Data"m_vecOrigin"clientVecOrigin); 
__________________
Dr.Doctor is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 09-08-2019 , 08:08   Re: How to Find Nearest Object & Entity (Help please)
Reply With Quote #9

Quote:
Originally Posted by Dr.Doctor View Post
@zipcore

You forgot
PHP Code:
GetEntPropVector(clientProp_Data"m_vecOrigin"clientVecOrigin); 
thank you but i getting warning

// D:\SteamLibrary\steamapps\common\Team Fortress 2\tf\addons\sourcemod\scripting\afk_bot.sp(26 07) : warning 217: loose indentation
// D:\SteamLibrary\steamapps\common\Team Fortress 2\tf\addons\sourcemod\scripting\afk_bot.sp(26 10) : warning 217: loose indentation
// Code size: 88212 bytes
// Data size: 13036 bytes
// Stack/heap size: 16384 bytes
// Total requirements: 117632 bytes
//
// 2 Warnings.
//
// Compilation Time: 0,66 sec
// ----------------------------------------

Press enter to exit ...

PHP Code:
public int GetNearestEntity(int clientchar[] classname)
{
    
int nearestEntity = -1;
    
float clientVecOrigin[3], entityVecOrigin[3];
    
    
GetEntPropVector(clientProp_Data"m_vecOrigin"clientVecOrigin); // Line 2607
    
    //Get the distance between the first entity and client
    
float distancenearestDistance = -1.0;
    
    
//Find all the entity and compare the distances
    
int entity = -1;
    while ((
entity FindEntityByClassname(entityclassname)) != -1)
    {
        
GetEntPropVector(entityProp_Data"m_vecOrigin"entityVecOrigin); // Line 2610
        
distance GetVectorDistance(clientVecOriginentityVecOrigin);
        
        if (
distance nearestDistance || nearestDistance == -1.0)
        {
            
nearestEntity entity;
            
nearestDistance distance;
        }
    }
    
    return 
nearestEntity;

EfeDursun125 is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 09-09-2019 , 06:08   Re: How to Find Nearest Object & Entity (Help please)
Reply With Quote #10

use tabs, avoid spaces for indenting rows.
__________________

Last edited by Ilusion9; 09-09-2019 at 06:10.
Ilusion9 is offline
Reply



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 02:23.


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