Create New Form Instance in Script

Post » Fri May 16, 2014 2:36 am

I guess perhaps I don't understand Papyrus as well as I had thought.

I've created a new class that extends ObjectReference. In another script, I would like to instantiate a new instance of my class. I thought I could just use something like:

MyExtendedClass objMyClass = new MyExtendedClass

However, the compiler chokes on this as it assumes I'm trying to use the New keyword to instantiate an array.

Hoping this is a simple syntax issue and not that Papyrus fundamentally prohibits generation of new script objects.

User avatar
Kayleigh Mcneil
 
Posts: 3352
Joined: Thu Jun 29, 2006 7:32 am

Post » Fri May 16, 2014 8:08 am

There are no "script objects" there are game objects with scripts attached. Papyrus is not a general purpose programming language. It's a scripting language designed to manipulate game objects. Don't think of your script as MyExtendedClass, think of it as MySpecialObjectScript. If you're really creating a special object you could make the script inherit from the base class (Activator, Weapon, MiscObject, etc.) and attach it to the base object. Then the PlaceAtMe or AddItem functions would also be creating the attached script.
User avatar
Yonah
 
Posts: 3462
Joined: Thu Aug 02, 2007 4:42 am

Post » Thu May 15, 2014 10:39 pm

Papyrus doesn't work that way i guess you have this pice of code from another programming Language if so please tell me which one, so i can make simpler comparissons...

Papyrus doesn't support something like classes.
You can think of objectreferences/forms/actors/etc as they where another type of variables.
User avatar
xemmybx
 
Posts: 3372
Joined: Thu Jun 22, 2006 2:01 pm

Post » Fri May 16, 2014 10:28 am

An objectReferrence is a thing in the skyrim world. There is no generic way in papyrus to tell the engine to create a new thing out of thin air which makes sense as papyrus is actually separate from the game world. How would the game world know anything about position or the like about the new object? However there are a number of specific ways to create new things. The most common is to take an existing ObjectReference and then PlaceAtMe() your new thing. Like

objectReferrence someObject ; filled somehowobjectReference tempRef = someObject.PlaceAtMe(MyExtendedClass)MyExtendedClass objMyClass = tempRef as MyExtendedClass

PlaceAtMe() takes a Form so it will take anything that extends form so MyExtendedClass is valid. However PlaceAtMe() returns an ObjectReference so you will have to explicitly cast it to MyExtendedClass if you want to use your functions.

If your script isn't supposed to actually be in the game world it shouldn't extend ObjectReference. But I don't believe you can't create new instances of other classes either. Papyrus is a bit of a strange beast depending on what languages you are used to. You could do something like

weapon property IronSword autoWeapon myWeapon = ironSword

But Weapon forms can't themselves be placed, they exist as the base that hold the stats for all objectReferences of type IronSword so modifying IronSword changes all the iron swords in the game. A new IronSword both wouldn't compile but would be meaningless if it did since there could only possibly be one IronSword form. Changing IronSword or myWeapon in the snippet would have the same effect since even though they have different names they refer to the same thing in the engine.

User avatar
Emma louise Wendelk
 
Posts: 3385
Joined: Sat Dec 09, 2006 9:31 pm


Return to V - Skyrim