Norbyte / bg3se

Baldur's Gate 3 Script Extender
Other
1.14k stars 66 forks source link

Attempted to access Osiris function in restricted context #22

Closed szphie closed 1 year ago

szphie commented 1 year ago

i have a really simple script that won't run since patch 2 - attempting to launch a timer causes an Attempted to access Osiris function in restricted context error; is there a different event i can subscribe to that will allow firing a timer on game load?

BootstrapServer.lua:

Ext.Require("Server/Timer.lua")

Server/Timer.lua:

Ext.Events.SessionLoaded:Subscribe(function(e)
    Osi.TimerLaunch("Trigger_CustomTimer", 3000)
end)

Ext.Osiris.RegisterListener("TimerFinished", 1, "after", function(finishedTimer)
    if finishedTimer == "Trigger_CustomTimer" then
         Ext.Utils.Print("Timer finished")
    end
end)

run log:

******************************************************************************
*                                                                            *
*                     BG3 Script Extender Debug Console                      *
*                                                                            *
******************************************************************************

BG3Ext v2 built on Aug 31 2023 17:39:20
Game version v4.36.86.210 OK
ScriptExtender::Initialize: Starting
No match found for mapping 'KillLauncher'
Library startup took 931 ms
LibraryManager::PostStartupFindLibraries() took 51 ms
Configuration for 'SimpleTimer':
        MinVersion 1; Feature flags: Lua
1 mod configuration(s) loaded.
Merged configuration:
        MinVersion 1; Feature flags: Lua
Initializing client with target context Load
Modded achievements enabled.
bg3se::ExtensionStateBase::LuaResetInternal(): LUA VM reset.
Configuration for 'SimpleTimer':
        MinVersion 1; Feature flags: Lua
1 mod configuration(s) loaded.
Merged configuration:
        MinVersion 1; Feature flags: Lua
Initializing client with target context Game
bg3se::ExtensionStateBase::LuaResetInternal(): LUA VM reset.
ecl::ScriptExtender::OnGameStateChanged(): Loaded module
esv::ScriptExtender::OnGameStateChanged(): Unloading session
ecl::ScriptExtender::OnGameStateChanged(): Unloading session
ecl::ScriptExtender::OnClientGameStateChanged(): Loading game session
Configuration for 'SimpleTimer':
        MinVersion 1; Feature flags: Lua
1 mod configuration(s) loaded.
Merged configuration:
        MinVersion 1; Feature flags: Lua
Initializing client with target context Game
bg3se::ExtensionStateBase::LuaResetInternal(): LUA VM reset.
esv::ScriptExtender::OnGameStateChanged(): Loaded module
Configuration for 'SimpleTimer':
        MinVersion 1; Feature flags: Lua
1 mod configuration(s) loaded.
Merged configuration:
        MinVersion 1; Feature flags: Lua
Initializing server with target context Game
CustomFunctionManager::ClearDynamicEntries(): Clearing dynamic functions
bg3se::ExtensionStateBase::LuaResetInternal(): LUA VM reset.
Loading bootstrap script: Mods/SimpleTimer/ScriptExtender/Lua/BootstrapServer.lua
esv::ScriptExtender::OnGameStateChanged(): Loading game session
ScriptExtender::OnAfterOsirisLoad: 129330 nodes
ExtensionStateServer::StoryLoaded()
CustomFunctionInjector mapping phase: 1264 -> 1264 functions
ScriptExtender::OnAfterOsirisLoad: 129330 nodes
ExtensionStateServer::StoryLoaded()
CustomFunctionInjector mapping phase: 1264 -> 1264 functions
ScriptExtender::MergeWrapper() - Started merge
ScriptExtender::MergeWrapper() - Finished merge
Error while dispatching event SessionLoaded:    [string "SimpleTimer/Server/Timer.lua"]:2: Attempted to access Osiris function in restricted context
stack traceback:
        [C++ Code]: in field 'TimerLaunch'
        SimpleTimer/Server/Timer.lua:2: in function <SimpleTimer/Server/Timer.lua:1>
        [C++ Code]: in function 'xpcall'
        builtin://Libs/Event.lua:129: in method 'Throw'
        builtin://Libs/Event.lua:184: in function <builtin://Libs/Event.lua:183>
Norbyte commented 1 year ago

Hi,

Osiris scripts can only run when the game has completely finished loading. The SessionLoaded event triggers when the game finished serializing savegame data, but did not load the map, did not send it over to the client, etc. You can listen to the GameStateChanged engine event and wait for the server state to change to "Running", or alternatively the LevelGameplayStarted Osiris event.