Raised This Month: $32 Target: $400
 8% 

Improve the code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
undead52
Senior Member
Join Date: Dec 2016
Old 04-18-2019 , 07:57   Improve the code
Reply With Quote #1

The code works fine, but feels hardcoded, i still have problems with arrays, like if i dont -1 at random numbers i get default models.
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <cstrike>

#define VERSION "1.0" 
#define PLUGIN "Models"
#define AUTHOR "Artorias"

new const g_szModels[64][64];

new 
g_szLine;

public 
plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
register_event("ResetHUD""resetModel""bf")
}

public 
plugin_precache() {

    new 
path[128];
    new 
szLineData[33];
    
get_configsdir(pathcharsmax(path))
    
    
format(pathcharsmax(path), "%s/models.ini"path)
    
    if(!
file_exists(path)){
        
log_amx("Error: file does not exist (%s)."path)
        return
    }

    new 
file fopen(path"rt")
    if(!
file){
        
log_amx("Error: Could not open file (%s)."path)
        return
    }
    
    while(!
feof(file))
    {
        
fgets(fileszLineDatacharsmax(szLineData))
            
        
replace(szLineDatacharsmax(szLineData), "^n""")
            
        if (
szLineData[0] == ';' || !szLineData[0] || equal(szLineData[0],"")) continue
            
        
g_szModels[g_szLine] = szLineData
            
        g_szLine
++
    }
    
    
fclose(file)
    
    for(new 
0<= g_szLine;i++)
    {
        if(
equal(g_szModels[i],""))
            continue;
        
        
formatex(pathcharsmax(path), "models/player/%s/%s.mdl"g_szModels[i], g_szModels[i]);
        
precache_model(path);
        
formatex(pathcharsmax(path), "models/player/%s/%sT.mdl"g_szModels[i], g_szModels[i]);
        if(
file_exists(path))
            
precache_model(path);
    }
}

public 
resetModel(idlevelcid) {
    new 
rand random_num(0g_szLine 1);
    if (
cs_get_user_team(id) == CS_TEAM_T && is_user_connected(id)){ 
        
cs_set_user_model(idg_szModels[rand]); 
    }


Last edited by undead52; 04-18-2019 at 10:09. Reason: Updated code
undead52 is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 04-18-2019 , 09:59   Re: Improve the code
Reply With Quote #2

The first step to improving it is putting it in a [php] tag.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
undead52
Senior Member
Join Date: Dec 2016
Old 04-18-2019 , 10:10   Re: Improve the code
Reply With Quote #3

Deleted php tag by mistake while editing it.

Last edited by undead52; 04-18-2019 at 10:15.
undead52 is offline
Reply


Thread Tools
Display Modes

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 22:24.


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