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(PLUGIN, VERSION, AUTHOR) /* 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(id, level, cid) /* 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(id, level, cid, 3)) /* 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(1, Arg1, 23) /* Only use 23 characters of the 24 available in array Arg1, in place for the null terminating byte */
read_argv(2, Arg2, 3) /* 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(players, num) /* Still don't see why this is used */
for (i=0; i<num; i++) /* 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(id, Arg1, 1) /* 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(player, Health) /* 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