Jump to content

[Papyrus] Accessing an object globally


madmatty946

Recommended Posts

Hi,

I'm fairly experienced in programming but very new to FO4 modding and Papyrus. So here is my question:

 

What is the best way to get an ObjectReference from an object that is placed somewhere in the map? In some way I imagine the reference of the target should be stored globally in some variable, but how do I do this? As far as I understand, you would use a quest for that that keeps running and just attach the object reference to the quest? But how is this done? And how does my script get the reference to the quest?

 

I'm confused.

 

Link to comment
Share on other sites

If it helps, on whatever objects (scripts, containers, items, etc) that you can attach scripts to will also have a properties button, to add a reference for use in your script, you just go to the scripts window which should have 3 buttons, Add/Remove/Properties. Then click Properties>Add New Property>ObjectReference(Or whatever the type is) then you may be able to auto-fill if its the exact same name as the reference, otherwise you edit the value, use the dropdowns or render window to add your reference, then you can use it in script with the property name.

 

At least, I think that's what you were asking.

Link to comment
Share on other sites

If it helps, on whatever objects (scripts, containers, items, etc) that you can attach scripts to will also have a properties button, to add a reference for use in your script, you just go to the scripts window which should have 3 buttons, Add/Remove/Properties. Then click Properties>Add New Property>ObjectReference(Or whatever the type is) then you may be able to auto-fill if its the exact same name as the reference, otherwise you edit the value, use the dropdowns or render window to add your reference, then you can use it in script with the property name.

 

At least, I think that's what you were asking.

Thanks! This would be how I attach the quest script to the object, that I will call object A. Now I want to have an object B (that can be built from the workbench) that has a script attached to it and I want the script attached to object B access the properties of object A. How would this work now? Am I overlooking something really simple?

Link to comment
Share on other sites

Well, there's a few ways, depending on what you're wanting to do. I really hope this isn't as confusing as it sounds to me trying to explain it.

 

Let's call your object CoolGun and we attach a script to it that you've created called MegaDeathScript that we'll say has a function to add bullets every time you.. we'll say equip the other item, we'll call it AmmoHat, we'll call this function AddAmmo(int bulletcount) also. And a AmmoHatScript.

 

Now, how you reference the object can vary. Here's two easy ways to define it, and this would be on AmmoHatScript

 

ObjectReference Property CoolGun Auto Const ; The Auto will try to autofill it, the const just says its a constant(never changes) and since its a specific reference, I don't think it should but anyone can correct me if I'm wrong.

 

MegaDeathScript Property CoolGunScript Auto Const ; This allows you to reference the actual script attached to that.

 

If you go to the properties after this, you /should/ have values defined, not default/none, you can try to auto-fill or choose from the drop downs after editing values.

 

So on your script, you could have an event, Actor.OnItemEquipped (I think that's the name), that fires whenever you equip AmmoHat. We're going to use that particular event to mess with CoolGun.

 

CoolGun.GetCurrentLocation() ; This is an objectreference function so works without a hitch and returns the object's current position

CoolGun.AddAmmo(50) ; this would be an error because it's not the correct type so Papyrus doesn't know what to expect out of it

CoolGunScript.AddAmmo(50) ; this would work because it knows the actual type

 

Alternatively, you could do, I think...

(CoolGun as MegaDeathScript).AddAmmo(50) ; If you're not familiar with it, this is called casting an object, it makes it behave as a specific type of script.

 

I'm not 100% sure if this is how Papyrus handles it, but it should, since your MegaDeathScript extends objectreference (builds on it)...

CoolGunScript.GetCurrentLocation()

Though don't quote me on that, I haven't tested it.

 

Also, this is a very roundabout way of handling this. All of this could easily be handled on CoolGun without needing an AmmoHat, but I dunno what your intended application is.

Link to comment
Share on other sites

Yea pretty much what Maduin said but to be a little more concise: If you're familiar with inheritance and polymorphism then this shouldn't be too much to understand.

Scriptname ScriptForObjectA extends ObjectReference

Function doStuff(ObjectReference objToDoStuff)
     ;Does something
endFunction

---------------Different File--------------
Scriptname ScriptForObjectB extends ObjectReference

ObjectReference property ObjectA auto mandatory

Event OnWorkshopObjectDestroyed(ObjectReference akReference)
     if akReference == ObjectA
          (ObjectA as ScriptForObjectA).doStuff((self as ObjectReference))
     endif
endEvent

What this does is checks every time a workshop item is destroyed. If it's our ObjectA then it will have our ScriptForObjectA attached so we can just cast it as the script and pass this Object (Note: this script would have to be attached to objectB to get the desired effect).

 

But yea there are many ways to do this depending on your direct application.

 

Edit: Of course even if it is an improper use of OnWorkshopObjectDestroyed the point's all the same, it could be anything really. Just a good starting point I'd think.

Edited by ant2888
Link to comment
Share on other sites

  • Recently Browsing   0 members

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