AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   General (https://forums.alliedmods.net/forumdisplay.php?f=7)
-   -   get_user_msgid() fails to work on server startup? (https://forums.alliedmods.net/showthread.php?t=308592)

OciXCrom 06-26-2018 15:10

get_user_msgid() fails to work on server startup?
 
I just noticed something weird. I made a simple code to demonstrate.

PHP Code:

#include <amxmodx>

public plugin_precache()
    
log_amx("%i %i %i"get_user_msgid("TeamInfo"), get_user_msgid("Damage"), get_user_msgid("ScreenFade")) 

The output of this code is "0 0 0" on server start.
After I restart the server with the "restart" command or change the map, the output is "86 71 98" as it should be.

I tested both on 1.8.3 latest dev build and 1.8.2. The results are the same. It happens with any message.
However, if I change the event to plugin_init() instead of plugin_precache(), it works normally.

Am I missing something or is this a bug?

edon1337 06-26-2018 15:14

Re: get_user_msgid() fails to work on server startup?
 
Most likely get_user_msgid is assigned it's values after plugin_precache is triggered, that's why you're getting null values.

^SmileY 06-26-2018 18:47

Re: get_user_msgid() fails to work on server startup?
 
Quote:

Originally Posted by edon1337 (Post 2599414)
Most likely get_user_msgid is assigned it's values after plugin_precache is triggered, that's why you're getting null values.

It is during plugin_precache, but you can't get it at plugin_precache.
Also it only return msgId, and do not have an valid reason to work with this on plugin_precache

PRoSToTeM@ 07-30-2018 13:22

Re: get_user_msgid() fails to work on server startup?
 
plugin_precache is useful for hooking WeaponList via register_message which requires get_user_msgid call.

OciXCrom 07-30-2018 13:37

Re: get_user_msgid() fails to work on server startup?
 
Quote:

Originally Posted by PRoSToTeM@ (Post 2607105)
plugin_precache is useful for hooking WeaponList via register_message which requires get_user_msgid call.

Nope:

PHP Code:

#include <amxmodx>

public plugin_precache()
{
    
log_amx("%i"register_message(get_user_msgid("WeaponList"), "TestFunc"))
}

public 
TestFunc()
{
    static 
ii++
    
log_amx("test success %i"i)


Prints 0 on server start.
Prints 1 and "test success" 29 times after server restart or mapchange.

I already submitted a pull request to add a warning for this function in the documentation - https://github.com/alliedmodders/amxmodx/pull/506
Correct me if there's indeed a bug that needs to be fixed.

PRoSToTeM@ 07-30-2018 14:10

Re: get_user_msgid() fails to work on server startup?
 
Quote:

Originally Posted by OciXCrom (Post 2607107)
Nope:

Yeah, I know, I only want to say that there is an application for that, but it doesn't work as expected.
Quote:

Originally Posted by OciXCrom (Post 2607107)
I already submitted a pull request to add a warning for this function in the documentation - https://github.com/alliedmodders/amxmodx/pull/506

That's why I'm here.
Quote:

Originally Posted by OciXCrom (Post 2607107)
Correct me if there's indeed a bug that needs to be fixed.

If register_message will work on startup (with substituted WeaponList msgid constant), then get_user_msgid is bugged. If not, then register_message & get_user_msgid are both bugged.

UPD: register_message works, so only get_user_msgid is bugged.

Arkshine 07-30-2018 14:27

Re: get_user_msgid() fails to work on server startup?
 
Not really a bug I believe. It's just that the game registers such messages in pfnServerActivate.

plugin_precache() is called before, at the first word pfnSpawn. So you can't use get_user_msgid() at this point at the first server start because not cached yet.
plugin_init() is called after in pfnServerActivate as post. It's too late to hook for WeaponList.

One possible solution is to hook FM_RegUserMsg, and hooking WeaponList once the message is registered (In CS, the WeaponList messages are fired right after the message is registered).

Probably not fixable unless we implement a dedicated forward which would be fired once all the messages are registered.

PRoSToTeM@ 07-30-2018 15:00

Re: get_user_msgid() fails to work on server startup?
 
Ye, plugin_precache is called from first spawn (from SV_LoadEntities). And usermsg registering is called from SV_ActivateServer (pfnServerActivate callback). SV_ActivateServer is called after SV_LoadEntities. As @Arkshine said the only and best way is to hook FM_RegUserMsg.

Possibly we can add register_message_lazy which would get usermsg name instead of id and retrieve msgid when the first message is sent.

edon1337 07-30-2018 15:27

Re: get_user_msgid() fails to work on server startup?
 
This didn't work for me
PHP Code:

public plugin_init( )
{
    
register_forwardFM_RegUserMsg"@RegUserMsg" );
}

@
RegUserMsgszMsg[ ], iSize )
{
    
register_messageget_user_msgid"WeaponList" ), "OnMessageWeaponList" );
}

public 
OnMessageWeaponList( )
{
    new 
iAmmoIndex get_msg_arg_int);
    new 
iAmmoCount get_msg_arg_int);
    new 
iWeaponID get_msg_arg_int);
    
    
g_eAmmoIndexiWeaponID ][ AmmoIndex ] = iAmmoIndex;
    
g_eAmmoIndexiWeaponID ][ AmmoCount ] = iAmmoCount;
    
    
log_to_file"Weaponlist.txt""%d %d %d"iAmmoIndexiAmmoCountiWeaponID );


EDIT:
I forgot I had to hook FM_RegUserMsg in plugin_precache, works now.

Arkshine 07-30-2018 15:30

Re: get_user_msgid() fails to work on server startup?
 
You have to check when WeaponList is being registered, then getting the return value.
RegUserMsg is going to be called for all the messages at once.

The game does that essentially:

Spoiler


All times are GMT -4. The time now is 20:49.

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