SKSE Plugin Development/Calling Papyrus Functions

From the CreationKit Wiki
Jump to navigation Jump to search
auto vm = RE::BSScript::Internal::VirtualMachine::GetSingleton();

RE::TESForm* form = RE::TESForm::LookupByEditorID("MyQuest");

auto policy = vm->GetObjectHandlePolicy();
RE::VMHandle handle = policy->GetHandleForObject(form->GetFormType(), form);

if (handle == policy->EmptyHandle()) {
    return;
}

RE::BSFixedString scriptName = "MyScript";
RE::BSFixedString functionName = "MyFunction";

RE::BSTSmartPointer<RE::BSScript::Object> object;
RE::BSTSmartPointer<RE::BSScript::IStackCallbackFunctor> result;

if (vm->FindBoundObject(handle, scriptName.c_str(), object)) {
    int x = 42;
    auto args = RE::MakeFunctionArguments(std::move(x));
    vm->DispatchMethodCall1(object, functionName, args, result);
}

Source: How to call Papyrus functions from SKSE / CommonLibSSE Plugin : r/skyrimmods