Skip to content

Conversation

@DasKeifer
Copy link
Member

@DasKeifer DasKeifer commented Jan 26, 2026

This makes debugging why your callback failed much easier especially if you are using other peoples libs/code

Below is an example of an intentionally injected hook and event failure. Before it only included the line it failed on instead of teh call trace section. Additionally it did not have the event\hook field before. This should allow for easier debugging of hooks especially that are failing in lower level, generic code

[2026-02-02 22:01:01] [./scripts/mod_loader/bootstrap/event.lua:282]: An event callback failed: ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:19: attempt to index global 'varToFailOn' (a nil value)
- Event\Hook: onGameEntered
- Call trace: 
    stack traceback:
        ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:19: in function 'testFnEventFailure3'
        ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:23: in function 'testFnEventFailure2'
        ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:27: in function 'testFnEventFailure1'
        ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:56: in function <...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:55>
        (tail call): ?
- Subscribed at: 
    stack traceback:
        ./scripts/mod_loader/bootstrap/event.lua:140: in function 'subscribe'
        ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:55: in function 'init'
        ...ns/RedactedRiceExts/exts/CPLUS+_Ex/cplus_plus_ex.lua:77: in function 'initModules'
        ...ns/RedactedRiceExts/exts/CPLUS+_Ex/cplus_plus_ex.lua:135: in function 'init'
        ...ons/RedactedRiceExts/exts/CPLUS+_Ex/scripts/init.lua:25: in function 'init'
        ./scripts/mod_loader/mod_loader.lua:464: in function <./scripts/mod_loader/mod_loader.lua:462>
        [C]: in function 'xpcall'
        ./scripts/mod_loader/mod_loader.lua:461: in function 'initMod'
        ./scripts/mod_loader/mod_loader.lua:157: in function 'init'
        ./scripts/mod_loader/mod_loader.lua:781: in main chunk
        [C]: in function 'require'
        ./scripts/mod_loader/__scripts.lua:42: in main chunk
        [C]: in function 'require'
        scripts/modloader.lua:1: in main chunk
- Dispatched at: 
    stack traceback:
        ./scripts/mod_loader/bootstrap/event.lua:294: in function 'dispatch'
        ./scripts/mod_loader/modui/root.lua:288: in function <./scripts/mod_loader/modui/root.lua:226>
[2026-02-02 22:04:33] [./scripts/mod_loader/bootstrap/event.lua:282]: An event callback failed: ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:31: attempt to index global 'varToFailOn' (a nil value)
- Event\Hook: fireNextTurnHooks
- Call trace: 
    stack traceback:
        ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:31: in function 'testFnHookFailure3'
        ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:35: in function 'testFnHookFailure2'
        ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:39: in function 'testFnHookFailure1'
        ...tedRiceExts/exts/CPLUS+_Ex/scripts/time_traveler.lua:78: in function 'fn'
        ./scripts/mod_loader/bootstrap/modApi.lua:186: in function <./scripts/mod_loader/bootstrap/modApi.lua:184>
        (tail call): ?
- Subscribed at: 
    stack traceback:
        ./scripts/mod_loader/bootstrap/event.lua:140: in function 'subscribe'
        ./scripts/mod_loader/bootstrap/modApi.lua:184: in function 'AddHook'
        ./scripts/mod_loader/bootstrap/modApi.lua:235: in main chunk
        [C]: in function 'require'
        ./scripts/mod_loader/bootstrap/__scripts.lua:18: in main chunk
        [C]: in function 'require'
        ./scripts/mod_loader/__scripts.lua:42: in main chunk
        [C]: in function 'require'
        scripts/modloader.lua:1: in main chunk
- Dispatched at: 
    stack traceback:
        ./scripts/mod_loader/bootstrap/event.lua:294: in function 'dispatch'
        ./scripts/mod_loader/bootstrap/modApi.lua:181: in function 'fireNextTurnHooks'
        ./scripts/mod_loader/altered/missions.lua:57: in function <./scripts/mod_loader/altered/missions.lua:45>
        (tail call): ?		

To test, add code that will intentionally fail and trigger the hooks. In this case, I added an addNextTurnHook hook and a onGameEntered event (each only had the hook/event which is why they are different hook/event) with a few layers of fns just to show the stack trace is working correctly

local function testFnEventFailure3()
	varToFailOn.nilObj = 5
end

local function testFnEventFailure2()
	testFnEventFailure3()
end

local function testFnEventFailure1()
	testFnEventFailure2()
end

local function testFnHookFailure3()
	varToFailOn.nilObj = 5
end

local function testFnHookFailure2()
	testFnHookFailure3()
end

local function testFnHookFailure1()
	testFnHookFailure2()
end

mod:init()
	modApi.events.onGameEntered:subscribe(function()
		testFnEventFailure1()
	end)
end
mod:load()
	modApi:addNextTurnHook(function()
		testFnHookFailure1()
	end)
end

Then start a game (trigger onGameEntered failure), enter a mission and end turn (trigger nextTurnHook)

@DasKeifer DasKeifer added the enhancement New feature or request label Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants