Skip to content

Instantly share code, notes, and snippets.

@Scrivener07
Last active April 29, 2017 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scrivener07/b1026466db2009bf2de53ac46f7c30c7 to your computer and use it in GitHub Desktop.
Save Scrivener07/b1026466db2009bf2de53ac46f7c30c7 to your computer and use it in GitHub Desktop.
Papyrus Object Factory
Scriptname Papyrus:Collections:Dictionary extends Papyrus:Collections:Collection Default
{Represents a collection of keys and values.}
; https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx
import Papyrus
import Papyrus:Objects:Factory
var[] Keys
var[] Values
; Constructor
;---------------------------------------------
Dictionary Function Init() Global
Objects:Factory Factory = GetFactory()
return Factory.Initialize(0x0000083B) as Dictionary
EndFunction
; Events
;---------------------------------------------
Event OnInit()
Keys = new var[0]
Values = new var[0]
EndEvent
; Methods
;---------------------------------------------
int Function IndexOf(var[] array, var object)
{Determines the index of a specific object in the collection.}
If (object)
return array.Find(object)
Else
return Invalid
EndIf
EndFunction
bool Function ContainsKey(var tKey)
{Determines whether the Dictionary<TKey, TValue> contains the specified key.}
return IndexOf(Keys, tKey) > Invalid
EndFunction
bool Function ContainsValue(var tValue)
{Determines whether the Dictionary<TKey, TValue> contains a specific value.}
return IndexOf(Values, tValue) > Invalid
EndFunction
Function Add(var tKey, var tValue)
{Adds the specified key and value to the dictionary.}
If !(ContainsKey(tKey))
Keys.Add(tKey)
Values.Add(tValue)
EndIf
EndFunction
bool Function Remove(var tKey)
{Removes the value with the specified key from the Dictionary<TKey, TValue>.}
int index = IndexOf(Keys, tKey)
If (index > Invalid)
Keys.Remove(index)
Values.Remove(index)
return true
Else
return false
EndIf
EndFunction
Function Clear()
{Removes all keys and values from the Dictionary<TKey, TValue>.}
Keys.Clear()
Values.Clear()
EndFunction
var Function TryGetValue(var tKey)
{Gets the value associated with the specified key.}
int index = IndexOf(Keys, tKey)
If (index > Invalid)
return Values[index]
Else
return none
EndIf
EndFunction
; Properties
;---------------------------------------------
Group Collection
int Property Count Hidden
int Function Get()
{Gets the number of elements contained in the collection.}
return Keys.Length
EndFunction
EndProperty
EndGroup
Scriptname Papyrus:Objects:Factory extends Quest Const
import Papyrus:Objects
; Functions
;---------------------------------------------
ObjectReference Function Initialize(int aFormID)
Activator Type = GetType(aFormID)
return Create(Type)
EndFunction
ObjectReference Function Create(Activator aActivator)
return Anchor.PlaceAtMe(aActivator)
EndFunction
; Globals
;---------------------------------------------
Factory Function GetFactory() Global
return Game.GetFormFromFile(0x00000838, "Scripting.esp") as Factory
EndFunction
Activator Function GetType(int aFormID) Global
return Game.GetFormFromFile(aFormID, "Scripting.esp") as Activator
EndFunction
; Properties
;---------------------------------------------
Group Properties
ObjectReference Property Anchor Auto Const Mandatory
EndGroup
Scriptname Papyrus_Test:FactoryTest Hidden
import Papyrus:Collections
List MyList
Dictionary MyDictionary
Queue MyQueue
Stack MyStack
Event OnInit()
MyList = Papyrus:Collections:List.Init()
MyDictionary = Papyrus:Collections:Dictionary.Init()
MyQueue = Papyrus:Collections:Queue.Init()
MyStack = Papyrus:Collections:Stack.Init()
EndEvent
Scriptname Papyrus:Collections:List extends Papyrus:Collections:Collection Default
{Represents a list of objects that can be accessed by index.}
; https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
import Papyrus
import Papyrus:Objects:Factory
var[] Items
; Constructor
;---------------------------------------------
List Function Init() Global
Objects:Factory Factory = GetFactory()
return Factory.Initialize(0x0000083A) as List
EndFunction
; Events
;---------------------------------------------
Event OnInit()
Items = new var[0]
EndEvent
; Methods
;---------------------------------------------
int Function IndexOf(var object)
{Determines the index of a specific item in the IList.}
If (object)
return Items.Find(object)
Else
return Invalid
EndIf
EndFunction
bool Function Contains(var object)
{Determines whether an element is in the IList.}
return IndexOf(object) > Invalid
EndFunction
Function Add(var object)
{Calling this method always throws a NotSupportedException exception.}
Items.Add(object)
EndFunction
Function Insert(int index, var object)
{Inserts an item to the IList at the specified index.}
Items.Insert(object, index)
EndFunction
Function Remove(var object)
{Removes the first occurrence of a specific object from the IList.}
int index = IndexOf(object)
Items.Remove(index)
EndFunction
Function RemoveAt(int index)
{Removes the IList item at the specified index.}
Items.Remove(index)
EndFunction
Function Clear()
{Removes all items from the IList.}
Items.Clear()
EndFunction
; Constructor
;---------------------------------------------
; Properties
;---------------------------------------------
Group Collection
int Property Count Hidden
int Function Get()
{Gets the number of elements contained in the collection.}
return Items.Length
EndFunction
EndProperty
EndGroup
Scriptname Papyrus:Collections:Queue extends Papyrus:Collections:Collection Default
{Represents a first-in-first-out (FIFO) collection of objects.}
; https://msdn.microsoft.com/en-us/library/7977ey2c(v=vs.110).aspx
import Papyrus
import Papyrus:Objects:Factory
var[] Items
; Constructor
;---------------------------------------------
Queue Function Init() Global
Objects:Factory Factory = GetFactory()
return Factory.Initialize(0x0000083E) as Queue
EndFunction
; Events
;---------------------------------------------
Event OnInit()
Items = new var[0]
EndEvent
; Methods
;---------------------------------------------
Function Enqueue(var object)
{Adds an object to the end of the Queue}
Items.Add(object)
EndFunction
var Function Dequeue()
{Removes and returns the object at the beginning of the Queue}
var item = Items[First]
Items.Remove(First)
return item
EndFunction
var Function Peek()
{Returns the object at the beginning of the Queue<T> without removing it.}
return Items[First]
EndFunction
; Properties
;---------------------------------------------
Group Collection
int Property Count Hidden
int Function Get()
{Gets the number of elements contained in the collection.}
return Items.Length
EndFunction
EndProperty
int Property First Hidden
int Function Get()
return 0
EndFunction
EndProperty
EndGroup
Scriptname Papyrus:Collections:Stack extends Papyrus:Collections:Collection Default
{Represents a last-in-first-out (LIFO) collection of objects.}
; https://msdn.microsoft.com/en-us/library/3278tedw(v=vs.110).aspx
import Papyrus
import Papyrus:Objects:Factory
var[] Items
; Constructor
;---------------------------------------------
Stack Function Init() Global
Objects:Factory Factory = GetFactory()
return Factory.Initialize(0x0000083D) as Stack
EndFunction
; Events
;---------------------------------------------
Event OnInit()
Items = new var[0]
EndEvent
; Methods
;---------------------------------------------
Function Push(var object)
{Inserts an object at the top of the Stack<T>.}
Items.Add(object)
EndFunction
var Function Pop()
{Removes and returns the object at the top of the Stack<T>.}
var item = Items[Last]
Items.Remove(Last)
return item
EndFunction
var Function Peek()
{Returns the object at the top of the Stack<T> without removing it.}
return Items[Last]
EndFunction
; Properties
;---------------------------------------------
Group Collection
int Property Count Hidden
int Function Get()
{Gets the number of elements contained in the collection.}
return Items.Length
EndFunction
EndProperty
int Property Last Hidden
int Function Get()
return Items.Length - 1
EndFunction
EndProperty
EndGroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment