Jump to content

Call function in script attached to ObjectReference


Recommended Posts

Hello all. I'm trying to figure out how to call a function that is in a script attached to on objectreference.

ObjectReference property managerObj auto  ;This object has the script with the function I'm trying to access.

Event OnEffectStart(Actor akTarget, Actor akCaster)

    ;Here, I want to try to access the script attached to managerObj
    managerObj.Script01.Function01();  ;How do I do this?

EndEvent



This is all pseudo code, but I cannot figure out how to accomplish this.

 

I've seen it suggested that I should cast the ObjectReference to a temp variable of type "Script01":

Script01 managerScr = managerObj as Script01

This always throws an error about not being able to cast an ObjectReference to the script type. Is there a function I'm missing like ObjectReference.GetNthScript(0) or ObjectReference.GetScript(Script01)?

 

 

 

If this isn't actually possible, does anyone know of a way to create a Global ObjectReference, or to store a specific entity's prid so I can easily access that object through code? For a bit of reference, I'm trying to create a script that will let you mark a specific object and then summon that object to you. I can't assign a reference in the creation kit because I want the player to be able to change which object is marked while playing.

 

Thanks!

Link to comment
Share on other sites

Both of the following should be viable options

#1

ScriptName SomeScript Extends ActiveMagicEffect
 
ObjectReference Property MyObject Auto
{Object must have target script attached}
 
Event OnEffectStart(Actor akTarget, Actor akCaster)
  MyScript MyVariable = MyObject as MyScript
  MyVariable.MyFunction()
EndEvent

#2

ScriptName SomeScript Extends ActiveMagicEffect
 
MyScript Property MyVariable Auto
{Pick the correct object with the target script}
 
Event OnEffectStart(Actor akTarget, Actor akCaster)
  MyVariable.MyFunction()
EndEvent

It may be that you need to use the base object of your ObjectReference rather than the placed instance (i.e MiscObject, etc). It may also be that you need to actually put the script on the placed instance rather than on the base object.

 

All I know is that at one point I had used a function or property from an ObjectReference using the second option and in that case I had put the script directly on the placed reference instead of the base object. I have also done the first option but it was targeting a script on a quest, so no experience with an ObjectReference in that scenario.

Link to comment
Share on other sites

Thank you for the response.

 

I'm trying to use option 2, but whenever I add a property of type MyScript, it grays it out and doesn't allow me to assign a value from the properties window.

 

As for option 1, whenever I try that it gives me the following error:

 

"Cannot cast objectreference to a MyScript, types are incompatible."

Edited by PowderedSugar
Link to comment
Share on other sites

Are you adding the property in the Creation Kit? Try writing it in and recompiling the script. That is how I did mine. If that still doesn't work, then I am clueless. After all, while SSE Papyrus is supposed to be the same as original Skyrim Papyrus, they could have changed something that I am not aware of.

Link to comment
Share on other sites

Thank you for the response.

 

I'm trying to use option 2, but whenever I add a property of type MyScript, it grays it out and doesn't allow me to assign a value from the properties window.

 

As for option 1, whenever I try that it gives me the following error:

 

"Cannot cast objectreference to a MyScript, types are incompatible."

 

I think IsharaMeradin was only using MyScript as a place holder for your actual script name.

 

You would paste this into the script, save it, then fill the property. IsharaMeradin has provided you with very clear instructions, I will ask you to please up your game a little bit and not waste her time.

Script01 Property managerObj Auto
Edited by Masterofnet
Link to comment
Share on other sites

I know MyScript was a placeholder, it was just easier to write than _HC_HorseManager.

 

Here is the specific bit of code I tried, for both variants.

 

Option 1:

ObjectReference property HorseManagerObj auto

Event OnEffectStart(Actor akTarget, Actor akCaster)
    
    _HC_HorseManager manager = HorseManagerObj as _HC_HorseManager

EndEvent

This results in the following error:

 

 

Cannot cast a objectreference to _hc_horsemanager, types are incompatible.

 

Even if I add the property, save (ignoring the error), set the property to the object that has the _HC_HorseManager, then edit the source and try again, it still throws the error. (I'm aware that this shouldn't fix the problem, but wanted to let you know that I had tried it, just in case.)

 

Option 2:

_HC_HorseManager property HorseManager auto

This results in a grayed out property field. I cannot auto-fill or manually edit the value.

 

 

 

Despite my low post count, I am not a novice programmer, and do not appreciate the assumption that I'm being a deliberate time waster.

Edited by PowderedSugar
Link to comment
Share on other sites

 

Despite my low post count, I am not a novice programmer, and do not appreciate the assumption that I'm being a deliberate time waster.

 

That would not be obvious from this post.

 

I just went into the kit and did exactly what IsharaMeradin has been telling you to do and it worked perfectly.

 

I found and actor in a cell. I looked at script attached to them and I did this.

MyScript Property ActorRef Auto  ;I would find the exact ref name in the cell and use that.  That is what I did but it did not auto fill.

The Magic effect script compiled and then I opened up the properties and added the object reference property to the script. BTW the script was placed on the actor base.

 

You need to stop wasting time and post exactly what you have set up so you can be helped.

 

_HC_HorseManager Let us see this script.

Edited by Masterofnet
Link to comment
Share on other sites

 

 

Despite my low post count, I am not a novice programmer, and do not appreciate the assumption that I'm being a deliberate time waster.

 

That would not be obvious from this post.

 

I just went into the kit and did exactly what IsharaMeradin has been telling you to do and it worked perfectly.

 

I found and actor in a cell. I looked at script attached to them and I did this.

MyScript Property ActorRef Auto  ;I would find the exact ref name in the cell and use that.  That is what I did but it did not auto fill.

The Magic effect script compiled and then I opened up the properties and added the object reference property to the script. BTW the script was placed on the actor base.

 

You need to stop wasting time and post exactly what you have set up so you can be helped.

 

_HC_HorseManager Let us see this script.

 

 

Here's _HC_HorseManager

Scriptname _HC_HorseManager

ObjectReference property HCPrimaryHorse Auto

Function HCSetMarkedHorse(ObjectReference target)
    HCPrimaryHorse = target
EndFunction

THANK YOU!

 

As I was writing this, I realized that I never set _HC_HorseManager to extend ObjectReference, which is why I couldn't cast it. I changed that and it started working perfectly.

 

Much obliged!

Edited by PowderedSugar
Link to comment
Share on other sites

  • Recently Browsing   0 members

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