MSFD Making NPCs switch between spell "sets"
If you want an NPC to switch between different sets of spells, the skill-setting method will not work unless the spells are grouped into different schools (which is unlikely to be the case).
and removing spells will only take effect when the NPC is not in combat, but StopCombat will stop combat for all actors involved (usually not a good idea).
However, other AI commands can be used to stop combat for a single character.
This method is more useful for followers of the player than for hostile NPCs as the character will exit combat, however briefly: since the enemy will generally attack the player rather than a follower, temporarily removing a follower from combat is unlikely to cause problems; using AIFollow to remove the follower from combat ensures that the NPC will immediately re-enter combat at the first hit by or on the player. AIWander (and probably other AI commands) will also work, but you may need to restart combat using StartCombat afterwards.
The following example is an abbreviated snippet from the local script of a companion I'm working on.
Most of the time the spell-switching is barely noticeable (just a moment of hesitation), and the companion quickly re-enters combat.
;CombatStyle is set from dialogue when the player gives combat orders
if ( CombatStyle == 1 ) ;switch between targeted and touch spells
if ( player->GetSpellReadied == 1 )
set CombatMage to 1
elseif ( player->GetWeaponType >= 9 )
set CombatMage to 1
elseif ( player->GetWeaponType <= 8 )
set CombatMage to 2
endif
elseif ( CombatStyle == 2 ) ;switch between targeted spells and weapons
if ( player->GetSpellReadied == 1 )
set CombatMage to 1
elseif ( player->GetWeaponType >= 9 )
set CombatMage to 1
elseif ( player->GetWeaponType <= 8 )
set CombatMage to 3
endif
elseif ( CombatStyle >= 3 ) ;no combat magic
set CombatMage to 3
elseif ( CombatStyle == 0 ) ;AI choose
set CombatMage to 0
endif
if ( CombatMage != MageState ) ;MageState is a doonce for spell switching
if ( MageState != 3 ) ;don't remove spells that aren't there
if ( CombatMage != 0 ) ;don't remove spells just to add them back
;remove unwanted spells here
endif
endif
if ( CombatMage == 0 )
;add all combat spells here
elseif ( CombatMage == 1 )
;add targeted spells here
elseif ( CombatMage == 2 )
;add touch spells here
endif
set MageState to CombatMage
AIFollow player 0 0 0 0 ;this ensures that changes take effect immediately
endif