This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

Solved Stale Variable problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
N3v3rM1nd
Member
Join Date: Apr 2021
Old 05-09-2024 , 09:17   Stale Variable problem
Reply With Quote #1

Hi, I have a problem.

I have this global saved,
PHP Code:
new_g_cName[33][32
, it holds the names of players.


PHP Code:
public client_putinserver(iPlayer)
    
get_user_name(iPlayerg_cName[iPlayer], charsmax(g_cName[])); 

PHP Code:
public client_infochanged(id)
{
    if(!
is_user_connected(id) || g_bFake[id])
        return;
    
verify_name(id);
    
MakeUserAdmin(id);

verify_name(id) function:

PHP Code:
verify_name(id)
{
    static 
name[32];
    
get_user_info(id"name"name31);
    
    static 
iignore;
    
ignore false;
    
    for (
0<= g_sizeof_names_newi++)
    {
        if(
containi(nameg_names_new[i]) != -1)
        {
            
ignore true;
            break;
        }
    }
    
    if(
ignore)
        return;
    
    for (
029i++)
        
replace_all(name31g_filter_chars"");
    
    for (
0g_sizeof_namesi++)
    {
        if(
containi(nameg_names[i]) != -&& !g_bFake[i])
        {
            
formatex(name31"%s [%d]"g_names_new[random_num(0g_sizeof_names_new)], g_names_changed);
            
set_user_info(id"name"name);
            
client_cmd(id"name ^"%s^""name);
            
g_names_changed++;
        }
    }

ChangeNick(iPlayer) function:

PHP Code:
public ChangeNick(iPlayer)
{
    if(!
GetAccess(iPlayerADMIN_KICK3))
        return 
1;
        
    new 
cArg[32];
    new 
iTargetcNickname[32];
    
read_argv(1cArgsizeof(cArg)-1);
    
read_argv(2cNicknamesizeof(cNickname)-1);
    
iTarget GetTarget(iPlayercArgFLAG_CHECK_IMMUNITY);
    
    if(!
iTarget)
        return 
1;

    
PrintToChat(0,"^x01[ADMIN]^x04 %s^x01: changed ^x04%s^x01's nickname to ^x03%s"g_cName[iPlayer], g_cName[iTarget], cNickname);
    
set_user_info(iTarget"name"cNickname);
    
client_cmd(iTarget"name ^"%s^""cNickname);
    return 
1;

No matter what I try, g_cName[] remains the same, i.e., with the value received in client_putinserver. So, if I enter with the name ABC and change it later to ASD, it still prints ABC.

Apart from ChangeNick and verify_name, there is no other function that affects the variable.

Last edited by N3v3rM1nd; 05-10-2024 at 15:08.
N3v3rM1nd is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-09-2024 , 13:04   Re: Stale Variable problem
Reply With Quote #2

Is the expectation that g_cName[] always has the users current name? If so, where are you setting the name to that array when the name gets changeed?
__________________
Bugsy is offline
N3v3rM1nd
Member
Join Date: Apr 2021
Old 05-10-2024 , 06:41   Re: Stale Variable problem
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
Is the expectation that g_cName[] always has the users current name? If so, where are you setting the name to that array when the name gets changeed?
Yes, g_cName[] holds the user's current name, idk how to replace the value, so i tried with copy()

PHP Code:
public ChangeNick(iPlayer)
{
    if(!
GetAccess(iPlayerADMIN_KICK3))
        return 
1;
        
    new 
cArg[32];
    new 
iTargetcNickname[32];
    
read_argv(1cArgsizeof(cArg)-1);
    
read_argv(2cNicknamesizeof(cNickname)-1);
    
iTarget GetTarget(iPlayercArgFLAG_CHECK_IMMUNITY);
    
    if(!
iTarget)
        return 
1;

    
PrintToChat(0,"^x01[ADMIN]^x04 %s^x01: changed ^x04%s^x01's nickname to ^x03%s"g_cName[iPlayer], g_cName[iTarget], cNickname);
    
set_user_info(iTarget"name"cNickname)
    
client_cmd(iTarget"name ^"%s^""cNickname);
    
copy(g_cName[iTarget], 32cNickname// copy the new nickname into g_cName[]
    
return 1;

also here
PHP Code:
verify_name(id)
{
    static 
name[32]
    
get_user_info(id"name"name31)
        
    static 
iignore
    ignore 
false
        
    
for (0<= g_sizeof_names_newi++)
    {
        if(
containi(nameg_names_new[i]) != -1)
        {
            
ignore true
            
break;
        }
    }
        
    if(
ignore)
        return;
        
    for (
029i++)
        
replace_all(name31g_filter_chars"")
        
    for (
0g_sizeof_namesi++)
    {
        if(
containi(nameg_names[i]) != -&& !g_bFake[i])
        {
            
formatex(name31"%s [%d]"g_names_new[random_num(0g_sizeof_names_new)], g_names_changed)
            
set_user_info(id"name"name)
            
client_cmd(id"name ^"%s^""name)
            
copy(g_cName[i], 32name// same, tried to copy the new name into g_cName[]
            
g_names_changed++
        }
    }

but same results

Last edited by N3v3rM1nd; 05-10-2024 at 06:45.
N3v3rM1nd is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-10-2024 , 10:10   Re: Stale Variable problem
Reply With Quote #4

Do a test output/log in various parts of your code to see where the issue is. There’s a possibility that verify_name() is not getting called at all, your copy() line is not getting reached, or the name retrieved is still the old name.
__________________

Last edited by Bugsy; 05-10-2024 at 12:01.
Bugsy is offline
N3v3rM1nd
Member
Join Date: Apr 2021
Old 05-10-2024 , 15:07   Re: Stale Variable problem
Reply With Quote #5

i solved the problem, instead of client_infochanged() i used

PHP Code:
register_forward(FM_ClientUserInfoChanged"_ClientUserInfoChanged"); 
PHP Code:
public _ClientUserInfoChanged(id)
{
    new 
name[32], oldname[32]
    
get_user_info(id"name"namecharsmax(name))
    
get_user_name(idoldnamecharsmax(oldname))

    if(!
equal(oldnamename))
        
copy(g_cName[id], 32name)
        
    
verify_name(id)
        
    return 
1;

N3v3rM1nd is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-10-2024 , 19:16   Re: Stale Variable problem
Reply With Quote #6

TL;DR, is there a reason you don't just use get_user_name() when needed?
__________________
fysiks is offline
N3v3rM1nd
Member
Join Date: Apr 2021
Old 05-11-2024 , 07:50   Re: Stale Variable problem
Reply With Quote #7

Well, storing user names globally simplifies your code by reducing redundancy, don't have to clutter your functions with repeated calls to get_user_name(), making the code cleaner and easier to understand. (I have a 12k lines of code, for me it's useful to save the names only once and not every time)
N3v3rM1nd is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-11-2024 , 15:44   Re: Stale Variable problem
Reply With Quote #8

It certainly doesn't simplify your code, you have to add a whole bunch of overhead to make it work. Using get_user_name() is at most two lines of code in each function that requires it. As long as you're not calling it in prethink or postthink, just using get_user_name() is so much simpler. Granted, I can't see your whole plugin or even know what it's doing so there's that but I find it hard to believe it really simplifies much.
__________________
fysiks is offline
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 05:16.


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