View Full Version : Gracefully override a function
xelab
February 25th, 2023, 19:24
Hi all,
I am trying to gracefully override a function in 5E/campaign/record_power.xml.
I follow the advice of @Trenloe (link (https://www.fantasygrounds.com/forums/showthread.php?62502-Improving-Extensions-Compatability&p=547258&viewfull=1#post547258)) and @SilentRuin(link (https://www.fantasygrounds.com/forums/showthread.php?62377-Shared-Function-in-Ext&p=546087&viewfull=1#post546087)), but I still do not get expected behavior.
Here is my code:
<root>
<!-- campaign/record_power.xml -->
<windowclass name="power_page" merge="join">
<script>
function onInit()
if super and super.onInit then
super.onInit();
end
end
function updatePowerWindowUses(nodeChar, w)
super.updatePowerWindowUses(nodeChar, w); -- call old function
Debug.chat("works")
end
</script>
</windowclass>
</root>
I expect this code to have no effect except for printing "works" in chat, however, it disables some character sheet functionality, namely removing spell group titles.
What could be the reason for this?
Here is how char sheet looked before the change:
https://www.fantasygrounds.com/forums/attachment.php?attachmentid=56394&d=1677352647
And this is how it looks after:
https://www.fantasygrounds.com/forums/attachment.php?attachmentid=56393&d=1677352642
Moon Wizard
February 25th, 2023, 19:59
I just built a basic extension using your example, selected a 5E campaign, selected the extension, loaded campaign, then opened character sheet actions tab. It was working for me.
Sample extension is attached.
EDIT: Removed sample extension; see later posts for updated version.
Regards,
JPG
xelab
February 25th, 2023, 20:50
I just built a basic extension using your example, selected a 5E campaign, selected the extension, loaded campaign, then opened character sheet actions tab. It was working for me.
Sample extension is attached.
Regards,
JPG
Could you please create a character, add any spell to char sheet, switch to "combat" view? The spell group captions should disappear (as in my screenshots).
This means that some functionality is disabled by this...
Moon Wizard
February 25th, 2023, 21:20
The example code you posted doesn't do anything like that, just spits on a message when initializing and when uses changed (slots, etc.)
Regards,
JPG
SilentRuin
February 25th, 2023, 21:31
I would add that I try to do all my overrides in my manager if not XML scripting. As your example is overriding XML scripting - that can get tricky if everything in the XML script your overriding is not defined just right especially if its got globals its accessing.
xelab
February 25th, 2023, 21:42
The example code you posted doesn't do anything like that, just spits on a message when initializing and when uses changed (slots, etc.)
Regards,
JPG
@Moon Wizard:
That is precisely my point, the script SHOULD NOT do anything like that, it should just spit out a message, but it DOES remove spell-level captions in "Combat" mode.
Could you please check my screenshots again and try switching to "combat" mode on the character with the extension enabled?
I this behavior not reproduced on your end?
I am on the newest patch of the FGU.
@SilentRuin: What do you mean in your "manager"? Do you mean in the .lua file that is associated with the windowclass? In that case, I do get the same results as in the above.
Moon Wizard
February 25th, 2023, 21:58
What do you mean "spell level captions"?
Can you circle an example of a "spell level caption"?
JPG
xelab
February 25th, 2023, 22:10
What do you mean "spell level captions"?
Can you circle an example of a "spell level caption"?
JPG
Here you go:
https://www.fantasygrounds.com/forums/attachment.php?attachmentid=56400&stc=1&d=1677362998
SilentRuin
February 26th, 2023, 04:41
@Moon Wizard:
That is precisely my point, the script SHOULD NOT do anything like that, it should just spit out a message, but it DOES remove spell-level captions in "Combat" mode.
Could you please check my screenshots again and try switching to "combat" mode on the character with the extension enabled?
I this behavior not reproduced on your end?
I am on the newest patch of the FGU.
@SilentRuin: What do you mean in your "manager"? Do you mean in the .lua file that is associated with the windowclass? In that case, I do get the same results as in the above.
I mean a named lua in the extension.xml vs any script defined directly in a windowclass or template xml file (in lua or not). They are not the same. Things that are named have all the data referenced right there and can be overriden and not worry about any super function accessing external data to that function or things externally calling it. In the non named ones they have to be defined perfectly in order to have it called in the super or reference any external data or it simply wipes it out with your local copy of it and if you don't override every single thing that touches it or it accesses in that super script then all the things that call it or access things externally to that super override will not happen. Unless the script your overriding (not just the function) have the self and all that perfectly defined. Most don't.
Moon Wizard
February 26th, 2023, 05:46
Are you sure that you don't have an unpacked CoreRPG or 5E ruleset in your FG data folder?
I'm not seeing that issue with the extension I posted above. (See image.)
EDIT: Hold on, just remembered you mentioned "Combat" mode and seeing something different though not specifically what you saw. Investigating.
Regards,
JPG
Moon Wizard
February 26th, 2023, 07:14
Found the issue you were having with overriding the script. The updatePowerWindowUses actually returns a value that is used by another function in power_page.lua. In your version, you weren't returning anything, which is the same as always returning "nil" value which equates to false on boolean checks.
Here's an updated version of the extension with the return value passed through correctly.
Regards,
JPG
xelab
February 26th, 2023, 08:39
Found the issue you were having with overriding the script. The updatePowerWindowUses actually returns a value that is used by another function in power_page.lua. In your version, you weren't returning anything, which is the same as always returning "nil" value which equates to false on boolean checks.
Here's an updated version of the extension with the return value passed through correctly.
Regards,
JPG
Ah, a rookie mistake on my part. Not only do I need to call the old function, but I also need to return its result.
Thank you for your help as always!
Powered by vBulletin® Version 4.2.1 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.