Raised This Month: $51 Target: $400
 12% 

Solved TF2 - Replacing weapons


Post New Thread Reply   
 
Thread Tools Display Modes
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 05-29-2016 , 16:24   Re: TF2 - Replacing weapons
Reply With Quote #11

At this point, the answer is quite clear.
__________________
asherkin is offline
Oshizu
Veteran Member
Join Date: Nov 2012
Location: Warsaw
Old 05-29-2016 , 16:33   Re: TF2 - Replacing weapons
Reply With Quote #12

Jesus guys where did all those outdated snippets come from
__________________
...
Oshizu is offline
luki1412
Veteran Member
Join Date: Oct 2008
Location: OnPluginStart()
Old 05-30-2016 , 06:43   Re: TF2 - Replacing weapons
Reply With Quote #13

Quote:
Originally Posted by asherkin View Post
At this point, the answer is quite clear.
Sadly, yes.
__________________
luki1412 is offline
luki1412
Veteran Member
Join Date: Oct 2008
Location: OnPluginStart()
Old 05-30-2016 , 06:45   Re: TF2 - Replacing weapons
Reply With Quote #14

Quote:
Originally Posted by Oshizu View Post
Jesus guys where did all those outdated snippets come from
Old plugins.
Your post isn't helping either.
__________________
luki1412 is offline
luki1412
Veteran Member
Join Date: Oct 2008
Location: OnPluginStart()
Old 06-15-2016 , 16:27   Re: TF2 - Replacing weapons
Reply With Quote #15

There is a way, actually! Got it to work without tf2items.
__________________
luki1412 is offline
gvngngr
Junior Member
Join Date: Nov 2020
Old 11-28-2020 , 16:55   Re: TF2 - Replacing weapons
Reply With Quote #16

Hello, could you share how you managed to do it, if you still have/remember the code?
gvngngr is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 11-29-2020 , 04:42   Re: TF2 - Replacing weapons
Reply With Quote #17

Quote:
Originally Posted by gvngngr View Post
Hello, could you share how you managed to do it, if you still have/remember the code?
Here's a link to the terrific plugin made by Luki1412 to give weapons to Bots: https://forums.alliedmods.net/showthread.php?p=2453169

