Best way to give/remove and select weapon in code for a pawn

Discussions about Coding and Scripting
Posts: 198
Joined: Thu Apr 16, 2020 3:23 pm
Personal rank: GameDev

Best way to give/remove and select weapon in code for a pawn

Post by 1337GameDev »

I have some code that I want to execute on a given pawn, and a given weapon reference.

I want to achieve a few things:

1. Given a new instance of a weapon that has been spawned, give it to a pawn. I have tried

Code: Select all

Weapon.GiveTo(Pawn P)
and it does seem to work, but I'm unsure how to then select that weapon (regardless of priority / self rating).

2. Given a pawn, and a weapon class, remove it from the given Pawn, and select a new weapon.
I currently do this:

Code: Select all

Pawn.DeleteInventory(wep);//but this also only takes an instance, currently I find an instance using Pawn.FindInventoryType(wep.Class) and pass to this
3. Give a weapon to Pawn that was removed from another Pawn, and select it:

Code: Select all

//I used help from:

local Weapon existing;
Pawn.DeleteInventory(wep);//but this also only takes an instance

//below copied from Pawn.ChangedWeapon with minor changes inspired from PlayerPawn.SwitchWeapon, and DeathMatchPlus.GiveWeapon
if((p == None) || (wep == None)){

existing = Weapon(p.FindInventoryType(wep.Class));
if(existing == None){

p.PendingWeapon = wep;

//if we are switching to the currently equipped weapon, then cancel the switch
if(p.PendingWeapon == p.Weapon){
	p.PendingWeapon = None;

//if we cancelled the switch, or we are switching to nothing, then just return
if(p.PendingWeapon == None){

//we know past this point that we arent switching to the same weapon as we have selected
if(p.Weapon != None){


if((p.PendingWeapon != None) && (p.PendingWeapon.Mass > 20) && (p.carriedDecoration != None)){

p.Weapon = p.PendingWeapon;
p.Inventory.ChangedWeapon(); // tell inventory that weapon changed (in case any effect was being applied)


if ((p.Level.Game != None) && (p.Level.Game.Difficulty > 1)){
	p.MakeNoise(0.1 * p.Level.Game.Difficulty);

p.PendingWeapon = None;
I'm not sure of the best method for this to retain proper player AND generic pawn game state. Using the above final method switches.... but triggers the raise up animation twice for some reason.

What's the best way? My ultimate goal is to "swap" weapons of 2 given pawns, and any that have a "none" weapon, will equip the best weapon they can (eg:

Code: Select all


User avatar
Posts: 5477
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Best way to give/remove and select weapon in code for a pawn

Post by Feralidragon »

Back when I did NW3 I spent a while creating a mutator that would both replace and be able to give weapons to any kind of pawn (player, bot, monster, ...) without any issues, since the "standard" way of doing it is broken beyond repair, it simply doesn't work well or at all.

What I have come up with is with the type of mutator I am linking here (inside the zip there's a single .uc file):
This class does a whole lot of stuff that doesn't really matter for your case, so the specific function to check is GiveWeaponByClass, it should give you a better idea of what you have to do to properly give a weapon to a pawn.

I gave you the entire class rather than the needed excerpt, because this function has some calls to other functions inside it, some which are not really relevant for you (NW3-specific stuff), others which may be, hence linking you to the entire file instead so you can check it by yourself.
You do not have the required permissions to view the files attached to this post.