AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Some things I wonder about. (https://forums.alliedmods.net/showthread.php?t=193670)

Moody92 08-21-2012 17:36

Some things I wonder about.
 
Hello there, I am in the intro tutorial but I didn't understand this

PHP Code:

public cmd_hp(idlevelcid)
{
     if (!
cmd_access(idlevelcid3))
        return 
PLUGIN_HANDLED
 
     
new Arg1[24]
     new 
Arg2[4]
 
     
//Get the command arguments from the console
     
read_argv(1Arg123)
     
read_argv(2Arg23)
 
     
//Convert the health from a string to a number
     
new Health str_to_num(Arg2)
 
     
//Is the first character the @ symbol?
     
if (Arg1[0] == '@')
     {
          new 
Team 0
          
if (equali(Arg1[1], "CT"))
          {
               
Team 2
          
} else if (equali(Arg1[1], "T")) {
               
Team 1
          
}
          new 
players[32], num
          get_players
(playersnum)
          new 
i
          
for (i=0i<numi++)
          {
               if (!
Team)
               {
                    
set_user_health(players[i], Health)
               } else {
                    if (
get_user_team(players[i]) == Team)
                    {
                         
set_user_health(players[i], Health)
                    }
               }
          }
     } else {
          new 
player cmd_target(idArg11)
          if (!
player)
          {
               
console_print(id"Sorry, player %s could not be found or targetted!"Arg1)
               return 
PLUGIN_HANDLED
          
} else {
               
set_user_health(playerHealth)
          }
     }
 
     return 
PLUGIN_HANDLED


First thing is
PHP Code:

return PLUGIN_HANDLED 

why did he used PLUGIN_HANDLED exactly. and what if I put it without return PLUGIN_HANDLED, or retrun PLUGIN_CONTINUE or even return % what will happen ?

2nd thing
PHP Code:

     read_argv(1Arg123)
     
read_argv(2Arg23

I realize the number 1,2 the arguments number but which I didn't understand is 23,3 I think they're the ID of the operation but how to get the id or it should be specific numbers depends on the operation ?

also this one
PHP Code:

          new players[32], num 

What is 32 ? I saw in many threads that It's player index id, but what is player index id ?

Napoleon_be 08-21-2012 18:34

Re: Some things I wonder about.
 
2nd thing: the 23 and 3 are charsmaxes from the arrays you've declared.
For example, you have
Arg1[24], Arg2[4]
read_argv(1, Arg1, charsmax(Arg1))
read_argv(2, Arg2, charsmax(Arg2))

This would just be the same

3rd thing,

It's very hard to explain, but how much players can there be max in a server? 32, how many characters can a STEAM ID have? This is still being complained about, but it's just the size of the array you want it to be.

pokemonmaster 08-21-2012 18:38

Re: Some things I wonder about.
 
Quote:

Originally Posted by Moody92 (Post 1778286)
Hello there, I am in the intro tutorial but I didn't understand this

First thing is
PHP Code:

return PLUGIN_HANDLED 

why did he used PLUGIN_HANDLED exactly. and what if I put it without return PLUGIN_HANDLED, or retrun PLUGIN_CONTINUE or even return % what will happen ?

Quote:

Originally Posted by wiki.alliedmods.net
PLUGIN_CONTINUE generally means "continue with normal operation", and PLUGIN_HANDLED means "block further operation". The differences are subtle but important. For example, when binding a command, you should never return PLUGIN_CONTINUE. But if you return PLUGIN_HANDLED while binding to the "say" command, it will block the player's text from ever appearing.

So clearly, he used return PLUGIN_HANDLED to block any further functions from working (Stop the command from working to make it simpler)

Quote:

Originally Posted by Moody92 (Post 1778286)
2nd thing
PHP Code:

     read_argv(1Arg123)
     
read_argv(2Arg23

I realize the number 1,2 the arguments number but which I didn't understand is 23,3 I think they're the ID of the operation but how to get the id or it should be specific numbers depends on the operation ?

Actully, your wrong.
The last argument in the function(s) (23, 3) are the len.
Len is max characters that can be stored in Arg1, Arg2.
Just to make sure you understood,
PHP Code:

new szString1[35]     // Here, max characters that can be stored 34, but len can be 1,2,3,4,5,6,7.......34
// So,
charsmax(szString// == 34 

Quote:

Originally Posted by Moody92 (Post 1778286)
also this one
PHP Code:

          new players[32], num 

What is 32 ? I saw in many threads that It's player index id, but what is player index id ?

Player index is the player's unique id, defined by amxx or half life engine (I don't really know).
Player index ranges from 1 to 32 (Thats why we created an array with 32 rows), but when you use this
PHP Code:

get_players(playersnum

All players indexes will be stored in players like this: (BTW, count is the number of the players)
PHP Code:

// You need to read arrays to understand this

// If count == 5  (Note that == is used for checking, = is used for assigning)
players[0] = 1
players
[1] = 2
players
[2] = 3
players
[3] = 4
players
[4] = 5

// If count == 10 (Players connected are 10)
players[0] = 1
players
[1] = 2
players
[2] = 3
players
[3] = 4
players
[4] = 5
players
[5] = 6
players
[6] = 7
players
[7] = 8
players
[8] = 9
players
[9] = 10
// Etc 

Also, as I know, ids (Players indexes) are assigned depending on who connects first, so if the server had 0 players, and you connected, players count will change to 1, also your index will be 1

Let's say that there were 3 players connected, and players[32] was like this
PHP Code:

players[0] = 1      // Player 1
players[1] = 2      // Player 2
players[2] = 3      // Player 3 

And then suddenly, player with index 2 has disconnected, so who will get index 2?
players are shifted automatically to fill the empty index
so players[32] will be like this:
PHP Code:

players[0] = 1      // Player 1
players[1] = 2      // Player 3 

That's all.

I too at the first didn't understand much about indexes, but then I started reading and testing till I understood it.
I think you should do that too.

Hopefully, you understood everything.
Regards.

EDIT: LOOOOL!
After I wrote all this post, I looked in the forum just to waste sometime, then I found this:
http://forums.alliedmods.net/showthread.php?t=181629
My idea about indexes was just a thought, I only understood it from get_players as I got confused in it, but it turned out that my thought is true! LOL!
PS: Only read the top of that post, you will get confused about other things.

Moody92 08-22-2012 04:23

Re: Some things I wonder about.
 
I think I've understood about player indexes, so we need to put it [32] always to avoid mixing up with their numbers

claudiuhks 08-22-2012 04:34

Re: Some things I wonder about.
 
Each cell may store a value.

PHP Code:

new c] = { 'a''b''c' }; // three
new a] = { 'a''x' }; // two
new d] = { 123}; // four

/*
  So, d[ 0 ] = 1, d[ 1 ] = 2, d[ 2 ] = 3, d[ 3 ] = 4.

  c = "abc";
  c[ 0 ] = 'a', c[ 1 ] = 'b', c[ 2 ] = 'c';
*/ 

You have to define 32 cells for all players. From 0 to 31 inclusive.
If you want to use the cells from 1 to 32 inclusive, you have to define 33 cells.

pokemonmaster 08-22-2012 05:05

Re: Some things I wonder about.
 
Quote:

Originally Posted by Moody92 (Post 1778597)
I think I've understood about player indexes, so we need to put it [32] always to avoid mixing up with their numbers

PHP Code:

// As I said before, player indexes ranges from 1 two 32
// Lets say you made a new array with 32 rows
new iArray[32]
// and id = 32
// Now if you did this it will give you an error (Index out of bounds)
iArray[id] = 5
server_print
("%d"iArray[id])// And this function won't work
// What happened here that it gave an error because there is no such a row with the number 32 in Array
// Arrays ranges from 0 to (Whatever you assign the array with) - 1


// So to make it correct, you must declare the array with 33 rows (last row is 32)
new iArray[33]
iArray[id] = 5
server_print
("%d"iArray[id])    // This would print in server console 5 

You just declare the array (that you are going to use with get_players) with 32 rows, because that's how the function works
PHP Code:

new players[32], count
get_players
(playerscount)

/* 
if count == 5
Then players will look like this
players[0] = 1
players[1] = 2
players[2] = 3
players[3] = 4
players[4] = 5
*/ 


dark_style 08-22-2012 05:20

Re: Some things I wonder about.
 
Note that if he uses get_maxplayers(), he should use it like that:

PHP Code:

new i_MaxPlayers get_maxplayers();
for( new 
id 1id <= i_MaxPlayersid++ ) 


Moody92 08-23-2012 16:09

Re: Some things I wonder about.
 
Quote:

Originally Posted by pokemonmaster (Post 1778624)
PHP Code:

new iArray[32]
// and id = 32
iArray[id] = 5
server_print
("%d"iArray[id])// And this function won't work
// WRONG ^^

// THE RIGHT ONE
new iArray[33]
iArray[id] = 5
server_print
("%d"iArray[id]) 

PHP Code:

new players[32], count
get_players
(playerscount)

/* 
if count == 5
Then players will look like this
players[0] = 1
players[1] = 2
players[2] = 3
players[3] = 4
players[4] = 5
*/ 


I got it now so to make it simple we just need to declare the array to 33 but the server will consider it as 32, and if we make it less than 33 there will be an issue.

HTML Code:

(Index out of bounds)
Correct me if I am wrong.


ConnorMcLeod 08-23-2012 16:13

Re: Some things I wonder about.
 
Quote:

Originally Posted by dark_style (Post 1778636)
Note that if he uses get_maxplayers(), he should use it like that:

PHP Code:

new i_MaxPlayers get_maxplayers();
for( new 
id 1id <= i_MaxPlayersid++ ) 


This is a bad way, get_players should always be used, so you directly get a list (filled in players[32] array) of connected players.
With the other method, on each iteration you need to check if the player is connected, which is bad for performances.
Also get_players lets you add the "a" flag to retrieve only alive player that improve performances in the same way that having to put is_user_alive on each iteration.

dark_style 08-24-2012 01:28

Re: Some things I wonder about.
 
Connor what I have meant was if he wants to use get_maxplayers, he should put 1 for starting value, I didn't mean he should use get_maxplayers.


All times are GMT -4. The time now is 05:42.

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