Code snippet. Requires a gamedata file for WeaponEquip:
PHP Code:
bool CreateWeapon(int clientchar[] classnameint itemindexint level 0)
{
    
int weapon CreateEntityByName(classname);
    
    if (!
IsValidEntity(weapon))
    {
        return 
false;
    }
    
    
char entclass[64];
    
GetEntityNetClass(weaponentclasssizeof(entclass));
    
SetEntData(weaponFindSendPropInfo(entclass"m_iItemDefinitionIndex"), itemindex);     
    
SetEntData(weaponFindSendPropInfo(entclass"m_bInitialized"), 1);
    
SetEntData(weaponFindSendPropInfo(entclass"m_iEntityQuality"), 6);

    if (
level)
    {
        
SetEntData(weaponFindSendPropInfo(entclass"m_iEntityLevel"), level);
    }
    else
    {
        
SetEntData(weaponFindSendPropInfo(entclass"m_iEntityLevel"), GetRandomUInt(1,99));
    }

    switch (
itemindex)
    {
        case 
810:
        {
            
SetEntData(weaponFindSendPropInfo(entclass"m_iObjectType"), 3);
        }
        case 
998:
        {
            
SetEntData(weaponFindSendPropInfo(entclass"m_nChargeResistType"), GetRandomUInt(0,2));
        }
    }
    
    
DispatchSpawn(weapon);
    
SDKCall(g_hWeaponEquipclientweapon);
    return 
true;

PC Gamer is offline
gvngngr
Junior Member
Join Date: Nov 2020
Old 11-29-2020 , 08:25   Re: TF2 - Replacing weapons
Reply With Quote #18

Quote:
Originally Posted by PC Gamer View Post
Here's a link to the terrific plugin made by Luki1412 to give weapons to Bots: https://forums.alliedmods.net/showthread.php?p=2453169

Code snippet. Requires a gamedata file for WeaponEquip:
PHP Code:
bool CreateWeapon(int clientchar[] classnameint itemindexint level 0)
{
    
int weapon CreateEntityByName(classname);
    
    if (!
IsValidEntity(weapon))
    {
        return 
false;
    }
    
    
char entclass[64];
    
GetEntityNetClass(weaponentclasssizeof(entclass));
    
SetEntData(weaponFindSendPropInfo(entclass"m_iItemDefinitionIndex"), itemindex);     
    
SetEntData(weaponFindSendPropInfo(entclass"m_bInitialized"), 1);
    
SetEntData(weaponFindSendPropInfo(entclass"m_iEntityQuality"), 6);

    if (
level)
    {
        
SetEntData(weaponFindSendPropInfo(entclass"m_iEntityLevel"), level);
    }
    else
    {
        
SetEntData(weaponFindSendPropInfo(entclass"m_iEntityLevel"), GetRandomUInt(1,99));
    }

    switch (
itemindex)
    {
        case 
810:
        {
            
SetEntData(weaponFindSendPropInfo(entclass"m_iObjectType"), 3);
        }
        case 
998:
        {
            
SetEntData(weaponFindSendPropInfo(entclass"m_nChargeResistType"), GetRandomUInt(0,2));
        }
    }
    
    
DispatchSpawn(weapon);
    
SDKCall(g_hWeaponEquipclientweapon);
    return 
true;

Thank you for your reply.
I also saw the plugin afterwards and been trying to implement the relevant parts inside my code.
I am creating this for TF2Classic so it is not possible to use TF2Items. Only base Sourcemod works. That's why I need this one.
But m_bInitialized causes an error. And when I comment it out, there is no error but I don't have the weapon I desire either.

Below is what I have inside the code. I am a new starter in sourcemod coding so I'm sorry if it's poorly written. I'm trying to give the client a Sniper rifle when s/he changes class to Scout. This is not what I am ultimately trying to achieve but it's just a simple example case to make it work as a first step.

PHP Code:
Handle g_hWeaponEquip;
Handle g_hGameConfig;

public 
void OnPluginStart()
{
HookEvent("player_changeclass"Event_PlayerClass);

g_hGameConfig LoadGameConfigFile("give.bots.weapons");
    
    if (!
g_hGameConfig){
    
SetFailState("Failed to find give.bots.weapons.txt gamedata! Can't continue.");
    }
    
    
StartPrepSDKCall(SDKCall_Player);
    
PrepSDKCall_SetFromConf(g_hGameConfigSDKConf_Virtual"WeaponEquip");
    
PrepSDKCall_AddParameter(SDKType_CBaseEntitySDKPass_Pointer);
    
g_hWeaponEquip EndPrepSDKCall();
    
    if (!
g_hWeaponEquip){
    
SetFailState("Failed to prepare the SDKCall for giving weapons. Try updating gamedata or restarting your server.");
    }
}

public 
void Event_PlayerClass(Event event, const char[] namebool dontBroadcast){
    
int iClient GetClientOfUserId(event.GetInt("userid")),
    
iClass event.GetInt("class");
    
if (
iClass==1){
    
PrintToServer("Scout class is selected.");
        
    if(!
g_hWeaponEquip){
    
PrintToServer("g_hWeaponEquip is not valid."); /*just checking*/
    
}
    
    
TF2_RemoveWeaponSlot(iClient0);
    
int weapon CreateEntityByName("tf_weapon_sniperrifle");
    
char entclass[64];
    
GetEntityNetClass(weaponentclasssizeof(entclass));
    
SetEntData(weaponFindSendPropInfo(entclass"m_iItemDefinitionIndex"),14);
    
SetEntData(weaponFindSendPropInfo(entclass"m_bInitialized"),1);
    
DispatchSpawn(weapon);
    
SDKCall(g_hWeaponEquipiClientweapon);
}


This is what I have inside "...\tf2classic\addons\sourcemod\gamedata\giv e.bots.weapons.txt", as created by Luki except the "tf2classic" part:
PHP Code:
"Games"
{
    
/* Team Fortress 2 */
    
"tf2classic" /*I think I tried it with "tf" as well*/
    
{
        
"Offsets"
        
{
            
"WeaponEquip"
            
{
                
"windows"    "264"
                "linux"        "265"
                "mac"        "265"
            
}
            
            
"EquipWearable"
            
{
                
"windows"    "430"
                "linux"        "431"
                "mac"        "431"
            
}
        }
    }

This is the error I get:

And Line 60 is this one: SetEntData(weapon, FindSendPropInfo(entclass, "m_bInitialized"),1); And as I said above, when I remove it, then there is no error, but I don't get a sniper rifle as a scout either. So, I am not sure but this line may be essential for the entity?

I have already deleted some properties like EntityQuality, thinking that it doesnot exist in TF2Classic. But this issue might also be due to TF2Classic itself.

Thanks in advance for any help/advice.

Last edited by gvngngr; 11-29-2020 at 08:32.
gvngngr is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 11-29-2020 , 15:49   Re: TF2 - Replacing weapons
Reply With Quote #19

The code you presented didn't work for me in TF2. The problem might be that the code is fired too early. The code fires when a player selects a class and before the player actually equips their weapons. You could probably test that theory by implementing a timer before removing/equipping weapons.

Alternatively, you could use a different event, like the player inventory event that fires when a player touches a locker or spawns. The following code was tested using player inventory event in TF2 and it works. I hope it helps!

PHP Code:
#include <sourcemod>
#include <tf2_stocks>

Handle g_hWeaponEquip;
Handle g_hGameConfig;

public 
void OnPluginStart()
{
    
HookEvent("post_inventory_application"player_inv);
    
g_hGameConfig LoadGameConfigFile("give.bots.weapons");
    
    if (!
g_hGameConfig){
        
SetFailState("Failed to find give.bots.weapons.txt gamedata! Can't continue.");
    }
    
    
StartPrepSDKCall(SDKCall_Player);
    
PrepSDKCall_SetFromConf(g_hGameConfigSDKConf_Virtual"WeaponEquip");
    
PrepSDKCall_AddParameter(SDKType_CBaseEntitySDKPass_Pointer);
    
g_hWeaponEquip EndPrepSDKCall();
    
    if (!
g_hWeaponEquip){
        
SetFailState("Failed to prepare the SDKCall for giving weapons. Try updating gamedata or restarting your server.");
    }
}

public 
void player_inv(Handle event, const char[] namebool dontBroadcast){
    
    
int userd GetEventInt(event"userid");
    
int iClient GetClientOfUserId(userd);
    
    if (
TF2_GetPlayerClass(iClient) == TFClass_Scout){
        
PrintToServer("%N selected Scout class."iClient);
        
        
TF2_RemoveWeaponSlot(iClient0);

        
int weaponprimary CreateEntityByName("tf_weapon_sniperrifle");
        
char entclass[64];
        
GetEntityNetClass(weaponprimaryentclasssizeof(entclass));
        
SetEntData(weaponprimaryFindSendPropInfo(entclass"m_iItemDefinitionIndex"), 14);     
        
SetEntData(weaponprimaryFindSendPropInfo(entclass"m_bInitialized"), 1);
        
SetEntData(weaponprimaryFindSendPropInfo(entclass"m_iEntityLevel"), 100);
        
SetEntData(weaponprimaryFindSendPropInfo(entclass"m_iEntityQuality"), 5);

        
DispatchSpawn(weaponprimary);
        
SDKCall(g_hWeaponEquipiClientweaponprimary);        
    }



Last edited by PC Gamer; 11-29-2020 at 15:50.
PC Gamer is offline
StrikeR14
AlliedModders Donor
Join Date: Apr 2016
Location: Behind my PC
Old 11-29-2020 , 16:06   Re: TF2 - Replacing weapons
Reply With Quote #20

I liked your solution PC Gamer, you should use SetEntProp with Prop_Send instead of SetEntData. It does not crash servers now.
__________________
Currently taking TF2/CSGO paid private requests!

My Plugins | My Discord Account

Last edited by StrikeR14; 11-29-2020 at 16:26.
StrikeR14 is offline
Reply



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 19:39.


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