PDA

View Full Version : Unable to write enxtension for custom encumbrance computation on 4E ruleset.



Facjad
July 16th, 2025, 15:05
Hello,

I wrote a simple test extension to change how encumbrance is computed (attached to this post). It works if loaded in CoreRPG, but not in 4E. It looks like something is called too early with the 4E ruleset, let me explain what I found with some debugging.

To do the debugging, I copied CoreRPG ruleset, and modified the files to add some debug prints. I did the same on 4E, basing it on that test CoreRPG ruleset. Basically, all I changed, is adding some lines on some function that tell which function is called. For example, in scripts/manager_char_encumbrance.lua from CoreRPG:


local _bInitComplete = false;
function onInit()
Debug.console("[Core] Call to CharEncumbranceManager.onInit")
_bInitComplete = true;
end

I also modified those functions in scripts/manager_char_encumbrance.lua from CoreRPG:


local _bInitialized = false;
function performInit()
Debug.console("[Core] Call to performInit")
if not _bInitComplete then
return;
end

if CharEncumbranceManager.isEnabled() then
if _bInitialized then
return;
end

Debug.console("[Core] Do the actual friggin performInit")
OptionsManager.registerOptionData({ sKey = "CURR", sGroupRes = "option_header_houserule", tCustom = { default = "on", }, });

if Session.IsHost then
OptionsManager.registerCallback("CURR", CharEncumbranceManager.onCurrencyOptionUpdate);
CurrencyManager.registerCallback(CharEncumbranceMa nager.onCurrencyUpdate);
CharEncumbranceManager.enableEncumbranceHandlers() ;

CharEncumbranceManager.updateAllCharacters();
end

_bInitialized = true;
end
end



local _fnEncumbranceCalc = nil;
function addCustomCalc(fnEncumbranceCalc)
Debug.console("[Core] Call to addCustomCalc")
if _bInitialized then
Debug.console("[Core] addCustomCalc stopped, initialization already finished")
ChatManager.SystemMessage("CharEncumbranceManager.addCustomCalc must be called in global script onInit function.")
return;
end
_fnEncumbranceCalc = fnEncumbranceCalc;
CharEncumbranceManager.performInit();
end


Here is what I get with CoreRPG, just loading a blank campaign, creating a character, and adding elements to the inventory to have encumbrance calculations:
64855
Here is what I get with 4E in a similar scenario:
64856

As we can see, on CoreRPG, the call to performInit for Core's addStandardCalc is done before the call to CharEncumbranceManager.onInit, so that in this call to performInit, _bInitComplete is false, and prevents the proper initialization. Thus, when the extensions calls addCustomCalc, the actual initialization is done with the extension's custom function.
With 4E, however, the call to CharEncumbranceManager.onInit is done too early, so that 4E's call to performInit performs the initialization, and puts the flag _bInitialized to true, so that when the extension laters calls for addCustomCalc, performInit returns before doing its job.

Could you confirm this is a problem with the ruleset? If not, what am I doing wrong?
If it is indeed unexpected behavior, would there be a workaround? I can't find in the 4E ruleset where that early call to CharEncumbranceManager.onInit stems from...

Thanks in advance!
Facjad

bayne7400
July 16th, 2025, 15:40
What you could do is override the core rpg function that does the standard calc. It would fork based on your options menu. Stay the standard calc path or do your custom code. it would be 4 or 5 lines of code only.

Facjad
July 16th, 2025, 16:50
Thanks for your reply.
Could you explain to me how to override a core function?
I tried:


function CharEncumbranceManager.calcDefaultEncumbrance(node Char)
Debug.console("Call to Custom Encum (total) Calc!")
...
end

But I got:

[7/16/2025 5:46:34 PM] [ERROR] Failed script initialization (P:CustomWeight): [string "Houserules:../change_weight_computation.lua"]:29: attempt to index global 'CharEncumbranceManager' (a nil value)
In Core and 4E rulesets, the functions are called through CharEncumbranceManager, for example:

CharEncumbranceManager.addStandardCalc();

Moon Wizard
July 16th, 2025, 17:05
Just pushed an update to the beta Test channel to remove the automatic initialization on custom calc assignment; and leave that for the onTabletopInit phase of loading. So, you should be able to register an override in an extension onInit global script onInit function in that version.

This version will be limited to beta Test channel until release in early August.

Regards,
JPG

Facjad
July 16th, 2025, 21:54
Thanks a lot, it works now! :D