Jump to content

Custom Objects and Inheritance in Papyrus


Teknofobe

Recommended Posts

I am writing my first Skyrim mod and I am trying to make my code easy to manage by splitting out functionality into parts. To make things simple:

 

I have a base class of shared methods. Let's keep this generic and call it Ball

 

Scriptname Ball

SomeObject[] property myItems auto

 

Where SomeObject is a real Skyrim object like armor, or weapon, etc. Then, I have a child class where I define specifics about my object. For instance:

 

Scriptname BasketBall extends Ball

SomeObject property Item1 auto
SomeObject property Item2 auto
SomeObject property Item3 auto

Function doSomething(Form item)
   ;logic here
EndFunction

Function Initialize()
   myItems = new SomeObject[3] ;inherited from base class

   myItems[0] = Item1
   myItems[1] = Item2
   myItems[2] = Item3
EndFunction

 

Where Item1, Item2, and Item3 are bound to actual skyrim objects in the editor. There might be other classes too, liek football, baseball, etc, where the items placed into myItems is different for each child.

 

Now, My problem is in the class that runs the logic and needs to reference and instance of the child Basketball. How do I do that? Is it even possible? Is there another solution where I can define Basketball and its siblings in their own scripts and reference them from my main script?

Link to comment
Share on other sites

Now, My problem is in the class that runs the logic and needs to reference and instance of the child Basketball. How do I do that? Is it even possible? Is there another solution where I can define Basketball and its siblings in their own scripts and reference them from my main script?

I think you already know, but if you plan on assigning any properties that reference anything (so anything other than bool, int, and float) then the script needs to be attached to something. To reference each instance of the script, you need to somehow reference the object that the script is attached to.

 

Sorry, but I'm not sure how exactly you would do that for your example scripts. If your script extends ObjectReference, then you would be able to simply assign each of the scripted objects to ObjectReference properties. Then you could use casting to reference the script attached to the object. For example, let's say I have a script like this:

 

Scriptname TestObjectScript extends ObjectReference

Armor Property MyArmor Auto
Weapon Property MyWeapon Auto

Function Initialize()
;do stuff
EndFunction

Let's say that I attach this script to SomeObject01 somewhere in the game world. In order to reference the variables and function of this instance of the script from another script, I would use casting like this:

 

Scriptname SomeScript extends something

ObjectReference Property SomeObject01 Auto

;;;Some event or function
Armor LocalArmorVariable = (SomeObject01 as TestObjectScript).MyArmor
Weapon LocalWeaponVariable = (SomeObject01 as TestObjectScript).MyWeapon
(SomeObject01 as TestObjectScript).Initialize()
;;;end event or function

If the script extends form, then I would have declared SomeObject01 as a Form property.

 

However, your Ball script doesn't extend anything, so I don't know how I would reference it... In my example "SomeScript" I used casting, but I could also have done this:

 

Scriptname SomeScript02 extends something

TestObjectScript Property SomeObject01 Auto

;;;Some event or function
Armor LocalArmorVariable = SomeObject01.MyArmor
Weapon LocalWeaponVariable = SomeObject01.MyWeapon
SomeObject01.Initialize()
;;;end event or function

 

I have never tried declaring a property of a script type that doesn't extend anything, and I don't know how you'll be able to fill such a property, but you could try it.

Link to comment
Share on other sites

Maybe that's where I am going wrong on this - my base class not extending from anything.

 

In both of your examples are, is SomeObject01 automatically instantiated - the first case by the casting, the second due to the property declaration? For some reason I am under the impression nothing is assigned if you don't use the editor to set the property.

Link to comment
Share on other sites

You're correct, nothing is assigned until you use the editor to set the property. I'm just not certain that the editor can set the property if the property is of a type that doesn't extend anything. Form and Alias scripts don't extend anything either, but I think we're allowed to use them since they are hard-coded.
Link to comment
Share on other sites

Let's assume I change the code so Ball extends ObjectReference

 

so if the property isn't assigned without the editor, and the type for the property is another script, how would I set that up through the editor?

 

As an aside, the script that houses my logic extends ReferenceAlias and is configured to attach to the Player Alias. I think my issue is that my property in this class to my subclass (Basketball in my above code) is not set up anywhere and I don't know how to set it up through the editor - if that can even be done.

Link to comment
Share on other sites

If Ball extends ObjectReference, then Basketball would also extend ObjectReference (through inheritance). Then you could use it just like in the example that I gave, only instead of TestObjectScript, you would be using Basketball.

 

It's possible to set the value to your property without the editor, but not reliable. You would have to use GetForm but since there's no way to tell where someone would put your mod in the load order, this should only be used if the object you are trying to reference is something from Skyrim.esm.

Link to comment
Share on other sites

Okay, I'm not sure that would work, because Basketball is not an actual object in skyrim. All it is is a script that holds some data (some actual skyrim objects) and some logic. So, when I reference Basketball in my main script, all I want is an instance of the basketball script so that I can access those methods and data, without having all of it in my main script.

 

EDIT:

 

In looking at the creation kit, I have noticed this:

 

Please remember that few example scripts will be complete by themselves, you must attach a script to an object in the Creation Kit before it will run. The exception is for "library" scripts, such as the Utility Script, which contain definitions of global functions for use in other scripts.

 

That is what I need Baskeball and Ball to be - a library script. I just don't understand how to make it a library script and use it.

Edited by Teknofobe
Link to comment
Share on other sites

If you are going to use it as a library script, you just need to import them in your script.

 

Scriptname Example extends Something

Import Basketball

;etc.

 

But as I said in the first post:

 

if you plan on assigning any properties that reference anything (so anything other than bool, int, and float) then the script needs to be attached to something
Link to comment
Share on other sites

  • Recently Browsing   0 members

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