AlliedModders

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

iloverain 06-23-2012 14:49

get_user_name ?
 
Hi all! I'm trying to get a users name. So I looked at the get_user_name function.
I saw someone post:

Code:

new name[18]
get_user_name(id, name, 17)
client_print(id, print_chat, "Your name is: %s", name)



Now, what I don't understand is the array, name[18]. What exactly does this do?

Then there's get_user_name(id, name, 17). what exactly is "id" for (index)? Then name? Also why's it 17 now?

client_print(id, ...) Whats the index there for? What is the index?


Sorry I'm just getting started in scripting. This may probably be the noobiest question ever asked. Hah.

Thanks in advance.

Arkshine 06-23-2012 14:53

Re: get_user_name ?
 
You should start to read the existing tutorials for beginner : https://forums.alliedmods.net/showth...awnProgramming

Backstabnoob 06-23-2012 14:56

Re: get_user_name ?
 
It's a string, not an array. The number means it can have 18 characters max. It looks exactly like an array because it works similiar to this:
Code:
new string[ 2 ] format( string, 1 /* max characters - 1 */, "oh" ) // we're copying "oh" into variable string string[ 0 ] = 'o' string[ 1 ] = 'h'
For a name, you should put 32 there, because 18 will not be enough if the player's nick reaches 18 characters and part of it will be cut off.

For the second question, id is the player index. It starts from 1 and ends with 32. It is passed in forwards like client_connect( id ). In your example, it means you're getting the name of player with index id.
name means you're copying the user's name into name string.
It is 17 because every string starts from 0 (not 1, see my example above), thus it's always max characters - 1.

For the third question, see above.

iloverain 06-23-2012 15:04

Re: get_user_name ?
 
Thank you guys for taking the time to answer. I really apreciate it. I understand now :)

edit;
Ok so I made this plugin but there are some errors:
Code:

Error: Invalid function or declaration on line 11
Error: Syntax error in the expression, or invalid function call on line 20
Error: Declaration of a local variable must appear in a compound block on line 22
Error: Undefined symbol "name" on line 22
Warning: Expression has no effect on line 22
Error: Expected token: ";", but found "]" on line 22
Error: Too many error messages on one line on line 22

Compilation aborted.
6 Errors.

Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "jm0d"
#define VERSION "1.0"
#define AUTHOR "iloverain"

new bool:isSimon
isSimon = false        // <----------------------------------------- LINE 11

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR)
   
    register_clcmd ( "say /simon", "SetSimon" )
}

public SetSimon() {
    if ( get_user_team == 2 )      // <--------------------------------------------- LINE 20
   
    new name[32];                    // <------------------------------------------------- LINE 22
    get_user_name( id, name, 31 );
   
    if ( isSimon == true ) {
        client_print(id, print_chat, "Someone is already Simon!");
    } else {
        client_print(id, print_chat, "%s is Simon!", name);
        isSimon = true
    }   
}


Waleed 06-23-2012 15:36

Re: get_user_name ?
 
To get user name use this method:

PHP Code:

new name[32
get_user_name(id,name,charsmax(name// Getting name here
client_print(id,print_chat," Your name is %s",name//Chat message to verify 

I think
PHP Code:

new bool:isSimon 

should be:

PHP Code:

new bool:isSimon[] or [33

To get team You can do this:

PHP Code:

if(cs_get_user_team(id) == CS_TEAM_CT//Include cstrike for this native 

Error on line 22 should be because you didn't put " { " or " } " after team check

EDIT: Check my practice plugin here : http://forums.alliedmods.net/showthread.php?t=188202
It would help you.

iloverain 06-23-2012 15:47

Re: get_user_name ?
 
@above Perfect! Thanks so much! I appreciate your help all!

I changed the bool to
Code:

new bool:isSimon=false
which worked perfectly.
I used the user name method Waleed suggested which worked perfectly.
I also googled how to get user team and i found the cstrike one too :P so yeah using that one.

Here's the working code:
Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <cstrike>

#define PLUGIN "jm0d"
#define VERSION "1.0"
#define AUTHOR "iloverain"

new bool:isSimon=false

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR)
   
    register_clcmd ( "say /simon", "SetSimon" )
}

public SetSimon(id) {
    (cs_get_user_team(id) == CS_TEAM_CT)
   
    new name[32];
    get_user_name( id, name, charsmax ( name ) )
   
    if ( isSimon == true ) {
        client_print( id, print_chat, "Someone is already Simon!" )
    } else {
        client_print( id, print_chat, "%s is Simon!", name )
        isSimon = true
    }   
}

Thanks all!

LOL edit;

last question. how would one reset a boolean to false after round end? o.0 I dont want to create a new thread for each question. Lmfao sorry.

Waleed 06-23-2012 15:58

Re: get_user_name ?
 
To find what natives or constants you need use AMX Wiki ---> http://www.amxmodx.org/funcwiki.php <---
Google is good for searching but AMX Wiki would be faster and easier to get info on modules and constants :)

<VeCo> 06-23-2012 16:10

Re: get_user_name ?
 
Every bool has false as default value, so you don't need to set it.

iloverain 06-23-2012 16:11

Re: get_user_name ?
 
oh ok thanks. ^ i also figured out how to make it back to false if it was true after round end. so I'm done for now! Thanks all :P

fysiks 06-23-2012 16:34

Re: get_user_name ?
 
Quote:

Originally Posted by Backstabnoob (Post 1734623)
It's a string, not an array. The number means it can have 18 characters max.

Let's not lie to him. A string is an array [of characters] with an "end of string" character to identify the end of the string. Also, if a string is declared with 18 then you can only use 17 characters (hence the 17 in the get_user_name() function).

Quote:

Originally Posted by <VeCo> (Post 1734656)
Every bool has false as default value, so you don't need to set it.

It is a good habit to initialize variables when they are declared regardless of whether they have a default value or not.


All times are GMT -4. The time now is 06:16.

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