casting objectreferences question

Post » Thu Jun 21, 2012 10:39 am

Basically I'm wondering what sorts of things I can cast ObjectReferences as. I have a script (I'm not at home, can't test myself) that I would like to add to all books using skyproc. Here's the script:

Scriptname YaddaYadda Extends ObjectReferenceFaction Property PlayerFollowerFaction autoEvent OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)   LearnedSpell = (self as Book).GetSpell()   If akNewContainer as Actor      pFollower = (akNewContainer as Actor)   endIf   If pFollower.IsInFaction(PlayerFollowerFaction)==True && pFollower.HasSpell(LearnedSpell)==false     (akNewContainer as Actor).addSpell(LearnedSpell)     pFollower.RemoveItem(self)     self.delete()endEvent

Problem is, I have no idea if I can directly cast the ObjectReference akNewContainer as Actor, or self as Book.

Anyone know? If not, can you think of a workaround?
User avatar
Rachie Stout
 
Posts: 3480
Joined: Sun Jun 25, 2006 2:19 pm

Post » Thu Jun 21, 2012 9:59 am

Scriptname YaddaYadda Extends ObjectReferenceFaction Property PlayerFollowerFaction autoSpell LearnedSpellActor pFollowerEvent OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)    if (GetBaseObject() as Book)        LearnedSpell = (GetBaseObject() as Book).GetSpell()        If akNewContainer as Actor            pFollower = akNewContainer as Actor            If pFollower.IsInFaction(PlayerFollowerFaction)==True && pFollower.HasSpell(LearnedSpell)==false                (akNewContainer as Actor).addSpell(LearnedSpell)                pFollower.RemoveItem(self)            endif        endif    endifendEvent
User avatar
Lou
 
Posts: 3518
Joined: Wed Aug 23, 2006 6:56 pm

Post » Thu Jun 21, 2012 8:56 pm

Thanks. As it turns out, UFO already does this, so I'm probably not going to bother. Still though, that teaches me something really valuable for future reference.
User avatar
Peter P Canning
 
Posts: 3531
Joined: Tue May 22, 2007 2:44 am

Post » Thu Jun 21, 2012 4:35 pm

The wiki's http://www.creationkit.com/Cast_Reference#Cast_to_Object has some good information on casting between object types.

Basically you can only cast up or down the inheritance tree; you can't cast to siblings, for example. Casting to ancestor types will always be successful, and casting to descendant types will be successful if the object has that type, otherwise it will result in None.

Cipscis
User avatar
Christina Trayler
 
Posts: 3434
Joined: Tue Nov 07, 2006 3:27 am


Return to V - Skyrim