Skip to content

Latest commit

 

History

History
118 lines (86 loc) · 3.65 KB

native.md

File metadata and controls

118 lines (86 loc) · 3.65 KB

Native functions

Most types have a list of native functions, they are divided into static functions (Native Global in Papyrus) and methods (Native).

Static functions are called on the type:

let sunX = Game.getSunPositionX();
let pl = Game.getPlayer();
Game.forceFirstPerson();

Methods are called on the object:

let isPlayerInCombat = pl.isInCombat();

A list of original game types with documentation can be found here: https://www.creationkit.com/index.php?title=Category:Script_Objects

WARNING: Papyrus types and methods are only available inside new events introduced by Skyrim Platform (except tick) or sendPapyrusEvent hook (see hooks).

An exception will be thrown if you try to use them outside any of those contexts.

Native functions from SKSE plugins

You are able to call Papyrus functions added by SKSE plugins.

This example illustrates how to use PapyrusUtil in a Skyrim Platform plugin:

import * as sp from "skyrimPlatform";

sp.once("update", () => {
  const filePath = "data/platform/plugins/plugin-example.js";

  // SkyrimPlatform doesn't contain PapyrusUtil typings so we cast to any to be able to call all functions.
  // Here we use 'MiscUtil.ReadFromFile' native. Of course, you can use any other function.
  const str = (sp as any).MiscUtil.readFromFile(filePath);

  sp.printConsole("Read", str.length, "characters");
});

Another option is to use converted library definitions or make them yourself by using Papyrus-2-Typescript; then you can use native SKSE functions the same way you would use them in Papyrus:

import { once, printConsole } from "skyrimPlatform";
import * as MiscUtil from "PapyrusUtil/MiscUtil";

once("update", () => {
  const filePath = "data/platform/plugins/plugin-example.js";

  // PapyrusUtil typings are defined inside MiscUtil.ts
  const str = MiscUtil.readFromFile(filePath);

  printConsole("Read", str.length, "characters");
});

Asynchronous

Some game functions take time and happen in the background. Such functions in SkyrimPlatform return Promise:

Game.getPlayer()
  .SetPosition(0, 0, 0)
  .then(() => {
    printConsole("Teleported to the center of the world");
  });
Utility.wait(1).then(() => printConsole("1 second passed"));

When called asynchronously, execution continues immediately:

Utility.wait(1);
printConsole(`Will be displayed immediately, not after a second`);
printConsole(`Should have used then`);

You can use async/await to make the code look synchronous:

let f = async () => {
  await Utility.wait(1);
  printConsole("1 second passed");
};

WARNING: When using asynchronous functions inside new events, Form references may be already lost when the time to do something with them has come.

For those cases, it's possible to save the FormId and retrieve the Form again with Game.getFormEx.

on("unequip", (event) => {
  // Define an async function.
  const f = async () => {
    const id = event.actor.getFormID();
    await Utility.wait(0.1);
    // event.actor is lost at this point. We need to get it again.
    const a = Game.getFormEx(id);
    printConsole(`${a?.getName()} unequipped something.`);
  }

  // Execute previosly defined async function
  f();
});