Getting the index of a reference in a formlist

Post » Fri Nov 16, 2012 5:34 pm

Hello,

I'm trying to make a script that replaces an armor when another one is equipped. When the player equips an armor, the script checks if the armor is in a form list, and if yes, replaces it for another armor in another formlist which is at the same position inside the formlist.

Here comes the trouble. I can easily detect if the armor is in the formlist, but I can't manage to retrieve it's position inside the formlist (didn't find a command for this...). So I made a while loop that searches for the index, as I can then use that index with the GetAt command to retrieve the new armor from the other formlist.

Now, my while loop is giving me the following error on compilation: (19,17): cannot compare a objectreference to a armor (cast missing or types unrelated)

This is in this line: If Reference == Cuirass

I think I understand why the error comes, I'm trying to compare an armor to an object reference? Anyway, how can I solve this? By the way, it's using some SKSE functions...

Scriptname ArmorReplace extends ReferenceAliasInt iIndexObjectReference ReferenceFormList property ArmorsVanilla AutoFormList property ArmorsModified AutoEvent OnObjectEquipped(Form akBaseObject, ObjectReference akReference)if akBaseObject as Armor  Debug.Trace("I just equipped an armor!")  Armor Cuirass = Game.GetPlayer().GetWornForm(0x00000004) as Armor  if (ArmorsVanilla.HasForm(Cuirass))   Game.GetPlayer().RemoveItem(akBaseObject, 1, True)   iIndex = ArmorsVanilla.GetSize()   While(iIndex > 0)	iIndex -= 1	Reference = ArmorsVanilla.GetAt(iIndex) as ObjectReference	If Reference == Cuirass	 Game.GetPlayer().AddItem(ArmorsModified.GetAt(iIndex), 1, true)	EndIf   EndWhile  EndIfendifendEvent

Thanks in advance for any help!
User avatar
Jordan Fletcher
 
Posts: 3355
Joined: Tue Oct 16, 2007 5:27 am

Post » Fri Nov 16, 2012 5:34 pm

Try:
Spoiler
ScriptName ArmorReplace Extends ReferenceAliasActor Property PlayerREF AutoFormList Property ArmorsVanilla AutoFormList Property ArmorsModified AutoEvent OnObjectEquipped(Form akBaseObject, ObjectReference akReference)	If ArmorsVanilla.HasForm(akBaseObject)  		Int iIndex = ArmorsVanilla.GetSize() - 1		While (akBaseObject != ArmorsVanilla.GetAt(iIndex)) && iIndex > 0 ; '> 0' should not be necessary, but the while loop might not otherwise resolve itself			iIndex -= 1		EndWhile		PlayerREF.RemoveItem(akBaseObject, 1, True)		PlayerREF.EquipItem(ArmorsModified.GetAt(iIndex), False, True) ; Will give and equip the replacement. Note the caveat in the EquipItem Wiki page	EndIfEndEvent
You don't want an ObjectReference, but a Form instead. All ObjectReferences are Forms, but not all Forms are ObjectReferences. Only an ObjectReference which is persistent or is currently filling a quest's ReferenceAlias is both an ObjectReference and a Form.

For instance, assume...
ExcaliburREF [REFR:XX00000801] (places ExcaliburWEAP "Excalibur" [WEAP:XX0000800] in GRUP Cell Persistent Children of Elsweyr "Holding Cell" [CELL:000B9BC6])
... is filling a property or is currently a quest's ReferenceAlias.
Event OnObjectEquipped(Form akBaseObject, ObjectReference akReference)	If akReference == ExcaliburREF		Debug.Trace("ExcaliburREF equipped") ; Will fire	EndIfEndEvent
If [REFR:XX00000801] is not persistent or filling an alias, 'None' would be returned, like another instance of ExcaliburWEAP creted by AddItem, EquipItem, or a placed Ref that's not persistent/filling an alias.
User avatar
Annika Marziniak
 
Posts: 3416
Joined: Wed Apr 18, 2007 6:22 am

Post » Fri Nov 16, 2012 11:44 pm

Thanks a lot!

Worked like a charm... You also cleaned the code and made it shorter and clearer!

I read about the EquipItem issue on the CK Wiki not registering as equipped, that's why I wanted to add the item first. But it might actually come handy that it doesn't register it, as I need to repeat the same process on reverse and I could get into a replacing loop. If it doesn't register it as equipped right away, that might solve my issue.

Thanks again for your extremely helpful, well explained and uninterested answer!!!


Edit: Forget about the replacement loop, I will be using the OnObjectUnequipped event...
User avatar
stephanie eastwood
 
Posts: 3526
Joined: Thu Jun 08, 2006 1:25 pm

Post » Fri Nov 16, 2012 11:08 pm

Glad it all worked out :)
User avatar
Jessica Thomson
 
Posts: 3337
Joined: Fri Jul 21, 2006 5:10 am


Return to V - Skyrim