AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Get_players stock (https://forums.alliedmods.net/showthread.php?t=332127)

Crackhead69 04-24-2021 12:07

Get_players stock
 
Hello!
I am trying to figure out how to make a get_players stock which can be used like so
PHP Code:

new players

public plugin_init() {
    
register_clcmd("say /test","TestStock")
}

public 
TestStock(id){
    if(!
is_user_alive(id) || !is_user_connected(id)) return
    
    
get_them_players("ae","TERRORIST")
    
    
set_user_health(players20)
}

stock get_them_players(szFlags[] = ""szTeam[] = ""){
    
players = -1
    
    
new iPlayers[32], iPnum
    get_players
(iPlayersiPnumszFlagsszTeam)
    
    for(new 
i;iiPnum;i++)
        
players iPlayers[i]


From the above code the goal is to set the HP of all the T players using the stock to 20 by calling the stock,
tho as i test it, it sets the hp to 20 on random T players, not on all of them.

I would appreciate if somebody could lay some wisdom.

Xalus 04-24-2021 12:25

Re: Get_players stock
 
You will have to give health to each player in the loop, they way you are trying is not possible.

Crackhead69 04-24-2021 12:44

Re: Get_players stock
 
Oh really? Didn't know that
So in general it is not possible to store the iPlayers[33] from get_players into a global variable?

LondoN 04-24-2021 12:52

Re: Get_players stock
 
Code:

stock get_and_set(szFlags[] = "", szTeam[] = "", iHealth)
{
        new Players[32], iNum;
        get_players(Players, iNum, szFlags, szTeam);

        for(new i; i < iNum; i++)
        {
                if(!iHealth)
                        break;

                set_user_health(Players[i], iHealth);
        }
}

Usage:

Code:

get_and_set("ae", "TERRORIST", 250);

fysiks 04-24-2021 14:48

Re: Get_players stock
 
Quote:

Originally Posted by Crackhead69 (Post 2745002)
Oh really? Didn't know that
So in general it is not possible to store the iPlayers[33] from get_players into a global variable?

Well, technically you can store anything in a global variable but that's not actually relevant here. You also don't want to do this because the result of get_players() will change depending on when people join and leave so you should always use get_players() when you need it so that you don't try to run functions on non-existent players (or players that no longer match the provided flags.

Also, your "players" global variable is a scalar so it will only contain a single value ever. In the for loop, you're simply overwriting that variable many times without having done anything with it and will have only the last player index in it when get_them_players() finishes executing. So, when you eventually call set_user_health() you are doing so on only that last player.

So here, based on just your code alone, the stock is unnecessary, simply put the get_players() and the loop in your main function and set the user's health for each player in that loop.

Quote:

Originally Posted by LondoN (Post 2745003)
Code:
stock get_and_set(szFlags[] = "", szTeam[] = "", iHealth) {     new Players[32], iNum;     get_players(Players, iNum, szFlags, szTeam);     for(new i; i < iNum; i++)     {         if(!iHealth)             break;         set_user_health(Players[i], iHealth);     } }

  1. iHealth will always be the same value in this function so it's pointless to even do any looping or even get_players() if it's just going to break out of the loop immediately.
  2. Also, a check for iHealth being zero doesn't make sense, the person calling the function should just not call the function if they don't want it to do it's function.
  3. You aren't really helping if you don't explain anything about what the OP is actually having issues with.

LondoN 04-24-2021 14:54

Re: Get_players stock
 
Quote:

Originally Posted by fysiks (Post 2745010)
Well, technically you can store anything in a global variable but that's not actually relevant here. You also don't want to do this because the result of get_players() will change depending on when people join and leave so you should always use get_players() when you need it so that you don't try to run functions on non-existent players (or players that no longer match the provided flags.

Also, your "players" global variable is a scalar so it will only contain a single value ever. In the for loop, you're simply overwriting that variable many times without having done anything with it and will have only the last player index in it when get_them_players() finishes executing. So, when you eventually call set_user_health() you are doing so on only that last player.

So here, based on just your code alone, the stock is unnecessary, simply put the get_players() and the loop in your main function and set the user's health for each player in that loop.


  1. iHealth will always be the same value in this function so it's pointless to even do any looping or even get_players() if it's just going to break out of the loop immediately.
  2. Also, a check for iHealth being zero doesn't make sense, the person calling the function should just not call the function if they don't want it to do it's function.
  3. You aren't really helping if you don't explain anything about what the OP is actually having issues with.

it's depend on how you use the stock.

Let's take an example:

instead of putting 250 as health (stock parameter) we also can use get_pcvar_num to take the value from a cvar.
if the cvar is zero just break the loop, this is what i wanted when i checked for !iHealth

Natsheh 04-24-2021 16:01

Re: Get_players stock
 
Nonsense.

There is no need for a get_players stock thats nonsense use get_players as it supposed to be used.

fysiks 04-25-2021 01:21

Re: Get_players stock
 
Quote:

Originally Posted by LondoN (Post 2745011)
it's depend on how you use the stock.

Let's take an example:

instead of putting 250 as health (stock parameter) we also can use get_pcvar_num to take the value from a cvar.
if the cvar is zero just break the loop, this is what i wanted when i checked for !iHealth

You didn't put a cvar and even if you had a cvar, the cvar wont' change in the middle of the loop.


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

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