Raised This Month: $ Target: $400
 0% 

BAIL's HP Plugin Explanation


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
[kirk]./musick`
Senior Member
Join Date: Jun 2007
Location: Tampa, Florida
Old 01-16-2008 , 20:08   BAIL's HP Plugin Explanation
Reply With Quote #1

I'm having a hard time understand some of the aspects of the Basic 'HP' plugin given in the AMXX wiki. I understand code that is familiar to me, like how you declare a 24 character array, but only use 23 for the null terminator, and such. That area comes easy to me, but whenever I'm introduced to new functions, with ID's and other weird arguments I've never had to use before, I'm kind of lost. I don't really want to try and figure some of this stuff out on my own since I don't want to get something wrong I think is right, and then stick with it. I would want to know the correct way to use these commands, functions, etc.

If someone is willing to help that would be really great. I hope I'm not really asking for too much I just want a little detailed explanation of the HP Plugin down below, explaining the use of functions, arguments, etc. and why you would use them, when you would use them, etc. I've commented on the sections I know for sure, and the ones I need help on.

PHP Code:
/* I understand the includes, they are there to include the functions, definitions, etc for the plugin to run. */
#include <amxmodx>
#include <amxmisc>
#include <fun>

/* Also, the defines, it's used to replace keywords, kind of like variables, in the plugin */
#define PLUGIN "Health Plugin"
#define VERSION "1.0"
#define AUTHOR "BAILOPAN"


public plugin_init() /* I understand that, it's like the int main () function used in C/C++. It declares the body of the plugin */
{
     
register_plugin(PLUGINVERSIONAUTHOR/* Also understand that, it registers the plugin, version, and author passed by the define at the top of the code to the plugin */
     
register_concmd("amx_hp""cmd_hp"ADMIN_SLAY"<target> <hp>"/* I'm assuming I understand this, the first argument is the command used for the plugin is amx_hp, it would be typed in the console to actually use the plugin. The next argument is used to declare the function that is to be used, which is below this. The next argument is used to declare the admin level of the user, you must have level ADMIN_SLAY or above to use, or it exits the function. The next argument is a description of the usage of the plugin. I think that would be displayed if there were no arguments when called in console. */
}
 
public 
cmd_hp(idlevelcid/* I don't know if I'm 100% correct on this one, but I'm guessing 'id' is used to pass the id of the player attempting to use the plugin. 'level' is used to pass the current level of access of the user attempting to run the plugin client-side. 'cid' is the commands internal id, I got that from the article in the wiki, but I don't understand why it is used */
{
     if (!
cmd_access(idlevelcid3)) /* I need some help on this one, I don't understand what the 3 is for, and why you would need to pass the cid, id, and level again. I bet clearing up the number 3 would help a lot */
        
return PLUGIN_HANDLED /* This exits the plugin? Kind of like the 'break;' statement in C++ and PHP? */
 
     
new Arg1[24/* This is used to declare the string array to store the username given through console? */
     
new Arg2[4/* This is used to declare the numeric array to store the HP given through console? */
 
     //Get the command arguments from the console
     
read_argv(1Arg123/* Only use 23 characters of the 24 available in array Arg1, in place for the null terminating byte */
     
read_argv(2Arg23/* Only use 3 characters of the 4 available in array Arg2, in place for the null terminating byte */
 
     //Convert the health from a string to a number
     
new Health str_to_num(Arg2/* heh, already explained ^^, I get that */
 
     //Is the first character the @ symbol?
     
if (Arg1[0] == '@'/* Already explained ^^ */
     
{
          new 
Team 0
          
if (equali(Arg1[1], "CT")) /* If the second and third characters are "CT" to change the team to CT. */
          
{
               
Team 2
          
} else if (equali(Arg1[1], "T")) {
/* If the second charcater is "T" to change the team to T. */
               
Team 1
          
}
          new 
players[32], num /* Only 32 players available at once, so it does.. what? I don't exactly understand this one */
          
get_players(playersnum/* Still don't see why this is used */
          
for (i=0i<numi++) /* If i is less than num, which is used in the above statement, but wouldn't that circle through all of the players? And if you circled through everyone, then wouldn't you be changing everybody's health? */
          
{
               if (!
Team/* If there is nothing in the 'team' value, however, 'team' is already set to 0, so wouldn't that be technically be a value? */
               
{
                    
set_user_health(players[i], Health/* If you went through the entire player list, then how would you be accurate with this? Wouldn't this just change the HP of every user? Shouldn't there be a statement that checks for the username entered? */
               
} else {
                    if (
get_user_team(players[i]) == Team/* I guess Team number 1 is the T's, and 2 is the CT's? From what I've collected within a second, is that this points to the team the players are on, which filters out based on team. */
                    
{
                         
set_user_health(players[i], Health/* This is the only place where scrolling through all players makes sense because the user entering command wants only the CT's, or T's. Therefore, only selecting the Team the users are on and then going through every number checking for current team would make sense */
                    
}
               }
          }
     } else {
          new 
player cmd_target(idArg11/* Nevermind about all of the 'scrolling through all players' controversy, I figured that out on my own. I never noticed the scope of the if statement checking for '@' in the first character, now it makes complete sense. */
          
if (!player)
          {
               
console_print(id"Sorry, player %s could not be found or targeted!"Arg1/* So if the player containing the text was entered, output to the console that the player couldn't be found? */
               
return PLUGIN_HANDLED /* Stop the plugin from continuing if error had occured? */
          
} else {
               
set_user_health(playerHealth/* Now you set the health of the selected user, only if the argument sent from Console doesn't have '@' as the first character, and if the player can be found, right? */
          
}
     }
 
     return 
PLUGIN_HANDLED /* Completely end the plugin from continuing */

There were only a couple of confusing things in there, but I wanted to list the entire code, and I wanted to comment on what I knew and didn't know so whoever helped didn't have to sit there and explain every bit of code even if I may have already understood it.

Thanks a lot!

-Kirk
[kirk]./musick` is offline
Send a message via AIM to [kirk]./musick`
pharse
Senior Member
Join Date: Jan 2008
Location: Germany, BW
Old 01-16-2008 , 21:03   Re: BAIL's HP Plugin Explanation
Reply With Quote #2

You can look up many functions (mostly with explanation) here:
http://www.amxmodx.org/funcwiki.php


I'll comment on your comments defined by the line numbers, don't want to quote all the time...


18: In line 15 you register a custom console cmd. This cmd is exactly defined by the cid value.

20: see the funcwiki for cmd_access. There you need the cid.

21: Some functions have to return a value, informing the engine to continue, supercede etc... See http://www.amxmodx.org/funcwiki.php?...1#const_return

59ff: see the funcwiki for cmd_target. If it couldn't find or identify uniquely a player it returns 0. Then No action is done but informing the admin that no player has been found.



Hope this helps you. Also have a look at the stickies in this forum section. Very useful stuff.
__________________
pharse is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-16-2008 , 21:14   Re: BAIL's HP Plugin Explanation
Reply With Quote #3

Quote:
'cid' is the commands internal id, I got that from the article in the wiki, but I don't understand why it is used
Because it's used with cmd_access() function, for example.


Quote:
I need some help on this one, I don't understand what the 3 is for
amx_hp <target> <hp>
  • First argument : command name
  • Second argument : target
  • Third argument : hp

Finaly, you have 3 arguments. That's why.


Quote:
and why you would need to pass the cid, id, and level again
Because cid is used in cmd_access() funtcion through get_concmd(). this function can be found in amxmisc.inc file.

PHP Code:
stock cmd_access(idlevelcidnumbool:accesssilent false
{
    new 
has_access 0;
    if ( 
id==(is_dedicated_server()?0:1) ) 
    {
        
has_access 1;
    }
    else if ( 
level==ADMIN_ADMIN )
    {
        if ( 
is_user_admin(id) )
        {
            
has_access 1;
        }
    }
    else if ( 
get_user_flags(id) & level )
    {
        
has_access 1;
    }
    else if (
level == ADMIN_ALL
    {
        
has_access 1;
    }

    if ( 
has_access==
    {
        if (!
accesssilent)
        {
#if defined AMXMOD_BCOMPAT
            
console_print(idSIMPLE_T("You have no access to that command."));
#else
            
console_print(id,"%L",id,"NO_ACC_COM");
#endif
        
}
        return 
0;
    }
    if (
read_argc() < num
    {
        new 
hcmd[32], hinfo[128], hflag;
        
get_concmd(cid,hcmd,31,hflag,hinfo,127,level);
#if defined AMXMOD_BCOMPAT
        
console_print(idSIMPLE_T("Usage:  %s %s"), hcmdSIMPLE_T(hinfo));
#else
        
console_print(id,"%L:  %s %s",id,"USAGE",hcmd,hinfo);
#endif
        
return 0;
    }
    
    return 
1;

get_concmd() is used to get info about console command. As you can see on the last condition, if number of arguments found is < number of specified argument ( Basically, if the user doesn't supply enough parameters ) then it will displayed a message with command usage and using command description, if set.


Quote:
This is used to declare the string array to store the username given through console?
Quote:
This is used to declare the numeric array to store the HP given through console?
Yep.

Quote:
Only 32 players available at once, so it does.. what? I don't exactly understand this one
...
Still don't see why this is used
get_players() returns a list of player indices. If specified, you can constrain which players are returned by flags.

Quote:
"a" - Don't return dead players
"b" - Don't return alive players
"c" - Skip bots
"d" - Skip real players
"e" - Match with passed team
"f" - Match with part of name
"g" - Ignore case sensitivity
"h" - Skip HLTV
E.g : get_players( players, num, "a" ) returns a list of alive player indices. 'num' contains the number of players found. Now we can loop through players found and execute some commands for each player.

If array is set to 32 is because the first id starts to 0 with this native. 0 to 31. That's why 32 is fine.


Quote:
If i is less than num, which is used in [...]
See my comments.

Code:
            for( i = 0; i < num; i++ )             {                 // It's an optimization. (*)                 player = players[i]                                 if( !Team )                 {                     // If no team has been defined through the command, we are going to consider for all players..                     set_user_health( player, Health )                 }                 else // .. otherweise, let's check.                 {                     if( get_user_team( player ) == Team )                     {                         // If player's team matches with the team defined in the command, we are given health.                         set_user_health( player, Health )                     }                }             }

(*) For more informations : http://wiki.alliedmods.net/Optimizin...e-index_Arrays

Quote:
So if the player containing the text was entered, output to the console that the player couldn't be found?
If cmd_target() doesn't find the specified player, then we display this message.

Quote:
Now you set the health of the selected user, only if the argument sent from Console doesn't have '@' as the first character, and if the player can be found, right?
cmd_target() found the specified player, now we can set health.


About PLUGIN_CONTINUE / PLUGIN_HANDLED :

Quote:
#define PLUGIN_CONTINUE 0 /* Results returned by public functions */
#define PLUGIN_HANDLED 1 /* stop other plugins */
#define PLUGIN_HANDLED_MAIN 2 /* to use in client_command(), continue all plugins but stop the command */


On a site note :

Please, please, search before to post. There are so many threads speaking about what you asked. it exists useful links like http://www.amxmodx.org/funcwiki.php to see some descriptions. You should also take a look at Amxx includes. Really, search, search again, try to understand by yourself and if you don't get it yet, then post a message.
__________________

Last edited by Arkshine; 01-16-2008 at 21:23.
Arkshine is offline
[kirk]./musick`
Senior Member
Join Date: Jun 2007
Location: Tampa, Florida
Old 01-16-2008 , 21:33   Re: BAIL's HP Plugin Explanation
Reply With Quote #4

Thanks for both of you guys' help, it has helped me understand a lot so far.
I'm trying to write a little tiny replace weapons plugin to start off with from scratch, so I can understand all of the functions and how they are used. It's truly been a great help, especially since I can now understand basic plugins, functions, etc. just within a few days. I'll be overviewing other functions, looking at other peoples code, and such so that I get a good understanding of what I can exactly do.

Sorry, I'll be sure to search all over the place before I post again. Again, thanks for all the help both of you, this has really helped me a lot.

Kirk
[kirk]./musick` is offline
Send a message via AIM to [kirk]./musick`
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 15:27.


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