Jump to content

[LE] Adding and removing armor with papyrus script


gba25

Recommended Posts

Hello,

 

I am trying to create spell which conjures armor and equips it to the player. I created magic effect with the following script:

Scriptname conjureArmorScript extends ActiveMagicEffect  

Armor Property bodyarmor Auto
Armor Property gauntlets Auto
Armor Property boots Auto
Armor Property helmet Auto
Armor[] Property oldArmor  Auto  

Event OnEffectStart(Actor akTarget, Actor akCaster)
    Actor player = akCaster
    ;saving old armor for equipping after effect is finished;
    oldArmor[0] = player.GetWornForm(0x00000004) as Armor 
    oldArmor[1] = player.GetWornForm(0x00000008) as Armor
    oldArmor[2] = player.GetWornForm(0x00000080) as Armor
    oldArmor[3] = player.GetWornForm(0x00000001) as Armor

    ;adding new armor;
    player.Additem(bodyarmor,1)
    player.Additem(gauntlets,1)
    player.Additem(boots,1)
    player.Additem(helmet,1)
    
    ;equip new armor;
    player.EquipItem(bodyarmor,false,true)
    player.EquipItem(gauntlets,false,true)
    player.EquipItem(boots,false,true)
    player.EquipItem(helmet,false,true)
EndEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
    Actor player = akCaster
    
    ;checking old armor and equipping:
    int i = 0
    while i<4
        Armor tmp = oldArmor[i]
        if tmp != None
            player.EquipItem(tmp,false,true)
        endif
    endwhile
    
    ;remove new armor from inventory, this part doesn't work, items still stay in the inventiry;
    player.RemoveItem(bodyarmor,1)
    player.RemoveItem(gauntlets,1)
    player.RemoveItem(boots,1)
    player.RemoveItem(helmet,1)
EndEvent


Magic effect is attached to lesser power. When I activate this power it behaves very strangely. First new armor is equipped but after power expires new armor isn't removed from the inventory.

Can someone tell me what I am doing wrong and how to fix it?

Link to comment
Share on other sites

Turns out I am dumb. I forgot to increment i inside while and it was stuck in infinite loop. This is my first time writing code in papyrus script so I was looking error in variable types or code structure

Edited by gba25
Link to comment
Share on other sites

For future reference, I'm going to make a small tweak to your while loop so that you can work with an array without explicitly stating the number of entries to loop through. It can save hassle if at any point you decide to change the number of entries in the array.

 

This loops in reverse order starting with the highest index and going to zero. It exits the loop once the value becomes negative one.

 

int i = oldArmor.Length - 1
while i >= 0
  Armor tmp = oldArmor[i]
  if tmp != None
    player.EquipItem(tmp,false,true)
  endif
  i -= 1
endwhile

 

This loops in order starting with zero and going to the highest index. It exits the loop once the value equals the length (the highest index plus one).

 

int i = 0
while i < oldArmor.Length
  Armor tmp = oldArmor[i]
  if tmp != None
    player.EquipItem(tmp,false,true)
  endif
  i += 1
endwhile

 


Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...