Jump to content

Get ObjectReference from own Script


Dielos

Recommended Posts

Hello. Finding myself with a stupid problem that I'm not managing to understand...

 

I have an object spawned through script. There is another script attached to the base object of this newly spawned reference. Everything works great in the newly placed object, but I just can't get the object reference of the placed object from within it's own script. It seems I'm missing to understand something...

 

After two days trying, I found out that "Self" does not point to the objectreference, but to the script itself, so how would I get the objectreference this script is running from?

 

I tried Self and Self as ObjectReference, but none work. Do I have to define a property for this, and how to link it to the object?

Link to comment
Share on other sites

It would help to see the source. And you won't find much better help than Reneer.

 

In CK you can try this.

 

Open the base object with the script atttached, select the script, click Add Property, Select ObjectReference, Name it the exact same thing as your Object, CK should 'auto-fill', you can now use that ObjectReference in script.

 

But this is a ref to the base object, not a specific instance of the object.

 

If you created the object yourself (through script) you should already have a place to obtain it's specific reference.

 

If you're looking at some reference after the fact and trying to determine if it is one of 'your objects', you can GetBaseID and compare to your BaseObjectID.

 

Hopefully helpful, possibly not.

Link to comment
Share on other sites

Sorry, thought the question was clear... I need the reference of the placed object, and I need to retrieve it from it's own script. Might be pretty simple, but I'm failing horribly.

 

This is the script in question:

Scriptname AASLFloatActBoat01Script extends ObjectReference

STATIC Property pAASLXMarkerHeading Auto
Activator Property pAASLBoat01 Auto 

ObjectReference Boat01Obj
ObjectReference HeadingMarker

float GameAngleZ
float TrigAngleZ

Event OnInit()
	CreateShip()
	Utility.Wait(15)
	debug.trace("Self is: " + Self as ObjectReference) 
	TranslateStraigth()
EndEvent

Event OnTranslationAlmostComplete()
	UpdateGhostPositions()
	TranslateStraigth()
	;debug.notification("Translation almost complete, updating translate function...")
EndEvent

;------------------------ F U N C T I O N S -------------------------------------

Function CreateShip()
	Boat01Obj = PlaceAtNode("FloatingPlatformHelper01", pAASLBoat01, 1, true, false, false, true)
	HeadingMarker = PlaceAtMe(pAASLXMarkerHeading, 1)
EndFunction

Function TranslateStraigth()
	SetDestMarkerPos()
	TranslateToRef(HeadingMarker, 120)
EndFunction

Function SetDestMarkerPos()
	GameAngleZ = GetAngleZ() - 90
			if ( GameAngleZ < 90 )
			  TrigAngleZ = 90 - GameAngleZ
			else
			  TrigAngleZ = 450 - GameAngleZ
			endif

	Float posX = GetPositionX() + 1100 * Math.Cos(TrigAngleZ);
	Float posY = GetPositionY() + 1100 * Math.Sin(TrigAngleZ);
	Float posZ = GetPositionZ() + 1;
	Float angX = GetAngleX();
	Float angY = GetAngleY();
	Float angZ = GetAngleZ();
	HeadingMarker.SetPosition(posX, posY, posZ)
	HeadingMarker.SetAngle(0, angY, angZ)
EndFunction

Function UpdateGhostPositions()
	Boat01Obj.moveToNode(Self as ObjectReference, "FloatingPlatformHelper01") 
EndFunction

All works fine in this script, apart from the last line. In the function UpdateGhostPositions(), Self as ObjectReference returns "none", and Self alone returns the script itself. I need to catch the spawned object this script is attached to, not the base object. I might need to define a property for it?

 

 

[EDIT]: Forgot to add that the object this script is attached to was spawned from another script with this line:

PlatformHelperObj = pXMarkerPosBoat01.PlaceAtNode("MarkerX", pAASLFloatingPlatformHelper01, 1, true, false, false, false)
Edited by Dielos
Link to comment
Share on other sites

Are you sure that Boat01Obj is actually being created? PlaceAtNode may be failing / returning none and you aren't checking for that possibility anywhere in the script. What are your Papyrus logs showing? Edited by Reneer
Link to comment
Share on other sites

Boat01Obj is a ship attached to a PlatformHelper. It spawns and tags along with the PlatformHelper just fine. The object I'm trying to retrieve is the PlatformHelper, which is the object with this script attached. The TranslateToFunction is applied to the PlatformHelper, and the GetPosition calls are running as both the PlatformHelper and the ship move as intended.

 

I have "debug.trace("Self is: " + Self as ObjectReference)" in the OnInit() event, and it returns the following: [07/15/2016 - 02:02:50PM] Self is: [AASLFloatActBoat01Script < (FF000F34)>] -- I understand this to be the script itself, right?

 

