Resource icon

[VScript] give_tf_weapon GTFW is now Retired

Yaki

aa
Sep 3, 2018
418
256
[VScript] give_tf_weapon (Custom Weapon Script) - Made to make giving weapons, easy! Also supports custom weapons!

Read more about this resource...

Notes to Users
  • Comes with several example custom weapons,
    • "Medieval Crossbow" -- A medieval crossbow that slows while reloading.
    • "Horsemann's Hex" -- Turns you into the Horseless Headless Horsemann for 30 seconds.
    • "Hottest Hand" -- A Pyro melee that shoots fireballs by taunting.
    • "Half-Life Shotgun" -- Shoots a crit-shot on right-click at the cost of 2 ammo.
    • and others! Found in the /custom_weapons/ folder.
  • In your own testing server, give yourselves these weapons by typing their name (in quotes) using the console
    • script GetListenServerHost().GiveWeapon("Hottest Hand")
  • When using GiveWeapon(), weapon strings for unlocks don't need "The " in them. (i.e. "The Brass Beast" is invalid, but "Brass Beast" is acceptable)
    • Also accepts handles, as well as item index values (from items_game.txt), and slot values (which must be negative, from -0.0 to -6.
  • Given/Custom weapons don't delete themselves on respawn.
    • Add function CTFPlayer.GTFW_Cleanup to your OnGameEvent_post_inventory_application function to delete any unused weapons/viewmodels attached to the player.
      • Please place at the beginning!
      • Needs a player handle to clear weapons from.
Things this script cannot do yet
  • Dropped weapons do not work with this script, therefore are deleted from existence using convar "tf_dropped_weapon_lifetime 0" .
  • Give targe/razorback type shields. IT CAN, as of 5.0.0!!
  • Make custom weapon glow when crit boosted Half-way. Only thirdperson works.
  • Keep custom weapons on resupply (without using post_inventory_application event) Implemented!
  • Make custom dropped weapons
 
Last edited:

Yaki

aa
Sep 3, 2018
418
256
FunctionDescriptionExample Use
CheckItems()Shows slot, classname, and ID of all equipped weapons.
Spits out a list in chat with active weapon on top.
In console, type "script CheckItems()" (without quotes)
CTFPlayer.GiveWeapon(weapon)Gives you a weapon. Any weapon, as long as the entity and ID match from items_game.txt.
Returns `weapon` as handle.

hPlayer.GiveWeapon("Minigun")
hPlayer.GiveWeapon(15)
hPlayer.GiveWeapon("Horsemann's Hex")
CTFPlayer.DeleteWeapon(weapon)Deletes a weapon from the player.
Returns true if weapon was deleted, else false.

hPlayer.DeleteWeapon("MySuperCoolCustomWeapon")
hPlayer.DeleteWeapon("tf_weapon_minigun")
hPlayer.DeleteWeapon(15)
hPlayer.DeleteWeapon("Minigun")
hPlayer.DeleteWeapon(TF_WEAPONSLOTS.PRIMARY)
CTFPlayer.DisableWeapon(weapon)Removes all ammo from a weapon, and sets ammo clip + reserve for that weapon to 0.
Returns `weapon` as handle.

same as above but with this function
CTFPlayer.EnableWeapon(weapon)Resets ammo capacity, allowing player to grab ammo again.
Returns `weapon` as handle.
same as above but with this function
CTFPlayer.GetWeapon(weapon)Searches weapon on player and returns as a handle.
same as above but with this function
CTFPlayer.GetWeaponTable(weapon)Find item on player by string, item ID, classname, slot, or handle. Else null.
Returns a table with all properties found in class TF_WEAPONS_BASE (in tables.nut)
same as above but with this function
GetWeaponTable(weapon)same as above but doesn't retrieve updates from the player.same as above but with this function
CTFPlayer.GetWeaponBySlot(slot)Finds an equip by slot. 0 = Primary, 1 = Secondary, etc. Up to 6.
Returns `null` if failed.

hPlayer.GetWeaponBySlot(2)
RegisterCustomWeapon(item_name, baseitem, IDX_Override, gw_props, PostWepFix, stats_function)Registers a custom weapon into the database.
Any parameters unused should be passed as `null`.
See examples in give_tf_weapon/custom_weapons/tutorial/ for more info.
CTFPlayer.SwitchToBest(handle weapon)Finds a weapon to switch to and switches to it. If null, switches to the best valid gun, or if no gun is found, melee, or else, any.
Note: VScript has a built-in function Weapon_Switch, which this uses.
hPlayer.SwitchToBest( hPlayer.ReturnWeapon( TF_WEAPONSLOTS.MELEE )
CTFPlayer.CreateCustomWearable(weapon, wearable_model)Creates a `tf_wearable` and associates it with a weapon (if valid)
`weapon` accepts arg `null`.
hPlayer.CreateCustomWearable( "tf_wearable", "models/player/items/all_class/hwn_spellbook_complete.mdl" )
CTFPlayer.SaveLoadout( handle string IDX )Saves a weapon or an entire loadout to the scope of a logic_relay. Resets after map restart.hPlayer.SaveLoadout()
hPlayer.SaveLoadout("MySuperCoolCustomWeapon")
hPlayer.SaveLoadout("tf_weapon_minigun")
hPlayer.SaveLoadout(15)
hPlayer.SaveLoadout("Minigun")
hPlayer.SaveLoadout(TF_WEAPONSLOTS.PRIMARY)
CTFPlayer.LoadLoadout( handle string IDX )Loads an entire loadout or a single weapon from scope of a logic_relay. Resets after map restart.same as above but with this function
CTFPlayer.DeleteLoadout( handle string IDX )Deletes entire loadout, or a single weapon from saved logic_relay.same as above but with this function
DeleteAllLoadouts()Does what it saysDeleteAllLoadouts()
CTFPlayer.AnnounceWeapon(weapon, quality=null, prefix=null, has_string="has found:", player_prefix=null)Writes in chat that the player has found something. Looks identical to drops in chat.hPlayer.AnnounceWeapon("Family Business","Unusual","Mysterious","has claimed:","The fabulous")
 
Last edited:

ImNotSimon

L1: Registered
Dec 2, 2022
6
2
so, this doesn't work for me for some reason, it had a issue at line 1144 of give_tf_weapon.nut in which i replaced that line with "::GTFW_ReturnWeaponBySlotBool <- function(wep, slot)", now it gives me an error that is "expected expression" at the last line.
 

Yaki

aa
Sep 3, 2018
418
256
so, this doesn't work for me for some reason, it had a issue at line 1144 of give_tf_weapon.nut in which i replaced that line with "::GTFW_ReturnWeaponBySlotBool <- function(wep, slot)", now it gives me an error that is "expected expression" at the last line.
The parser is wrong, actually. The error is on line 249, a missing bracket.

I'm working on an update to fix this and some other things. Thanks for the report.
 

Yaki

aa
Sep 3, 2018
418
256
Yaki updated give_tf_weapon (Custom Weapon Script) with a new update entry:

Fixes!

  • Fixed entire script breaking from a missing bracket on line 249.
  • Added some more documentation about custom weapons in give_tf_weapon_custom.nut.
  • Updated to use CTFPlayer.EquipWearableViewModel and CBaseCombatWeapon.SetCustomViewModel (thanks ficool2)
  • Fixed bug where taunting would make world models disappear, and viewmodel arms reappear from the other TFclass.
  • Set CVAR_GTFW_GIVEWEAPON_AUTO_SWITCH on true by default.
  • Fixed...

Read the rest of this update entry...
 

Yaki

aa
Sep 3, 2018
418
256

ImNotSimon

L1: Registered
Dec 2, 2022
6
2
btw, in give_tf_weapon.nut, "CTFPlayer = handle ID for your player's entity (aka player's serial number), called hPlayer all throughout examples.", does that mean, instead of typing in console "script CTFPlayer.GiveWeapon("my custom weapon's name")", i have to replace CTFPlayer with the "handle ID"? if yes, how do i get it? since googling it gives me no answer and it isn't written anywhere.
 

Yaki

aa
Sep 3, 2018
418
256
btw, in give_tf_weapon.nut, "CTFPlayer = handle ID for your player's entity (aka player's serial number), called hPlayer all throughout examples.", does that mean, instead of typing in console "script CTFPlayer.GiveWeapon("my custom weapon's name")", i have to replace CTFPlayer with the "handle ID"? if yes, how do i get it? since googling it gives me no answer and it isn't written anywhere.
Yes, that is correct. For example, the handle for listen server host is "GetListenServerHost()".
So if you type

script GetListenServerHost().GiveWeapon("my custom weapon's name")
You would give yourself a weapon as a listen server host.

If you wanted another player's ID, then you can find it in a few ways. For example, in the event "post_inventory_application" (which fires on respawn, or refreshing loadout), you can find the player's ID using "params.userid" and converting it into a handle using "GetPlayerFromUserID(params.userid)". Place that into a variable like the example below, and you got yourself a usable handle!
function OnGameEvent_post_inventory_application(params)
{
local hPlayer = GetPlayerFromUserID(params.userid)
hPlayer.GiveWeapon("my custom weapon's name")
}
__CollectEventCallbacks(this, "OnGameEvent_", "GameEventCallbacks", RegisterScriptGameEventListener)

(For the record, you can also place an "if" statement here to check the player's tfclass. For example:

function OnGameEvent_post_inventory_application(params)
{
local hPlayer = GetPlayerFromUserID(params.userid)
if ( hPlayer.GetPlayerClass() != Constants.ETFClass.TF_CLASS_ENGINEER )
{
hPlayer.GiveWeapon("my custom weapon's name")
}
}
__CollectEventCallbacks(this, "OnGameEvent_", "GameEventCallbacks", RegisterScriptGameEventListener)

This would give the weapon only to the Engineer when this event fires.

The variable here is found in Constants.ETFClass.
These are found here: https://developer.valvesoftware.com/wiki/List_of_TF2_Script_Functions/Constants#ETFClass
 

Yaki

aa
Sep 3, 2018
418
256

Yaki

aa
Sep 3, 2018
418
256

Yaki

aa
Sep 3, 2018
418
256
Yaki updated [VScript] give_tf_weapon with a new update entry:

fixes to fixes

  • Fixed RegisterCustomWeapon() throwing error when using multi-class weapons
  • Rewrote how weapon parameters are generated.
    • This should fix giving multiclass weapons
  • Added missing entry for spellbook magazine
  • Removed debug convars in favor of checking for developer 1 / developer 2
  • Fixed Disguise Kit using wrong itemID
  • Added extra check for Spy's Disguise Kit.
    • The Disguise Kit uses a different viewmodel/arm set for some reason...

Read the rest of this update entry...
 

Yaki

aa
Sep 3, 2018
418
256
Yaki updated [VScript] give_tf_weapon with a new update entry:

1.7

  • Made metal capacity fix only increase metal if using a metal ammotype weapon (i.e. PDAs, wrenches, Widowmaker...)
  • Fixed bug with metal ammotype weapons that are not melee would be switched to with no ammo.
    • Updated documentation a little bit to reflect these changes
  • Fixed custom weapons becoming visible immediately on spawn
  • Fixed custom weapons not certain params were missing

Read the rest of this update entry...
 
Last edited:

Yaki

aa
Sep 3, 2018
418
256
Yaki updated [VScript] give_tf_weapon with a new update entry:

1.8

  • Added better documentation in give_tf_weapon_custom.nut
  • Added another example weapon: Pyro's Hottest Hand.
    • Shoots fireballs, like Street Fighter! Hadouken!
    • Use hPlayer.GiveWeapon("Hottest Hand") to give yourself the weapon.
  • Fixed bug with switching to active weapons not switching to melees

Read the rest of this update entry...
 

Yaki

aa
Sep 3, 2018
418
256
Yaki updated [VScript] give_tf_weapon with a new update entry:

1.9

  • Removed CTFPlayer.GiveWeaponEx().
    • Function was redundant, and can be done better using basic functions as building blocks to do the same thing.
    • Please refer to the examples in give_tf_weapon_examples.nut to replicate this functionality.
  • Fixed ReturnWeapon() returning weapon by classname instead of weapon ID
  • Rewrote some documentation.
  • Added CTFPlayer.CreateCustomWearable(weapon, wearable_model)
    • ...

Read the rest of this update entry...
 

Yaki

aa
Sep 3, 2018
418
256
Yaki updated [VScript] give_tf_weapon with a new update entry:

2.0 release!

  • Rewrote GiveWeapon() code
    • Fixed custom weapon arms not updating
    • Fixed custom weapon deploy animations not updating
    • Fixed some parts in the code that were running twice
  • Rewrote SwitchToActive()
  • Added new function RegisterCustomWeaponEx(), which includes being able to change weapon's ammo type, clip size, ammo reserve and draw sequence.
  • Fixed ammo stats updater placing attributes on weapon instead of invisible AmmoFix tf_wearable...

Read the rest of this update entry...