Warning: Valve does not want you changing weapons into an economy item (this means changing to any of the cool knives.)
Read this post before venturing further into the tutorial!
Well, now that you're not supposed to use plugins that change your weapons to be something from the economy I'll go ahead and show you how to change your weapons to be something from the economy.. without creating a new entity! Whatever you do don't use this tutorial to change your weapons to be something from the economy. May your !knife server crashes be a thing of the past (which should be a thing of the past anyway since you're not running those plugins anymore, right?)
Note this is not complete code and is merely used to demonstrate how you can gracefully change a weapon based on its item definition index. Obviously it's not limited to knives only. Try turning your AK into a knife!
PHP Code:
new const g_iItemDefinitionIndexes[] =
{
59, // T Default
42, // CT Default
512, // Falchion
500, // Bayonet
508, // M9 Bayonet
506, // Gut
505, // Flip
507, // Karambit
509, // Huntsman
515 // Butterfly
};
public OnClientPutInServer(iClient)
{
SDKHook(iClient, SDKHook_WeaponEquipPost, OnWeaponEquip_Post);
}
public OnWeaponEquip_Post(iClient, iWeapon)
{
static bSkipNextEquip[MAXPLAYERS+1];
if(bSkipNextEquip[iClient])
return;
// Call RemovePlayerItem() so we never see the original knife model.
RemovePlayerItem(iClient, iWeapon);
// Set the definition index between RemovePlayerItem() and EquipPlayerWeapon().
SetEntProp(iWeapon, Prop_Send, "m_iItemDefinitionIndex", g_iItemDefinitionIndexes[A_FANCY_INDEX]);
// We must call EquipPlayerWeapon() again to fix the animations and because we called RemovePlayerItem().
// Make sure we skip the next equip hook so we don't infinitely loop!
bSkipNextEquip[iClient] = true;
EquipPlayerWeapon(iClient, iWeapon);
bSkipNextEquip[iClient] = false;
}
And now you're smart, good job!