AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved How to get the lowest health of the player? (https://forums.alliedmods.net/showthread.php?t=310748)

Edison1318 09-18-2018 07:59

How to get the lowest health of the player?
 
Well, let's just say i'm not advanced coder but I'm understood some coding experiences. I want to find player which has a lowest health. Drop comments to help me out.

ty.

eyal282 09-18-2018 08:45

Re: How to get the lowest health of the player?
 
It's always 1 as the lowest health a player can have being alone very and 0 the lowest possible which kills the player

Cruze 09-18-2018 09:03

Re: How to get the lowest health of the player?
 
Quote:

Originally Posted by eyal282 (Post 2615683)
0 the lowest possible which kills the player

and for csgo, it freezes the player

Edison1318 09-18-2018 09:12

Re: How to get the lowest health of the player?
 
I mean which player has a lowest health like for example:

Player 1: has 100%
Player 2: has 45%
Player 3: has 76%
Player 4: has 48%

Player 3 has a lowest health so i want to put like
if(playerhasalowesthealth(client))
{
(Do something)
}
Hope you what i mean.

Vaggelis 09-18-2018 09:30

Re: How to get the lowest health of the player?
 
Keep in mind this code will return only one client, i also suppose max health is 100HP, so i putted 101 in min.
PHP Code:

new client HasLowestHealth() 

PHP Code:

HasLowestHealth()
{
    new 
min 101
    
new clienthealth
    
    
for(new 1<= MaxClientsi++)
    {
        if(
IsClientInGame(i) && IsPlayerAlive(i))
        {
            
health GetClientHealth(i)
            
            if(
health min)
            {
                
min health
                client 
i
            
}
        }
    }
    
    return 
client



Shane1390 09-18-2018 09:49

Re: How to get the lowest health of the player?
 
Bool function on new syntax, since you need to iterate through every client, I guess the earlier return point could help a bit(?)

Code:

public bool playerHasLowestHealth(int client)
{
        if (!IsValidClient(client)) {
                return false;
        }
        int clientHp = GetClientHealth(client);
        for (int i = 1; i <= MAXPLAYERS; i++) {
                if (!IsValidClient(i) || !IsPlayerAlive(i)) {
                        continue;
                } else if (GetClientHealth(i) < clientHp) {
                        return false;
                }
        }
        return true;
}

stock bool IsValidClient(int client, bool nobots = false)
{
    if (client <= 0 || client > MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client)) || !IsClientInGame(client)) {
        return false;
    }
    return true;
}


Edison1318 09-18-2018 10:04

Re: How to get the lowest health of the player?
 
Quote:

Originally Posted by Vaggelis (Post 2615691)
Keep in mind this code will return only one client, i also suppose max health is 100HP, so i putted 101 in min.
PHP Code:

new client HasLowestHealth() 

PHP Code:

HasLowestHealth()
{
    new 
min 101
    
new clienthealth
    
    
for(new 1<= MaxClientsi++)
    {
        if(
IsClientInGame(i) && IsPlayerAlive(i))
        {
            
health GetClientHealth(i)
            
            if(
health min)
            {
                
min health
                client 
i
            
}
        }
    }
    
    return 
client



Quote:

Originally Posted by Shane1390 (Post 2615693)
Bool function on new syntax, since you need to iterate through every client, I guess the earlier return point could help a bit(?)

Code:

public bool playerHasLowestHealth(int client)
{
        if (!IsValidClient(client)) {
                return false;
        }
        int clientHp = GetClientHealth(client);
        for (int i = 1; i <= MAXPLAYERS; i++) {
                if (!IsValidClient(i) || !IsPlayerAlive(i)) {
                        continue;
                } else if (GetClientHealth(i) < clientHp) {
                        return false;
                }
        }
        return true;
}

stock bool IsValidClient(int client, bool nobots = false)
{
    if (client <= 0 || client > MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client)) || !IsClientInGame(client)) {
        return false;
    }
    return true;
}


Alright, much obliged. I would able to code further and my stuff with your help.

Ilusion9 09-18-2018 12:41

Re: How to get the lowest health of the player?
 
Quote:

Originally Posted by Shane1390 (Post 2615693)
Bool function on new syntax, since you need to iterate through every client, I guess the earlier return point could help a bit(?)

so you check if client > 0 && client <= MaxClients in a for that starts with 1 and ends at MaxClients?
also isclientconnected it's useless when you check if the client is in game.

PHP Code:

public int HasLowestHealth()
{
    
int client = -1min = -1;
    
    for (
int i 1<= MaxClientsi++)
    {
        if (
IsClientInGame(i))
        {
            if (
IsPlayerAlive(i))
            {
                
int health GetClientHealth(i);
                
                if (
min == -1)
                {
                    
min health;
                    
client i;
                }
                else
                {
                    if (
health min)
                    {
                        
min health;
                        
client i;
                    }
                }
            }
        }
    }

    return 
client;



Vaggelis 09-18-2018 13:21

Re: How to get the lowest health of the player?
 
Quote:

Originally Posted by Ilusion9 (Post 2615727)
so you check if client > 0 && client <= MaxClients in a for that starts with 1 and ends at MaxClients?
also isclientconnected it's useless when you check if the client is in game.

PHP Code:

public int HasLowestHealth()
{
    
size 0;
    
int client = -1min = -1;
    
    for (
int i 1<= MaxClientsi++)
    {
        if (
IsClientInGame(i))
        {
            if (
IsPlayerAlive(i))
            {
                
int health GetClientHealth(i);
                
                if (
min == -1)
                {
                    
min health;
                    
client i;
                }
                else
                {
                    if (
health min)
                    {
                        
min health;
                        
client i;
                    }
                }
            }
        }
    }

    return 
client;



Whats the point posting that code? Edison1318 got what he wanted

Edison1318 09-18-2018 14:09

Re: How to get the lowest health of the player?
 
Quote:

Originally Posted by Vaggelis (Post 2615739)
Whats the point posting that code? Edison1318 got what he wanted

It's okay. Since I got the code, i'm alright with trying people's code just to test in my game as well.


All times are GMT -4. The time now is 19:04.

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