Calling Papyrus Scripts from C#
The build generates a C# class from each .psc file in the project's Scripts\Source folder, named for the script with Script appended,
in the project's root namespace. Its members call the script's functions and read and write its properties through the Papyrus virtual machine,
with the parameter types, defaults, and comments the script declares. The build also compiles each .psc to the .pex the game loads,
as Compiling the Scripts describes.
This script counts the activations of the object it is attached to:
Scriptname SkyrimForgeCounter extends ObjectReference
; Counts each activation of the object this script is bound to.
int Property Counter Auto
Event OnActivate(ObjectReference akActionRef)
Counter += 1
EndEvent
; Describes the player's gold through GetPlayerGold, a native function SkyrimForgeExamples implements.
string Function DescribePlayerGold() global
Return "The player has " + SkyrimForgeExamples.GetPlayerGold() + " gold."
EndFunction
This plugin attaches it to the object under the crosshair, reads its Counter property, and calls its global function DescribePlayerGold
through the generated class SkyrimForgeCounterScript:
using SkyrimForge.Events;
using SkyrimForge.Game;
using SkyrimForge.Papyrus;
using System;
using System.Threading.Tasks;
namespace SkyrimForge.ExamplePlugins;
/// <summary>Binds a Papyrus script to the object under the crosshair when F7 is pressed, with no form record attaching it.</summary>
/// <remarks>
/// The Papyrus VM creates the same instance as for a script attached in the Creation Kit, and the game writes it into the save.
/// The build generates <see cref="SkyrimForgeCounterScript"/> from Scripts\Source\SkyrimForgeCounter.psc.
/// </remarks>
public sealed class PapyrusAttachPlugin(GameTarget gameTarget, IGameEvents events, IUtility utility, ILog log) : IPlugin
{
private static string ScriptName => SkyrimForgeCounterScript.Dispatcher.ScriptName;
/// <inheritdoc/>
public void Start()
{
events.OnKeyDown(BSKeyboardDevice.Keys.Key.kF7, ModifierKeys.None).Subscribe(_ => BindAsync());
log.Write($"F7 binds {ScriptName} to the object under the crosshair and logs its activation count.");
}
private async Task BindAsync()
{
await utility.WaitForGameThreadAsync();
if (gameTarget.CrosshairReference is not { } crosshairReference)
{
log.Write("Nothing is under the crosshair.");
return;
}
try
{
SkyrimForgeCounterScript.Dispatcher.Bind(crosshairReference);
// Counter is an Auto property, so reading it reads the instance's variable without a Papyrus call.
int activationCount = crosshairReference.Papyrus.Counter;
log.Write($"{ScriptName} is bound to {crosshairReference.GetDisplayFullName()}, 0x{crosshairReference.FormId}. Activations: {activationCount}.");
}
catch (InvalidOperationException ex)
{
log.Write($"{ScriptName} was not bound to 0x{crosshairReference.FormId}: {ex.Message}");
}
log.Write(await SkyrimForgeCounterScript.DescribePlayerGold());
}
}
- A global function is a static method of the class:
await SkyrimForgeCounterScript.DescribePlayerGold(). - A function or property of a script that extends a form type is a member of the form's
Papyrusproperty, whichusing SkyrimForge.Papyrus;brings in scope:reference.Papyrus.Counter. The form must have the script attached, in the Creation Kit or by the class'sDispatcher.Bind. A call on a form without the script fails withInvalidOperationException. - A function returns a task that completes when the script function returns. An
Autoproperty is read and written directly, on the game's thread, without a call through the virtual machine. - A
.pscthat the build writes for a class marked[PapyrusScript]has no class generated, since the plugin implements its functions. Papyrus Native Functions describes those classes. - SkyrimForge declares the same members for the game's and SKSE's scripts, in the
SkyrimForge.Papyrusnamespace:await player.Papyrus.GetLevel(). Papyrus'sGame.GetPlayer()isawait GameScript.GetPlayer(). Papyrus Functions with Direct Members lists the functions whose work a member does without the virtual machine.
Build Diagnostics
- SF1004 (error): a
.pscdeclares noScriptname, so no class is generated from it. - SF1005 (warning): a function or property has no generated member. Either the script extends no form type, as a script that extends
ActiveMagicEffectorReferenceAliasdoes, or its declaration uses a Papyrus type with no C# type, such asAliasor another script, or the script also declares a function with the member's name. The warning states which applies.