Raised This Month: $166 Target: $400
 41% 

[CS:GO] Custom Viewmodel


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CareFully
Junior Member
Join Date: Oct 2015
Old 10-28-2015 , 16:08   [CS:GO] Custom Viewmodel
Reply With Quote #1

PHP Code:
#include <sourcemod>
#include <sdkhooks> 
#include <sdktools>

new g_PVMid[MAXPLAYERS]; // Predicted ViewModel ID's
new g_iSMGModel;    // Custom ViewModel index

public OnPluginStart()
{
    
HookEvent("player_spawn"Event_PlayerSpawn);
}

public 
OnConfigsExecuted()
{
    
g_iSMGModel PrecacheModel("models/weapons/paintball/v_pbgunt.mdl"); // Custom model
}

public 
OnClientPostAdminCheck(client){
    
SDKHook(clientSDKHook_WeaponSwitchPostOnClientWeaponSwitchPost);    
}

public 
OnClientWeaponSwitchPost(clientwpnid)
{
    
    
decl String:szWpn[64];
    
GetEntityClassname(wpnid,szWpn,sizeof(szWpn));
    
    if(
StrEqual(szWpn"weapon_mac10")){
        
SetEntProp(wpnidProp_Send"m_nModelIndex"0);
        
SetEntProp(g_PVMid[client], Prop_Send"m_nModelIndex"g_iSMGModel);
    }
}

public 
Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    
g_PVMid[client] = Weapon_GetViewModelIndex(client, -1);
    


// Thanks to gubka for these 2 functions below.

// Get model index and prevent server from crash
Weapon_GetViewModelIndex(clientsIndex)
{
    while ((
sIndex FindEntityByClassname2(sIndex"predicted_viewmodel")) != -1)
    {
        new 
Owner GetEntPropEnt(sIndexProp_Send"m_hOwner");
        
        if (
Owner != client)
            continue;
        
        return 
sIndex;
    }
    return -
1;
}
// Get entity name
FindEntityByClassname2(sStartEntString:szClassname[])
{
    while (
sStartEnt > -&& !IsValidEntity(sStartEnt)) sStartEnt--;
    return 
FindEntityByClassname(sStartEntszClassname);

I've tinkered around with cs:go trying to get custom viewmodels working without flickering or turning off prediction and after hours and hours I think i finally got something that seems to work.

It might work with other mods aswell, but i haven't tested it.

The custom model must have animations in the same order as the original model, oherwise it's going to play wrong animations.

EDIT: I added a custom model to attachments to test the code. It's a paintball gun converted from cs 1.6 to cs:go. Lighting of the model might be a bit glicthy .
Attached Files
File Type: zip custommac10.zip (436.4 KB, 619 views)

Last edited by CareFully; 11-11-2015 at 12:07. Reason: Optimized code.
CareFully is offline
Franc1sco
Veteran Member
Join Date: Oct 2010
Location: Spain (Madrid)
Old 10-29-2015 , 12:37   Re: [CS:GO] Custom Viewmodel
Reply With Quote #2

I will test this. If this code works well then will be very usefull and more optimized that this code,

Thanks and good job
__________________
Veteran SourceMod Coder -> Activity channel
Taking some paid jobs and selling some private stuff.
Contact: Steam, Telegram or discord ( Franc1sco#7876 ).

You like my work? +Rep in my steam profile comments or donate.


Last edited by Franc1sco; 10-29-2015 at 12:38.
Franc1sco is online now
Send a message via MSN to Franc1sco
Vinnice
Member
Join Date: Sep 2012
Old 10-30-2015 , 07:54   Re: [CS:GO] Custom Viewmodel
Reply With Quote #3

Good job! It's working very well.
But, here's the question: Where can somebody get this kind of custom models?
Vinnice is offline
CareFully
Junior Member
Join Date: Oct 2015
Old 10-30-2015 , 12:22   Re: [CS:GO] Custom Viewmodel
Reply With Quote #4

Yea thats a little problem indeed. Well all source viewmodels should work, but you'll have to probably decompile it and change the animations order. Other way is to convert goldsrc models but thats big hassle to do

And one thing more. The game tries to put weapon skins to the custom models aswell, so here's my fix for that:
PHP Code:
public OnEntityCreated(entid){
    
decl String:szWpn[32];
    
GetEntityClassname(entidszWpnsizeof(szWpn));
    if(
StrEqual(szWpn"weapon_mac10"){
        
SDKHook(entidSDKHook_SpawnOnEntitySpawn);
    }
}
public 
OnEntitySpawn(entid){
    
SetEntProp(entid,Prop_Send,"m_iItemIDLow",0);
    
SetEntProp(entid,Prop_Send,"m_iItemIDHigh",0);

I hope valve is not gonna be mad for blocking skins

Last edited by CareFully; 10-30-2015 at 12:26.
CareFully is offline
gubka
Veteran Member
Join Date: Jan 2012
Location: Dublin
Old 11-01-2015 , 09:28   Re: [CS:GO] Custom Viewmodel
Reply With Quote #5

Good job , I will check it now , because yesterday I wrote new code for changing models with high ping , but method look better , thanks a million, I will use it in my mod
__________________
gubka is offline
Send a message via ICQ to gubka Send a message via Skype™ to gubka
gubka
Veteran Member
Join Date: Jan 2012
Location: Dublin
Old 11-01-2015 , 10:13   Re: [CS:GO] Custom Viewmodel
Reply With Quote #6

It still flippings on custom models, my way better

Quote:
public void ThinkOnThinkPost(int iPlayer)
{
if(!IsPlayerExist(iPlayer))
{
return;
}

if(gZombie[iPlayer])
{
// Check current weapon
char iWeapon[128];
int iItem = GetEntPropEnt(iPlayer, Prop_Send, "m_hActiveWeapon");

GetEdictClassname(iItem, iWeapon, sizeof(iWeapon));

// If weapon is a knife, then allow pickup.
if (StrEqual(iWeapon, "weapon_knife"))
{
int iButton = GetClientButtons(iPlayer);

// Prevent from flipping on CS:GO
if(iButton & IN_ATTACK || iButton & IN_ATTACK2)
SetEntPropFloat(iPlayer, Prop_Data, "m_flNextAttack", 0.0);
else
SetEntPropFloat(iPlayer, Prop_Data, "m_flNextAttack", 999.0);

// Show custom model
Weapon_SetViewModelIndex(iPlayer, "models/zombie/normal_f/hand/hand_zombie_normal_f.mdl");
}
}
}


/ Set ViewModel and check model index
void Weapon_SetViewModelIndex(int sClient, char[] pModel)
{
// Precache model , this is important
int sIndex = PrecacheModel(pModel);

if (sIndex < 1)
{
LogError("Unable to precache model '%s'", pModel);
return;
}

// Get index of model for changing it
pViewModelIndex[sClient] = Weapon_GetViewModelIndex(sClient, -1);

if (pViewModelIndex[sClient] < 1)
{
LogError("Unable to get a viewmodel index");
return;
}

// Create new vars
int iCurrentWeapon[MAXPLAYERS+1];
int iOldSequence[MAXPLAYERS+1];
float iOldCycle[MAXPLAYERS+1];

// Update some offsets
int iActiveWeapon = GetEntPropEnt(sClient, Prop_Send, "m_hActiveWeapon");
int iSequence = GetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nSequence");
float iCycle = GetEntPropFloat(pViewModelIndex[sClient], Prop_Data, "m_flCycle");

// If valid entity , change view_model for client
if (IsValidEdict(iActiveWeapon))
{
if (iActiveWeapon != iCurrentWeapon[sClient])
{
if (iCurrentWeapon[sClient])
{
ShowViewModel(pViewModelIndex[sClient], false);
}

iCurrentWeapon[sClient] = 0;

ShowViewModel(pViewModelIndex[sClient], true);

SetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nModelIndex", sIndex);
ChangeEdictState(pViewModelIndex[sClient], FindDataMapOffs(pViewModelIndex[sClient], "m_nModelIndex"));

iCurrentWeapon[sClient] = iActiveWeapon;
}
}

// If this is current weapon, check cycles and sequenses
if (iCurrentWeapon[sClient])
{
SetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nSequence", GetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nSequence"));
SetEntPropFloat(pViewModelIndex[sClient], Prop_Send, "m_flPlaybackRate", GetEntPropFloat(pViewModelIndex[sClient], Prop_Send, "m_flPlaybackRate"));

if ((iCycle < iOldCycle[sClient]) && (iSequence == iOldSequence[sClient]))
{
SetEntProp(pViewModelIndex[sClient], Prop_Send, "m_nSequence", 0);
}
}

// Update old sequences
iOldSequence[sClient] = iSequence;
iOldCycle[sClient] = iCycle;
}

// Check and change effect of weapon entity
void ShowViewModel(int iViewModel, bool bShow)
{
int iFlags = GetEntProp(iViewModel, Prop_Send, "m_fEffects");

SetEntProp(iViewModel, Prop_Send, "m_fEffects", bShow ? iFlags & ~EF_NODRAW : iFlags | EF_NODRAW);
}

// Get entity name
int FindEntityByClassname2(int sStartEnt, char[] szClassname)
{
while (sStartEnt > -1 && !IsValidEntity(sStartEnt)) sStartEnt--;
return FindEntityByClassname(sStartEnt, szClassname);
}

// Get model index and prevent server from crash
int Weapon_GetViewModelIndex(int sClient, int sIndex)
{
while ((sIndex = FindEntityByClassname2(sIndex, "predicted_viewmodel")) != -1)
{
int Owner = GetEntPropEnt(sIndex, Prop_Send, "m_hOwner");
int ClientWeapon = GetEntPropEnt(sClient, Prop_Send, "m_hActiveWeapon");
int Weapon = GetEntPropEnt(sIndex, Prop_Send, "m_hWeapon");

if (Owner != sClient)
continue;

if (ClientWeapon != Weapon)
continue;

return sIndex;
}
return -1;
}
__________________
gubka is offline
Send a message via ICQ to gubka Send a message via Skype™ to gubka
CareFully
Junior Member
Join Date: Oct 2015
Old 11-01-2015 , 11:28   Re: [CS:GO] Custom Viewmodel
Reply With Quote #7

For me, my way only flickers once and thats when you switch to the weapon.

Your way it flickers each shot for me.

Last edited by CareFully; 11-01-2015 at 13:10.
CareFully is offline
gubka
Veteran Member
Join Date: Jan 2012
Location: Dublin
Old 11-01-2015 , 15:43   Re: [CS:GO] Custom Viewmodel
Reply With Quote #8

I tryied you way, to change knife to zombie claws, and they filckers every sec
__________________
gubka is offline
Send a message via ICQ to gubka Send a message via Skype™ to gubka
Starbish
AlliedModders Donor
Join Date: Oct 2011
Location: South Korea
Old 11-02-2015 , 20:27   Re: [CS:GO] Custom Viewmodel
Reply With Quote #9

Quote:
Originally Posted by gubka View Post
I tryied you way, to change knife to zombie claws, and they filckers every sec
Isn't it already existed as a standalone plugin?
__________________
Starbish is offline
CareFully
Junior Member
Join Date: Oct 2015
Old 11-03-2015 , 12:03   Re: [CS:GO] Custom Viewmodel
Reply With Quote #10

Does the zombie claws model have animations or is it using offical csgo animations?
CareFully 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 18:06.


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