AlliedModders

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

rain 10-17-2004 15:35

get_user_name and null strings
 
How can I determine wheter get_user_name() provides me with a real valid player name or a (null) string? As a natural way I thought about the return value of this function, but in fact it returns the set_amxstring return value ;-) which is not what I want. I know this kind of checks is not *that* needed, but I like to write 100% reliable progs/scripts (ya, just my habit :-)). TIA for any replies.

devicenull 10-17-2004 16:03

strlen(name) > 0?

rain 10-17-2004 16:10

Nope, tried this. It returns 6. ;-) I don't know why, but non-existent names are returned not as real null strings, but sth like this:

name[0] = '('
name[1] = 'n'
name[2] = 'u'
name[3] = 'l'
name[4] = 'l'
name[5] = ')'

Haven't checked it on the latest amxx yet, so don't how it looks there. The strlen looks reasonable and I was a bit surprised to find this kind of strings. Dind't dig to the code that deep either, I will later check where it gets changed.

devicenull 10-17-2004 17:35

In what case can you get a null name anyway?

rain 10-18-2004 10:29

I think that specifying an invalid player ID can result in a null name (try calling get_user_name() with a bogus id).

What I make is:
Code:
// [...] function1() {    new players[32]    new playerCount        /* This is point 1 */    get_players(players, playerCount, "c")    // [...] Then I choose the best player (highest fragcount) and pass his ID to another function which prints his name on screen    function2(id) } function2(id) {    new name[32]        /* This is point 2 */    get_user_name(id, name, 32)    set_hudmessage(200, 100, 0, -1.0, 0.85, 0, 6.0, 10.0, 0.1, 0.2, 3)    show_hudmessage(0, name) }

Well, I know that it is almost impossible for a player to leave in the time occuring between points 1 and 2, but I wouldn't call it a 100% reliable method (or I just don't get it, are there any limits on get_user_name calls?). It doesn't seem to be a problem really, cause it's a simple plugin (the execution time between 1 and 2 should be near 0), so it's not that dangerous. Nevertheless, it's a flaw in a plugin, which I would like to avoid. If it's not possible - nevermind, I will write it the way you all do. ;-)

devicenull 10-18-2004 17:47

is_valid_user(id) :)
Just check if the user is valid with that, and if its not, don't try to display the name :)

Johnny got his gun 10-18-2004 18:50

hmm?
Code:
is_user_connected(id) is_valid_ent(id)

devicenull 10-18-2004 19:21

is_user_connnected is the one I meant :P
Couldn't think of the name, and is_valid_user seemed right :P

rain 10-19-2004 09:01

Well, yes. But I thought get_players() itself would return valid IDs. I just did not want to split the two operations which IMHO should be combined - validation of ID and its use to get playername. OK, I know I am messing things a bit and I am not clear. Just downloaded the amxx's source code, give me a minute, maybe I will come up with sth more precise.

UPDATE: OK, I think I've nailed down the problem. Stay tuned. ;-)

rain 10-19-2004 10:19

Nah, it's resolved. Lastly. My fault, sorry devicenull, you were correct. The strlen() is the way to go (obviously it seemed weird it didn't work). Just forgot the first 'bug reporting' rule - update your sources. Everything is fine on amxx 0.20 and CVS. The thing is I've used an old version - 0.16 and the String handling there, well - let's say could be improved (and in fact it did, thx guys).

Just look at this line of CString.h (old):
Code:

inline const char* str() const {  return napis ? napis : "(null)"; }
And now (the new one):
Code:

const char *c_str() { return v?v:""; }
const char *c_str() const { return v?v:""; }

This is what I was looking for. Thank you for helping me resolve this issue.


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

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