Quote:
Originally Posted by StrikeR14
... is the weapon's entity index, not the weapon's definition index.
|
Ah... that makes sense, but I was hoping for a method of creating a weapon that didn't require TF2Items.
Here's some good news I found after digging through the gamedata files. A regular SourceMod installation provides the WeaponEquip offset in game.tf.txt file located in the gamedata/sdktools.games directory. I wasn't aware of that. That means you can create a weapon without having to make your own gamedata file.
Here's a working example of creating a weapon without TF2Items or making your own gamedata file:
PHP Code:
#pragma semicolon 1
#include <sourcemod>
#include <tf2_stocks>
#pragma newdecls required
#define PLUGIN_VERSION "1.0"
public Plugin myinfo =
{
name = "[TF2] Chopper",
author = "PC Gamer",
description = "Test of giving weapon",
version = PLUGIN_VERSION,
url = "www.sourcemod.com"
}
Handle g_hWeaponEquip;
public void OnPluginStart()
{
RegAdminCmd("sm_chop", Command_Chop, ADMFLAG_SLAY, "Give Player a Weapon");
GameData hTF2sdk = new GameData("/sdktools.games/game.tf"); // sdktools tf2 gamdata
if (!hTF2sdk)
SetFailState("This plugin is designed for a TF2 dedicated server only.");
StartPrepSDKCall(SDKCall_Player);
PrepSDKCall_SetVirtual(hTF2sdk.GetOffset("WeaponEquip"));
PrepSDKCall_AddParameter(SDKType_CBaseEntity, SDKPass_Pointer);
g_hWeaponEquip = EndPrepSDKCall();
if (!g_hWeaponEquip)
SetFailState("Failed to create call: WeaponEquip. SourceMod version may be outdated.");
delete hTF2sdk;
}
Action Command_Chop(int client, int args)
{
char arg1[32];
if (args < 1)
{
arg1 = "@me";
}
else GetCmdArg(1, arg1, sizeof(arg1));
char target_name[MAX_TARGET_LENGTH];
int target_list[MAXPLAYERS], target_count;
bool tn_is_ml;
if ((target_count = ProcessTargetString(
arg1,
client,
target_list,
MAXPLAYERS,
COMMAND_FILTER_ALIVE|(args < 1 ? COMMAND_FILTER_NO_IMMUNITY : 0),
target_name,
sizeof(target_name),
tn_is_ml)) <= 0)
{
ReplyToTargetError(client, target_count);
return Plugin_Handled;
}
for (int i = 0; i < target_count; i++)
{
CreateWeapon(target_list[i], "tf_weapon_sword", 132, 3, 2); //Eyelander
PrintToChat(target_list[i], "You have a new Weapon!");
PrintToChat(target_list[i], "You will lose this weapon when you touch a locker or die.");
}
return Plugin_Handled;
}
bool CreateWeapon(int client, char[] classname, int itemindex, int quality, int slot)
{
int weapon = CreateEntityByName(classname);
if (!IsValidEntity(weapon))
{
return false;
}
char entclass[64];
GetEntityNetClass(weapon, entclass, sizeof(entclass));
SetEntProp(weapon, Prop_Send, "m_iItemDefinitionIndex", itemindex);
SetEntProp(weapon, Prop_Send, "m_bInitialized", 1);
SetEntData(weapon, FindSendPropInfo(entclass, "m_iEntityQuality"), quality);
SetEntProp(weapon, Prop_Send, "m_iEntityLevel", GetRandomInt(1,99));
TF2_RemoveWeaponSlot(client, slot);
DispatchSpawn(weapon);
SDKCall(g_hWeaponEquip, client, weapon);
return true;
}