Recharging weapons with scripts?

Post » Sun Nov 18, 2012 8:16 am

Someone requested that I add a feature to my mod where NPCs automatically recharge staffs and weapons with any local soul gems, however after several hours I haven't been able to find any information on how to do this programatically.

After hitting dead ends trying to do it correctly, I started looking for alternative options. Like giving all followers the Soul Siphon perk (or a custom perk) that kept their weapons fresh. But then, the Creation Kit wiki reports the AddPerk method is broke.

So now I am considering more drastic kludges like counting fights or hits and after some number, if the NPC has a soul gem, re-spawn the equipped weapon if it is enchanted... but I dont think that will work for custom items and personal upgrades would probably be lost.

Anyone know of a better way to:

1) Detect how many charges are remaining on a weapon
2) (Partially) Recharge the weapon, preferably with a soul gem
User avatar
Leah
 
Posts: 3358
Joined: Wed Nov 01, 2006 3:11 pm

Post » Sun Nov 18, 2012 2:56 am

Thought I would give an update, though there are no replies..

So... If I add an OnObjectEquipped/OnObjectUnEquipped handler a follower alias, both the items and enchantments get passed into the method when a follower equips/unequips a weapon. It IS called "Object" and not "Item", so I guess this shouldn't surprise me. However this seems to only be true for hostile enchmaents (you know... the ones that require charges). Furthermore, if the weapon is depleted, the enchmaent is still attached/received. So I can detect if they are using an enchanted weapon with or without SKSE, however either way, I have no idea how many charges the weapon has.

Second thing... I can "recharge" the item by RemoveItem(form) follower by AddItem(Form). So I made a prototype system that counted the number of enemies killed by the NPC and after every 15 kills, checked the inventory for soul gems and if enough were found, removed them and respawned+equipped the enchanted weapon.... thus recharging it.

It worked well for items like Grimsever, but ... As soon as I handed my NPCs crafted items with custom names and enchantments, the items were reset to their dull, unenchanted bases upon respawn. If there was a way to detect if the item was crafted, I would even consider making it only work for non-crafted items. But as far as I know, that isn't possible either.

Now... This system COULD WORK for a mod that only cares about recharging staffs. It just so happens that Vanilla skyrim does not allow people to "enchant" staffs. So you dont have to worry about accidently resetting a staff when you respawn it. Furthermore, a mod that only cares about staffs wouldn't need event handlers to detect if the item is enchanted (As all staffs are basically enchanted). They could just call GetEquippedItemType after each kill to see if the follower is holding a staff. If so, add one to the kill counter and after so many, check for soul gems and possibly respawn the staff.

For now, I am going to skip it because if I only recharge staffs and not all weapons, people will think it is a bug. But I thought I would post my findings for someone else.
User avatar
Charlotte Buckley
 
Posts: 3532
Joined: Fri Oct 27, 2006 11:29 am

Post » Sat Nov 17, 2012 8:12 pm

It is an old topic, but I figured out the solution to this one. Thought I would update for other authors.

Seems there is a not-so-documented actor value called "rightitemcharge" and "leftitemcharge".

You can use GetActorValue() to see the current number of charges. You can also restore the charges using SetAV

Now, when an echanted item leaves the Players inventory, its current number of charges becomes its base value. I think this is a bug, because you can still charge the weapon with the GUI and it will go up to the weapons actual max. Unfortunately, this bug means that GetAVPercentage always reports 100% (1.0). So unfortunately, you can't really detect what percentage of charges remain or even how many charges it would actually take to recharge the weapon.

One can get an idea if the item is exclusively held by the player. But even then, the base value will be the number of charges when the item entered the players inventory.

There also isn't an easy way to verify if an item is enchanted. If GetAV("rightitemcharge") reports non-zero... great, it is enchanted. But if it reports 0, it is possible that it is enchanted and simply out of charges.

Still, this does allow limited item charging. Instead of using percentages, you have to just hard code a number (say 250). If Get AV is less than 250 (but greater than 1), charge the weapon. So how much should you charge it? Well, that is the other issue. Luckily if you use SetAV with an unreasonable number, it will come back down to the weapons actual limit the next time it is actually used.

Which brings us to the final issue of how many soul gems to consume when you dont know what the cap is. Basically, I just came up with a general algorithm that may not get it perfect, but then, charging is rarely perfect anyway. My algorithm weights each of the gems:

petty : 250 charges
lesser : 500 charges (equiv to 2 petty)
common : 1000 charges (equiv to 4 petty)
greater : 2000 charges (equiv to 8 petty)
grand : 4000 charges. (equiv to 16 petty)

I search for/remove gems from least powerful to most powerful until I have an equivalent of 12 petty gems. If I reach 12, I stop and set the av to 10000. If, after consuming every gem I have less than 12, I give them an equivalent number of charges (numpetty * 250).
User avatar
Bloomer
 
Posts: 3435
Joined: Sun May 27, 2007 9:23 pm


Return to V - Skyrim