we were talking about how to add perks to all actors without the need to manually attach them to every single actor using the CK. Unfortunately, I still haven't found a way to get that to work, but using a method discussed in that thread (thanks to kuertee for his ideas!) I DID manage to dynamically add Papyrus scripts to all actors near the player (and they'll be remove again when the actors are dead, but that can be prevented as well if necessary). This same procedure should also work for adding items, spells and factions because these fields are also available in quest aliases, but unfortunately there is no place in quest aliases to add perks.
<<<<<<<<<<<<<<<<
- Requirements: 2 Quests, let's call them ''UpdateQuest'' and ''AliasesQuest''.
- First we create AliasesQuest, with a priority 99, Start Game Enabled (not necessary but probably best)
- For testing I added 3 Quest Aliases, but for a final version this should probably be changed to 10 or 15 or something, depending on how many actors you want to support your functionality for in your mod.
- The first Quest alias for my testing was PlayerAlias, with as Fill Type ''specific reference'' (and then go to (any cell) and pick PlayerRef) and a Papyrus Script attached (I wanted the player to have a different script from all other actors). (THIS IS A REFERENCE ALIAS, NOT A LOCATION ALIAS)
- Now we create our second Alias, (ALSO REFERENCE ALIAS) named CombatAliasNPC1. Fill type ''Find Matching Reference'' with ''In Loaded Area'' checked (because we don't need to attach our script to an NPC in solitude if the player is in whiterun), and ''Closest'' checked (because I'm making a combat mod, and therefore the closest NPCs are more important to actually get a script than NPCs further away).
Also (VERY IMPORTANT!) check the ''Optional'' flag. If this isn't checked, the quest will fail to update aliases when there are no living NPCs nearby. If scripts also need to be added to dead actors, check the ''Allow Dead'' flag, but I left it unchecked because this was unncessary for me.
We have one condition in ''Match Conditions'', which is ''IsActor == 1'' (running on Subject), because we don't want our script to be attached to for example a chair which happens to be nearby.
And finally, we attach a second Papyrus script (different from the player, because I WANT it to be different). For testing purposes, this is a very simple script:
Scriptname CombatAliasNPCScript extends ReferenceAlias Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked) Debug.MessageBox("This actor is quest alias!")EndEvent
- Back in the quest aliases tab, right click the CombatAliasNPC1 and click ''Duplicate''. Change the name of this alias to CombatAliasNPC2 and leave the rest as it is. Even though both NPC aliases have the ''Closest'' flag checked (which means they are filled with the closest NPC), the quest will still fill TWO NPCs (as opposed to filling both aliases with the same, closest NPC) because neither alias has the ''Allow Reuse in Quest'' flag checked.
- Click ok in the Quest Editor, because this quest is finished now. And then create the second quest, which we'll name ''UpdateQuest''. Update Quest also has a priority of 99 and this one MUST have ''Start Game Enabled'' checked.
- In the Quest Stages tab, create a new quest stage (0), create a new Log Entry (leaving it empty). Tick the ''Start Up Stage'' flag for this quest and add one line to the Papyrus Fragment:
RegisterForUpdate(5)
-Don't forget to click compile after adding this line of code. The ''5'' is the amount of seconds it takes to update. A smaller amount of seconds means the game will attach scripts faster when entering new areas or when actors near the player get summoned, but it also means scripts have to run more often. So you need to make a decision here: Is it more important to update scripts as soon as possible (use a low number) or is it more important that this mod keeps game performance as high as possible (use a high number). Generally the performance impact shouldn't be noticeable because only a couple lines of code are executed on each update, but it's still something to take into consideration.
- Go to the Scripts tab in this quest and attach a new script. This is the code I used in this script for testing:
Scriptname UpdateQuestScript extends Quest Quest property AliasesQuest autoEvent OnUpdate() AliasesQuest.Stop() Utility.Wait(0.5) AliasesQuest.Start()EndEvent
- Compile the script and don't forget to attach the AliasesQuest as a property to the script. I'm not entirely sure whether the ''Utility.Wait(0.5)'' line was necessary or not, didn't try without it, but it shouldn't hurt anyway. Now because of the Papyrus fragment in the startup stage this quest will update every 5 seconds, and every time it updates it will stop the AliasesQuest (clearing it's aliases and therefore removing all the scripts from actors again) and 0.5 seconds later start the quest again (allowing it to generate new Aliases in case the player is now in a different location or new actors have entered the scene through for example summons).
<<<<<<<<<<<<<<<<
Now I went on to test the mod. My last saved game happened to be in a place where at least one dead enemy was near me and my companion was near me. Hitting the dead enemy didn't produce any messageboxes (so he didn't have the script on him) and hitting my companion did (so she did have the script). Killing my companion and then hitting her body showed that messageboxes no longer appeared (might have to wait up to 5 seconds for the updating though). Resurrecting the dead bandit using the console and hitting him produced messageboxes, so he had received the script after getting resurrected. As a last test I used the console to resurrect both my companion AND the bandit, and they both generated messageboxes after hitting them, so they both had the script attached correctly (more NPCs wouldn't have worked because I used 2 NPC aliases for testing).