How do I reference the Object my script is attached to?

Post » Wed Feb 03, 2016 11:17 pm

Hello. I have a simple script that equips a different armor when you unequip the first and removes the first armor.



I can get it to work by passing a reference to the armor into the script. But, it doesn't seem very elegant to have to do that. I tried using "Self". But, that seems to reference the script rather than the Armor that "RemoveItem" needs to work.



Is there a way to accomplish this without sending in a reference to itself?



Here is the script:



Scriptname ArmorSwitcherScript extends ObjectReference

Armor Property ArmorRef Auto
Armor Property nextState Auto

Event OnUnequipped(Actor akActor)

akActor.EquipItem(nextState)
akActor.RemoveItem(Self) ;Does not work like this. Does work if I change Self to ArmorRef and populate that with the Armor the script is attached to.

EndEvent

I have a feeling that there is some casting that needs to happen here. Tried several combinations but none of them worked.



Any help would be greatly appreciated!

User avatar
Amy Cooper
 
Posts: 3400
Joined: Thu Feb 01, 2007 2:38 am

Post » Wed Feb 03, 2016 6:31 pm

The Armor type and a reference are two completely different things.



I think what you're looking for would be "Self.GetBaseObject()" not just "Self" for the RemoveItem call.

User avatar
Maria Garcia
 
Posts: 3358
Joined: Sat Jul 01, 2006 6:59 am

Post » Wed Feb 03, 2016 3:55 pm

Thank you. I had tried GetBaseObject() as well. But, it didn't work. It returns "None".



Any other ways to approach it?

User avatar
Greg Swan
 
Posts: 3413
Joined: Tue Jun 05, 2007 12:49 am

Post » Thu Feb 04, 2016 3:38 am


That's very interesting... I think inventory/containers in general get messed up with object references. It almost sounds like the object reference that the script is attached to is getting deleted and then replaced with something else when it is unequipped. I don't see anything wrong with what you have using ArmorRef above. If you want to eliminate one property to fill, you could just do something like this:


Armor Property nextState  Auto

Armor ArmorRef

Event OnInit()
ArmorRef = GetBaseObject() as Armor
EndEvent

Event OnUnequipped(Actor akActor)
akActor.EquipItem(nextState)
akActor.RemoveItem(ArmorRef)
EndEvent
User avatar
Auguste Bartholdi
 
Posts: 3521
Joined: Tue Jun 13, 2006 11:20 am

Post » Thu Feb 04, 2016 2:58 am

Thank you.



Yeah. It's not so bad to just have the ArmorRef there as I have it working. I just feel like it's a messy additional step. Also, I'd just like to better understand how Papyrus handles and references Objects, etc.



I tried your script and it didn't work. Same result. Maybe passing in a reference is the only way in this case?

User avatar
Chloe Mayo
 
Posts: 3404
Joined: Wed Jun 21, 2006 11:59 pm

Post » Thu Feb 04, 2016 5:24 am

Sorry, I wasn't thinking about it being in inventory (although I obviously should have).



Items in inventory aren't true references in 99.9% of the cases so there is no self attached to the script.



That's why it's failing, so yes you need the extra property for things like this.

User avatar
Spaceman
 
Posts: 3429
Joined: Wed May 23, 2007 10:09 am

Post » Thu Feb 04, 2016 4:18 am

Got it. Thank you for the clarification. A good point to know.



So, essentially, if the object were in the "game world" (in a chest or on the floor for example) it would have the self reference. Inventory items are just more basic code-wise because they haven't been "made real" in the game. Do I have that right?

User avatar
sam smith
 
Posts: 3386
Joined: Sun Aug 05, 2007 3:55 am

Post » Wed Feb 03, 2016 5:19 pm

It would have to be sitting loose in the world. If it's in a container (including being held by an NPC) then it won't have a true ObjectReference since those are only for things in the game world.

User avatar
+++CAZZY
 
Posts: 3403
Joined: Wed Sep 13, 2006 1:04 pm


Return to V - Skyrim