AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   player models bug (https://forums.alliedmods.net/showthread.php?t=332471)

LondoN 05-14-2021 12:30

player models bug
 
Code:

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

#define PLUGIN        "Player Models"
#define VERSION        "1.0"
#define AUTHOR        "LondoN eXtream"

#define MAX_PLAYERS 32

native cs_set_player_model ( iEntity, const szModel [ ] );
native cs_reset_player_model ( iEntity );

enum _:hDBModels
{
        g_hPlayerName [ MAX_PLAYERS ],
        g_hPlayerModel [ MAX_PLAYERS ]
};

new g_hDBModels [ hDBModels ], Array: g_hDBArray;

public plugin_precache ( )
{
        register_plugin ( PLUGIN, VERSION, AUTHOR );

        if ( g_hDBArray )
                ArrayDestroy ( g_hDBArray );

        g_hDBArray = ArrayCreate ( hDBModels );

        new szFile [ 128 ];
        get_configsdir ( szFile, charsmax ( szFile ) );
        formatex ( szFile, charsmax ( szFile ), "%s/PlayerModels.ini", szFile );

        if ( !file_exists ( szFile ) )        set_fail_state ( "File not found" );

        new PlayerModelsFile = fopen ( szFile, "r" );

        if ( PlayerModelsFile )
        {
                new szBuffer [ 256 ], szName [ MAX_PLAYERS ], szModel [ MAX_PLAYERS ];
               
                while ( !feof ( PlayerModelsFile ) )
                {
                        fgets ( PlayerModelsFile, szBuffer, charsmax ( szBuffer ) );
                        trim ( szBuffer );

                        if ( szBuffer [ 0 ] == ';' || ( szBuffer [ 0 ] == '/' && szBuffer [ 1 ] == '/' ) )
                                continue;

                        if ( parse ( szBuffer, szName, charsmax ( szName ), szModel, charsmax ( szModel ) ) < 2 )
                                continue;

                        g_hDBModels [ g_hPlayerName ] = szName;
                        g_hDBModels [ g_hPlayerModel ] = szModel;

                        ArrayPushArray ( g_hDBArray, g_hDBModels );
                }
        }

        fclose ( PlayerModelsFile );

        // Precache Models
        new iSize = ArraySize ( g_hDBArray );
        new ModelFile [ 128 ];
        new ij;

        for ( new i = 0; i < iSize; i++ )
        {
                ArrayGetArray ( g_hDBArray, i, g_hDBModels );

                formatex ( ModelFile, charsmax ( ModelFile ), "models/player/%s/%s.mdl", g_hDBModels [ g_hPlayerModel ], g_hDBModels [ g_hPlayerModel ] );

                log_amx ( "[DEBUG] File : %s", ModelFile );
                ij++;

                if ( file_exists ( ModelFile ) )
                        precache_model ( ModelFile );
        }       

        log_amx ( "[DEBUG] Adding %d models to download table.", ij );
}

public plugin_init ( )
{
        RegisterHam ( Ham_Spawn, "player", "CBase_Spawn_Post", true );
        RegisterHam ( Ham_Killed, "player", "CBase_Killed_Post", true );
}

public CBase_Spawn_Post ( iEntity )
{
        if ( !is_user_alive ( iEntity ) )
                return;

        set_task ( 0.7, "CBase_ModelSet_Player_", iEntity );
}

public CBase_ModelSet_Player_ ( iEntity )
{
        new iSize = ArraySize ( g_hDBArray );
       
        new hName [ 32 ];
        get_user_name ( iEntity, hName, charsmax ( hName ) );

        for ( new i = 0; i < iSize; i++ )
        {
                ArrayGetArray ( g_hDBArray, i, g_hDBModels );

                if ( equal ( hName, g_hDBModels [ g_hPlayerName ] ) )
                {
                        cs_set_user_model ( iEntity, g_hDBModels [ g_hPlayerModel ] );
                        cs_set_player_model ( iEntity, g_hDBModels [ g_hPlayerModel ] );
                        log_amx ( "[DEBUG] Set %s model for client %s", g_hDBModels [ g_hPlayerModel ], hName );
                        break;
                }
                else        log_amx ( "[DEBUG] No model set for client %s", hName );
        }
}

public CBase_Killed_Post ( iEntity )
        cs_reset_player_model ( iEntity );

This is my code
Code:

L 05/14/2021 - 19:25:05: -------- Mapchange to de_dust2 --------
L 05/14/2021 - 19:25:05: [PlayerModels.amxx] [DEBUG] File : models/player/plr_admin/plr_admin.mdl
L 05/14/2021 - 19:25:05: [PlayerModels.amxx] [DEBUG] File : models/player/avh_alien/avh_alien.mdl
L 05/14/2021 - 19:25:05: [PlayerModels.amxx] [DEBUG] Adding 2 models to download table.
[AMXX] Loaded 1 admin from file
Executing AMX Mod X Configuration File
Scrolling message displaying frequency: 10:00 minutes
Menu item 17 added to Menus Front-End: "Plugin Cvars" from plugin "pluginmenu.amxx"
Menu item 18 added to Menus Front-End: "Plugin Commands" from plugin "pluginmenu.amxx"
L 05/14/2021 - 19:25:15: [PlayerModels.amxx] [DEBUG] Set plr_admin model for client LondoN eXtream
Dropped LondoN eXtream from server
Reason:  Client sent 'drop'
192.168.1.139:27005:reconnect
L 05/14/2021 - 19:25:22: [PlayerModels.amxx] [DEBUG] Set plr_admin model for client LondoN eXtream
L 05/14/2021 - 19:25:29: [PlayerModels.amxx] [DEBUG] Set plr_admin model for client LondoN eXtream
Dropped LondoN eXtream from server
Reason:  Timed out

Console debug

But model don't apply to player. Maibe because i test on windows?

Using CS Player Models API and CSTRIKE but no one set the model

Ini file design

Code:

;"name" "model"
"LondoN eXtream" "plr_admin"


LondoN 05-14-2021 12:42

Re: player models bug
 
found the solution

new hModel[32];
formatex(hModel, charsmax(hModel), "%s", g_hDBModels[g_hPlayerModel]);
cs_set_user_model(id, hModel);

this is the solution.

fysiks 05-14-2021 21:56

Re: player models bug
 
Quote:

Originally Posted by LondoN (Post 2746810)
found the solution

new hModel[32];
formatex(hModel, charsmax(hModel), "%s", g_hDBModels[g_hPlayerModel]);
cs_set_user_model(id, hModel);

this is the solution.

Formatting with just "%s" is exactly the same as just using that variable directly (if you are not needing to edit the new string without changing the original string). If you really do need to copy the variable, as-is, use copy().

Also, avoid assigning strings to variables directly except when you declare variable with a default value. In fact, it's completely unnecessary to have those extra variable where you do this anyways. Just use the variables directly in parse:

PHP Code:

parse(szBufferg_hDBModels[g_hPlayerName], charsmax(g_hDBModels[g_hPlayerName]), g_hDBModels[g_hPlayerModel], charsmax(g_hDBModels[g_hPlayerModel])) 

Also, since you are not using g_hDBModels as a global variable, you shouldn't declare it as a global variable. Only declare it in the functions that use it. If it's in a function that is called often, you simply declare it in that function with "static" instead of "new".

Lastly, using MAX_PLAYERS for sizing your string arrays is confusing. You should either just use a number or create another, more aptly named define, to size those arrays. You shouldn't ever need to use that value ever again anyways because you should be using charsmax() anytime that you need to know the max length of a string for that variable or use sizeof() if you need a variable to have the same size as another variable.


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

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