OK here’s the code.
For PCs, CEP has a built in function. In the module OnPlayerEquipItem, add
#include "zep_inc_main"
at the beginning and
ZEPGenderRestrict(oItem,oPC);
as the last line of the main() body of the script.
This forces an instant unequip with a message explaining why.
For companions, it’s not that easy, because no event fires when they equip an item. My solution is to prevent players from giving them the item in the first place, in the module OnAcquireItem event.
You will need zep_inc_main again. At the end of the main() section, add
object oAcquirer = GetModuleItemAcquiredBy();
object oMaster = GetMaster(oAcquirer);
if (!GetIsPC(oAcquirer))
if (GetIsObjectValid(oMaster))
{
int bInvalid = FALSE;
if (GetItemHasItemProperty(oItem, ITEM_PROPERTY_USE_LIMITATION_GENDER))
{
itemproperty ipGenderProperty=GetFirstItemProperty(oItem);
while ((GetIsItemPropertyValid(ipGenderProperty))
&&(GetItemPropertyType(ipGenderProperty) != ITEM_PROPERTY_USE_LIMITATION_GENDER))
{
ipGenderProperty=GetNextItemProperty(oItem);
}
if (GetItemPropertySubType(ipGenderProperty) != GetGender(oAcquirer))
{
bInvalid = TRUE;
}
}
if (bInvalid)
{
AssignCommand(oMaster, zRecoverItem(oAcquirer, oItem));
return;
}
}
The function definition is
// Recover illegal item from companion
void zRecoverItem(object oOwner, object oItem)
{
object oPC = OBJECT_SELF;
ClearAllActions(TRUE);
ActionTakeItem(oItem, oOwner);
ActionDoCommand(OpenInventory(oPC, oPC));
ActionDoCommand(OpenInventory(oOwner, oPC));
SendMessageToPC(oPC, "The " + GetStringLowerCase(GetName(oItem)) + " doesn't fit " + GetName(oOwner));
}
Some similar scripts, including earlier versions of mine, have a loophole when all three inventories (player, companion, merchant) are open simultanously. The code above fixes that.
It can probably be tidied up a bit.