Difference between revisions of "Command an NPC/Creature to Attack"

no edit summary
imported>Phinix
(A simple way to set up an Attack/Defend menu using OnActivate.)
 
imported>Phinix
 
(12 intermediate revisions by 2 users not shown)
Line 2: Line 2:
[[Category:Scripting]]
[[Category:Scripting]]
This tutorial will explain how to set up a script on a custom NPC/creature so that by activating it you will be able to command it to attack nearby actors (including friendlies.)
This tutorial will explain how to set up a script on a custom NPC/creature so that by activating it you will be able to command it to attack nearby actors (including friendlies.)
*Clipped from my [http://planetelderscrolls.gamespy.com/View.php?view=OblivionMods.Detail&id=601 Phinix Master Summon] mod.


== Create a Global variable ==
== Create a Global variable ==


Start by clicking '''Gameplay''' on the main CS menu, and select '''Globals''' from the menu.
Start by clicking '''Gameplay''' on the main CS menu, and select '''Globals'''.


Right-click the list on the left and select "New."
Right-click the list on the left and select "New."
Line 13: Line 11:
[[Image:Phicommandtut01.jpg]]
[[Image:Phicommandtut01.jpg]]


Give your variable a name. For this guide I’ll use '''globalvar1''' but you'll want to make sure and use something unique for any global you create.
Give your variable a name. For this guide I’ll use '''globalvar1''' but you'll want to make certain and use something unique for any global you create.


You can see it is set as a '''short''' variable by default with a value of "0" which is what we want, so click OK again to close the Globals window.
You can see it gets set as a '''short''' variable with a value of "0" by default, which is what we want. Click OK to close the Globals window.


== Create a Magic Effect script ==
== Create a Magic Effect script ==
Line 28: Line 26:
   
   
  begin ScriptEffectStart
  begin ScriptEffectStart
    SetAV Aggression 50
  end
  end
   
   
  begin ScriptEffectUpdate
  begin ScriptEffectUpdate
    if globalvar1 == 2
if globalvar1 == 1
        SetAV Aggression 10
SetAV Aggression 50
        StopCombatAlarmOnActor
elseif globalvar1 == 2
    endif
SetAV Aggression 10
endif
  end
  end
   
   
  begin ScriptEffectFinish
  begin ScriptEffectFinish
    SetAV Aggression 10
SetAV Aggression 10
  end
  end


Line 54: Line 52:
Now right-click the space on the right and select "New" to bring up the box to add a new spell effect.  
Now right-click the space on the right and select "New" to bring up the box to add a new spell effect.  


For the effect choose "Script Effect" and give it a duration (I use 360 but it doesn’t really matter since in this case it will stay on until it’s dismissed.)
For the effect choose "Script Effect" and give it a duration of 0, since we're adding it as an Ability which remains constant.


Give it an "Effect Name," then choose any school and visual. Do not check "Effect is Hostile."
Give it an "Effect Name," then choose any school and visual. Do not check "Effect is Hostile."
Line 61: Line 59:


[[Image:Phicommandtut03.jpg]]
[[Image:Phicommandtut03.jpg]]
'''NOTE:''' Left this out before; it is very important you be sure to put this spell in the spell list of the NPC/Creature you want to be able to command.
Do this by first double-clicking the MPC/character in the CS to open their stats sheet. Click the '''SpellList''' tab.
Now go back to the Spell branch of the Object Window and find your Frenzy spell you just made, and drag it into the NPC/Creature's spell list.


== Create the Object script for your NPC ==
== Create the Object script for your NPC ==


Now this next bit needs to be in an '''Object''' type script on the character you want to be able to command. You can replace the names of the short and float variables listed at the top to be whatever you wish. Same goes for the script name:  
Follow the same steps as above to create another new script, only make this one an '''Object''' type. You can replace the names of the short and float variables listed at the top to be whatever you wish. Same goes for the script name.
 
Once created this script will need to go on the NPC/creature you want to command:


  ScriptName MyNPCScript
  ScriptName MyNPCScript
Line 70: Line 76:
  short timerinit
  short timerinit
  short activestat
  short activestat
short button
  float timer
  float timer
   
   
Line 90: Line 97:
  set activestat to 0
  set activestat to 0
  elseif button == 1
  elseif button == 1
set timer to 1.5
  set globalvar1 to 2
  set globalvar1 to 2
  set activestat to 0
  set activestat to 0
Line 96: Line 104:
  endif
  endif
  endif
  endif
endif
if globalvar1 == 0
set timerinit to 0
endif
if globalvar1 == 1
addspell myFrenzySpell
set globalvar1 to 0
  endif
  endif
  if globalvar1 == 2
  if globalvar1 == 2
  if timerinit == 0
  if timer == 0
  set timer to .5
  set globalvar1 to 0
set timerinit to 1
  elseif timer > 0
  else
set timer to timer - getSecondsPassed
if timer > 0
  StopCombatAlarmOnActor
set timer to timer - getSecondsPassed
elseif timer < 0
  else
set timer to 0
removespell myFrenzySpell
StopCombatAlarmOnActor
set globalvar1 to 0
endif
  endif
  endif
  endif
  endif
Line 121: Line 118:


That should do it. I set my summons to have '''aggression''' at 10 in their AI setup by default. That way they’ll fight when attacked but won’t go after friendlies unless you command them to.
That should do it. I set my summons to have '''aggression''' at 10 in their AI setup by default. That way they’ll fight when attacked but won’t go after friendlies unless you command them to.
'''UPDATE:''' The reason I use the timer for setting the defensive state to 2 and then back to 0 (waiting state) is so that if multiple NPC/Creatures are watching this same global variable status to change their hostility (using the Ability script we added), we want to give the game a few frames to think about it so all of them get the message.
This is something I have discovered in troubleshooting some issues with actor scripts seemingly losing the ability to effect the global through their menu. I believe this has been resolved by adding the Ability to their spell list directly in the editor rather than attempting to do so on-the-fly via additional scripting. This method eliminates some unnecessary complexity, and seems to work better. Your mileage may vary.


Best of luck!
Best of luck!
-Phinix
Anonymous user