PlaceAtNode could be failing indeed, but with the trace line there I have been blaming Self. The PlaceAtNode call actually sends my boat to Four Leaf Fishpacking Plant... Beats me why, as I'm nowhere near it nor is the PlatformHelper. But I do get from this that the call works, and it's just the Self failing. My guess is that if PlaceToNode has a "none" in the function, it might just be sending my boat to the center of the map.

Link to comment
Share on other sites

I haveÃÂ "debug.trace("Self is: " + Self as ObjectReference)"ÃÂ in the OnInit() event, and it returns the following: [07/15/2016 - 02:02:50PM] Self is: [AASLFloatActBoat01Script < (FF000F34)>] -- I understand this to be the script itself, right?

No, Self in this case should be the Objectreference, not the script.

 

PlaceAtNode could be failing indeed, but with the trace line there I have been blaming Self. The PlaceAtNode call actually sends my boat to Four Leaf Fishpacking Plant... Beats me why, as I'm nowhere near it nor is the PlatformHelper. But I do get from this that the call works, and it's just the Self failing. My guess is that if PlaceToNode has a "none" in the function, it might just be sending my boat to the center of the map.

Does your object model / NIF have those nodes inside it? There are just a few variables here that need to be sussed out. Edited by Reneer
Link to comment
Share on other sites

 

I haveÃÂ "debug.trace("Self is: " + Self as ObjectReference)"ÃÂ in the OnInit() event, and it returns the following: [07/15/2016 - 02:02:50PM] Self is: [AASLFloatActBoat01Script < (FF000F34)>] -- I understand this to be the script itself, right?

No, Self in this case should be the Objectreference, not the script.

Good to know, although I'm at a complete loss then on why this isn't working. I guess then that I could use only "Self", and "Self as ObjectReference" is not needed?

 

PlaceAtNode could be failing indeed, but with the trace line there I have been blaming Self. The PlaceAtNode call actually sends my boat to Four Leaf Fishpacking Plant... Beats me why, as I'm nowhere near it nor is the PlatformHelper. But I do get from this that the call works, and it's just the Self failing. My guess is that if PlaceToNode has a "none" in the function, it might just be sending my boat to the center of the map.

Does your object model / NIF have those nodes inside it? There are just a few variables here that need to be sussed out.

 

The NIF has that node, it's the main one. I can use SetPosition as well, that works, but the object then jumps and it's quite jarring for the player. The single call of PlaceAtNode is much cleaner for an object that is moving. That said, I will make a couple of checks with the PlaceAtNode funtion to see if this might be indeed failing in some way.

Link to comment
Share on other sites

Well, there is certainly something amiss here... You are right though and Self seems to work properly, but I'm doing something wrong anyway.

 

I included these traces on the script to test:

	debug.trace("Empty Self is in: " + GetPositionX() + GetPositionY())
	debug.trace("Self is in: " + Self.GetPositionX() + Self.GetPositionY())
	debug.trace("HeadingMarker is in: " + HeadingMarker.GetPositionX() + HeadingMarker.GetPositionY())
	debug.trace("Boat01Obj is in: " + Boat01Obj.GetPositionX() + Boat01Obj.GetPositionY())

This is what I got, so Self is being detected fine:

[07/15/2016 - 04:42:37PM] Empty Self is in: 69658.73437543368.886719
[07/15/2016 - 04:42:37PM] Self is in: 69662.01562543370.992188
[07/15/2016 - 04:42:37PM] HeadingMarker is in: 69682.85156343382.910156
[07/15/2016 - 04:42:37PM] Boat01Obj is in: 68750.00000042800.000000

You will see that there is a substantial difference in position between the HeadingMarker and Self to the Boat01Obj. The "ghost object" remains in it's original position and only the 3D mesh tags along. As soon as the original cell unloads, my boat unloads as well. That's why I need to update it's position manually.

 

I still don't understand why MoveToNode doesn't work. I also made a test using PlaceAtNode, basically creating just more copies of the object, and that worked fine and they appeared in the expected position. The difference between MoveToNode and PlaceAtNode is basically that in the first I need Self, as I need to pass a parameter, while in the second I dont. I called them like this:

Boat01Obj.moveToNode(Self, "FloatingPlatformHelper01")

Boat01Obj = PlaceAtNode("FloatingPlatformHelper01", pAASLBoat01, 1, true, false, false, true)

I also tested the same moveToNode but with myself (game.getplayer()), and I got moved to the center of the map along my boat.

 

If there is any idea why this is failing...

 

Link to comment
Share on other sites

Well, there has to be certainly a problem with Self...

 

Now I tried this:

	Boat01Obj.SetPosition(GetPositionX(), GetPositionY(), GetPositionZ())
	Boat01Obj.SetAngle(GetAngleX(), GetAngleY(), GetAngleZ())
	Boat01Obj.attachTo(Self) 

The object moves correctly to the new position, but it does not attach...

Link to comment
Share on other sites

  • Recently Browsing   0 members

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