AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Get player by distance (https://forums.alliedmods.net/showthread.php?t=214064)

Killer zm 04-22-2013 13:13

Get player by distance
 
My code

PHP Code:

public FindClosesEnemy(entid)
{
    new 
Float:Dist
    
new Float:maxdistance=4000.0
    
new indexid=0
    
    
    
for(new i=1;i<=g_iMaxPlayers;i++)
    {
        
        
        if(
is_user_alive(i) || is_user_bot(i) && is_valid_ent(i) && can_see_fm(entidi))
        {
            
Dist entity_range(entidi)
            
            
            if(
Dist <= maxdistance)
            {
                
maxdistance=Dist
                indexid
=i
                
                
return indexid
            
}
        }    
    }    
    return 
0


Why is always return indexid = 1
and i saw that when i change new i=1 with another number new i=5 indexid will be returned like 5

Please help me .Thank You

didoWEE 04-22-2013 14:12

Re: Get player by distance
 
because when you return sth, your function stops, the whole function
you can store the indexes in array

Killer zm 04-22-2013 14:36

Re: Get player by distance
 
how ??

jimaway 04-22-2013 17:02

Re: Get player by distance
 
you exit the function after first iteration of the for loop

Code:
public FindClosesEnemy(entid) {     new Float:Dist     new Float:maxdistance=4000.0     new indexid=0             for(new i=1;i<=g_iMaxPlayers;i++)     {                         if(is_user_alive(i) || is_user_bot(i) && is_valid_ent(i) && can_see_fm(entid, i))         {             Dist = entity_range(entid, i)                                     if(Dist <= maxdistance)             {                 maxdistance=Dist                 indexid=i             }         }         }         return (indexid) ? indexid : 0 }

Killer zm 04-23-2013 12:54

Re: Get player by distance
 
is working thank you . TC


All times are GMT -4. The time now is 10:51.

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