As it seems, not everybody knows that this function returns a pointer that you can use to gain control of the created weapon.
Syntax: Select all
from commands.typed import TypedSayCommand
from memory import make_object
from messages import SayText2
from players.entity import Player
from weapons.entity import Weapon
@TypedSayCommand('!gimme')
def cmd_on_gimme(command_info, weapon_name):
player = Player(command_info.index)
weapon = make_object(Weapon, player.give_named_item(weapon_name))
SayText2(f"Given {weapon.class_name} to {player.name}").send()
Approach #2. Weapon.create
When you create the entity by youself, obviously you have the reference to it, but the question is completely the opposite: how to give this weapon to a player. This is done by telling the player to touch a weapon. Doesn't even matter where the weapon was spawned.
Syntax: Select all
from commands.typed import TypedSayCommand
from messages import SayText2
from players.entity import Player
from weapons.entity import Weapon
@TypedSayCommand('!gimme')
def cmd_on_gimme(command_info, weapon_name):
player = Player(command_info.index)
weapon = Weapon.create(weapon_name)
weapon.spawn()
player.touch(weapon.pointer)
SayText2(f"Given {weapon.class_name} to {player.name}").send()
Now some information about CS:GO. As you may know, CS:GO introduced some weapons that share the classname (USP-S and HKP2000, for example, are both weapon_hkp2000). The difference between such weapons lies in their properties.
For your convenience, Source.Python's Weapon.create converts aliases (say, weapon_usp_silencer) to the real classname and assigns correct properties to the created entity to make sure that the resulting weapon will be USP-S and not HKP2000.
Well, at least it tries to do so. From my testing,
Syntax: Select all
Weapon.create('weapon_usp_silencer')
still spawns HKP2000, but if you drop it and look at it, the hint will caption it as "USP-S".
What about give_named_item then? It's a native Source function and it should do all the magic by itself. And it successfully recognizes the weapon_usp_silencer alias. The only problem is that this function is too good. Nobody asks for it, but the function will look up the player's inventory to see what weapon - USP or HKP2000 - the player has equipped. Then... it just ignores the alias and spawns the one it thinks "correct". I.e. it can spawn USP-S when you wanted it to spawn HKP2000 and vice versa.
How to prevent it? The inventory lookup only succeeds if the player is on the correct team. Remember that USP/HKP2000 can only be bought by Counter-Terrorists? Then when the player is on the Terrorists team, give_named_item will spawn the weapon that we ask for.
The following code:
Syntax: Select all
entity = Entity(player.index)
old_team = entity.team
entity.team = 2 # Terrorists
player.give_named_item('weapon_hkp2000')
entity.team = old_team
basically saves player's current team, moves them to terrorists, gives the correct weapon and then restores their team.
You might ask, why won't I use player.team? Because player.team is a shortcut to player.playerinfo.team, and changing the team using this shortcut will kill the player in process.
entity.team, on the other side, is more raw and allows us to change player's team without any casualties.