Get attached item

Post » Tue Jun 19, 2012 7:04 am

I'm trying to add a script on a book, so when the player read it, it will be removed after.
I saw some posts where people use "self" variable to get the attached items, so my first attempt was :
Scriptname BookScript extends ObjectReferenceevent onRead();doing stuffGame.GetPlayer().removeItem(self, 1)endEvent

But it doesn't work, the book was not removed.
At first, I thought the problem was because we can't remove the item attached to the running script, so I tried to use "addItem", but it didn't work too.
I also tried to use self.GetBaseObject() with no luck.

However, if I use a property instead, and set the attached book manually, everything work well :
Scriptname BookScript extends ObjectReferencebook Property testbook autoevent onRead();doing stuffGame.GetPlayer().removeItem(testbook, 1)endEvent

So, it seem that the "self" object didn't work like I expected, to verify it, I put this code in the script :
Scriptname BookScript extends ObjectReferencebook Property testbook autoevent onRead()debug.notification ("self : " + self.GetBaseObject())debug.notification ("self only : " + self)debug.notification ("property :" + testbook)endEvent
Tested in game and notifications are :
self : none
self only : [BookScript
property : [Book

The "self" variable refer to the script itself ([bookscript), and not the attached book ([book), so it's explain why it doesn't work.

Anyway, some people seem to successfully use the self variable to get the attached item, so I think I made a mistake somewhere...
Any help on this ?
User avatar
Lori Joe
 
Posts: 3539
Joined: Tue Jun 20, 2006 6:10 am

Post » Tue Jun 19, 2012 6:23 am

So, no one know how to get attached items ?
User avatar
Assumptah George
 
Posts: 3373
Joined: Wed Sep 13, 2006 9:43 am

Post » Tue Jun 19, 2012 1:52 pm

Your "self only" bit returned that because, from the perspective of your script, the object to which your script is attached is of type "BookScript" (every time you write a script you are creating a new type).

I'm not sure why http://www.creationkit.com/GetBaseObject_-_ObjectReference isn't working for you in this case. I think I've seen another report of GetBaseObject not working in an http://www.creationkit.com/OnRead_-_ObjectReference block, which strikes me as very odd.

Perhaps this function doesn't work correctly for books? If you add, say, and http://www.creationkit.com/OnInit block in which you do your reporting, do you get the same result? If you then attach it to a different type of object, do you still get the same result?

Cipscis
User avatar
john page
 
Posts: 3401
Joined: Thu May 31, 2007 10:52 pm

Post » Tue Jun 19, 2012 4:03 am

I tried to use event OnEquipped and OnInit and it doesn't work.
I also tried to use an armor instead of an book and the GetBaseObject function always return none, so I think the self function didn't work has expected ?

Really want to know if someone get this working.

Meanwhile, it seem that the only way for me will be to set the attached book in a property for all my scripted books...
User avatar
Philip Lyon
 
Posts: 3297
Joined: Tue Aug 14, 2007 6:08 am

Post » Tue Jun 19, 2012 12:16 pm

"Self" isn't a function - it's a special variable accessible to non-global functions (including, by definition, all events) that points to the object on which the function is running. For native events, this is always the object to which the script is attached.

I'm confident I've used http://www.creationkit.com/GetBaseObject_-_ObjectReference successfully before, although I don't have access to my source code at the moment so I can't check. There may be caveats to this function not currently documented on the wiki.

Cipscis
User avatar
JLG
 
Posts: 3364
Joined: Fri Oct 19, 2007 7:42 pm

Post » Tue Jun 19, 2012 11:41 am

Some additionnal information :

used this script on armors and books items :
Scriptname bookscript extends ObjectReferenceEvent OnEquipped(Actor akActor)  if akActor == Game.GetPlayer()	   Game.GetPlayer().addItem(self, 1)	   Game.GetPlayer().addItem(self.GetBaseObject(), 1)  endIfendEvent

Checked the papyrus log :

[ (00000014)].Actor.AddItem() - "" Line ?[Item 16 in container  (00000014)].bookscript.OnEquipped() - "bookscript.psc" Line 39[02/23/2012 - 01:44:45AM] error: Unable to call GetBaseObject - no native object bound to the script object, or object is of incorrect typestack:[Item 14 in container  (00000014)].bookscript.GetBaseObject() - "" Line ?[Item 14 in container  (00000014)].bookscript.OnEquipped() - "bookscript.psc" Line 35[02/23/2012 - 01:44:45AM] error: Cannot add None to a container
User avatar
Latisha Fry
 
Posts: 3399
Joined: Sat Jun 24, 2006 6:42 am

Post » Tue Jun 19, 2012 8:03 am

no native object bound to the script object, or object is of incorrect type
That's unexpected - The native "Book" or "Armor" type should be bound to the object in those cases.

What happens if you try this instead?
Parent.GetBaseObject()

P.S. You don't ever need to explicitly call a function on "Self" - the following two snippets are functionally identical:
self.FunctionName()
FunctionName()
The "Self" keyword is only really useful when passing the information as a parameter to a function, with perhaps a couple of small exceptions.

Cipscis
User avatar
jodie
 
Posts: 3494
Joined: Wed Jun 14, 2006 8:42 pm

Post » Tue Jun 19, 2012 5:50 am

Doesn't work with Parent.getbaseobject()
It return "none" and parent only return a blank.

in logs : "Argument variable "parent" was not successfully looked up"

If you successfully use http://www.creationkit.com/GetBaseObject_-_ObjectReference before, I'm really interesting to know how you do it.
User avatar
Aliish Sheldonn
 
Posts: 3487
Joined: Fri Feb 16, 2007 3:19 am

Post » Tue Jun 19, 2012 10:20 am

Now that's odd... The "Parent" special variable should be available within all non-global functions, including events, and should be essentially the same as "(Self as )". In this case, that would be:
(Self as ObjectReference)
I expect these two issues are closely related

Here's a script in which I've successfully used http://www.creationkit.com/GetBaseObject_-_ObjectReference:
ScriptName SwapForEquivalent extends ReferenceAlias{Replaces world object at which this ReferenceAlias ispointing with another object via a 1:1 mapping between FormLists}FormList Property Origin auto{The ordered list of unchanged Forms}FormList Property Destination auto{The ordered list of changed Forms}int Function GetIndex(FormList List, Form Member) global{Returns the index of Member in List, or -1 if Member is not in List}	If (!List.HasForm(Member))		Return -1	EndIf	int Index = 0	While (List.GetAt(Index) != Member)		Index += 1	EndWhile	Return IndexEndFunctionEvent OnInit()	ObjectReference Me = GetReference()	Me.PlaceAtMe(Destination.GetAt(GetIndex(Origin, Me.GetBaseObject())), 1, false, true).Enable()	Me.Disable()	Clear() ; Stop alias from forcing persistence, which stops deletion ; The rest of the function will finish	Me.Delete()	Me.SetPosition(Me.GetPositionX(), Me.GetPositionY(), Me.GetPositionZ() - 30000) ; In case the reference is still persistent (so not deleted)EndEvent
If I remember correctly, this http://www.creationkit.com/ReferenceAlias_Script to which this script was attached ended up pointing to an http://www.creationkit.com/ObjectReference_Script with a base object of type http://www.creationkit.com/Potion_Script.

Cipscis
User avatar
Taylor Bakos
 
Posts: 3408
Joined: Mon Jan 15, 2007 12:05 am

Post » Tue Jun 19, 2012 12:40 pm

Thanks you for your help, but I think I will no more spend time on this.
I manually added attached book in a property, it's boring, but, it's working :-)

I keep an eye on this post if someone found a way to do it.
User avatar
Yung Prince
 
Posts: 3373
Joined: Thu Oct 11, 2007 10:45 pm


Return to V - Skyrim