What’s the correct way to run the ActionEquipMostDamagingMelee within a creature’s onspawn event?

I have random weapons spawning with my creatures, but there are some of those creatures I don’t want using ranged weapons, so I’d like to force them to only use melee weapons. I assume the ActionEquipMostDamagingMelee(); is what is needed?

Hm. Judging by the Lexicon page, it won’t enforce melee wielding, and will try ranged weapons next if it can’t find any melee ones. Kinda looks like you just go


with it.

What’s going wrong with this in your setup? Are your NPCs switching to ranged weaponry mid-fight? Are they equipping ranged weapons on spawn? Are they not equipping anything at all?

1 Like

Maybe easier to prevent those creatures from acquiring ranged weapons in the first place.

You can manage that by setting a local integer flag on the creature template for “no ranged”. When that flag is set, choose a random melee weapon rather than a random weapon of any type.

If you don’t want to maintain two lists of weapons, you could keep generating random weapons in a loop until a melee weapon is chosen. See Lexicon comment on GetMeleeWeapon - if you’re using custom base item types, you will need to build a custom function to determine this.


The creatures have melee weapons by default from the editor. When they spawn, I run a script that randomly gives them another weapon (melee or ranged) that drops as loot. I’m ok with most of the creatures using melee or ranged weapons, it adds a nice variety when fighting them.

However, when it comes to my boss fights, I want them to only use the melee weapon they start with. Or if a better melee weapon spawns from the script is better, I want them to use it instead. What’s happening however, is if a ranged weapon spawns on them from the script, they ignore the ActionEquipMostDamagingMelee(); and use the spawned ranged weapon instead of the melee weapon they are given in the toolset.

I’m thinking in my script that runs on their spawn just have it run a ActionEquipItem instead. The script already checks if it’s a melee or ranged weapon. So, maybe in the melee section have it run the ActionEquipItem and if it’s a ranged weapon, just do nothing?

1 Like

Sounds like a plan. :slight_smile: Go for it!

You could check for the enhancement bonus and total amount of item properties on each new melee weapon you spawn while you’re spawning them, replacing the “best” one if the new one has a higher enhancement bonus or more item properties, and equip the “best” one at the end. As long as you’re only spawning them weapons that they’re guaranteed to be able to use (no conflicting usage restrictions, no base item types they can’t use), that should work.

Aren’t actions supposed to be assigned with AssignCommand?

AssignCommand ( oNPC, ClearAllActions ( ) ) ;
AssignCommand ( oNPC, ActionEquipMostDamagingMelee ( ) ) ;

It’s not necessarily to assign actions If the object calling this script (OBJECT_SELF) is the same object that’s supposed to perform these actions. Actions are assigned automatically to OBJECT_SELF by default.

object oNPC = OBJECT_SELF;
AssignCommand(oNPC, ClearAllActions());
AssignCommand(oNPC, ActionEquipMostDamagingMelee());

is equivalent to

1 Like

Ah right, OP even said “with the creatures onspawn event”…

I don’t understand why the creature would simply ignore it. If it equipped the melee weapon as instructed onspawn and then reverted to a ranged weapon in combat, I would assume it had something to do with an AI/combat script that made it default to ranged weapons if no enemies were in melee range.

I have npcs that spawn with random clothes, but some of them are wearing job specific outfits, like a barmaids apron for example. My clothes spawning onspawn script simply checks if they’re wearing something in their clothes slot already before spawning random clothes for them.