PDA

View Full Version : Overriding function



ApesAmongUs
August 11th, 2020, 20:15
So far I have found 2 ways of overriding a function, but I'm in a situation where I'm not sure which applies.

1. The function is in a lua file and the lua file is included from base.xml (or, really any xml file) -
<script name="CharacterManager" file="scripts/manager_character.lua" />
If that's the case, then I can use the name: CharacterManager.whateverFunctionName = new function

2. The function is part of a script section that is included directly as part of a xml file and only referenced in there
For this I can use merge to override the entire script section instead of just the one function -
<root>
<template name="pc_traitdiebase">
<traitdiebase merge="replace">
<script>
function onInit()

But now I've hit a new situation.

3. The function is in a lua file, but that lua file is not referenced as part of the base.xml. Instead, it is only references once under a windowclass in an xml -
<windowclass name="npc_skillentry">
<sizelimits>
<minimum height="42" />
</sizelimits>
<script file="campaign/scripts/npc_skill.lua"/>
It is never assigned a name for external referencing, so I can't use method 1. But it also doesn't have local code, so method 2 doesn't exactly match either.

I'm thinking I can make a copy of the lua with an altered name. Then use method 2 to merge the new name as a replacement -
<script file="campaign/scripts/npc_skill_replace.lua"/>
Which would mean the new lua file would need to contain all of the functions of the old file even if I am only altering one of them.


Is there a better method of doing this?

Trenloe
August 11th, 2020, 20:18
This might help (or confuse. ;)

https://www.fantasygrounds.com/forums/showthread.php?58755-ct_entry-lua&p=516228&viewfull=1#post516228

ApesAmongUs
August 11th, 2020, 20:49
Yea, that helps. Though, really, I'd almost rather just copy the file and redo changes following any updates than bother with using super to hop up for 7/8 functions in the file. I mean, I was basically right about my solution except for the "super"/dynamic rebuilding of all the other functions.

I'm sure others have said this before, but there really needs to be a better way of doing this.

Trenloe
August 11th, 2020, 20:52
Yea, that helps. Though, really, I'd almost rather just copy the file and redo changes following any updates than bother with using super to hop up for 7/8 functions in the file. I mean, I was basically right about my solution except for the "super"/dynamic rebuilding of all the other functions.
You may not have to use the super calls for functions that you're not overriding. I've done more work with these since I made that post. If the function isn't there in the overriding script then, from my limited testing, I believe it will run the one a layer up.

So, maybe there is a "better" way of doing this! :)

Stv
August 12th, 2020, 14:12
I've just ripped out a lot of functions from an overridden script file I've been working on where I am only overriding 2 of around 12 functions.
With the 10 I'm not needing stripped out of the new lua script, it still looks like it works as intended, so I would say you're correct Trenloe.
Happy days if so, saves alot of extra typing :)

Cheers, Steve.

Trenloe
August 12th, 2020, 19:13
I've just ripped out a lot of functions from an overridden script file I've been working on where I am only overriding 2 of around 12 functions.
With the 10 I'm not needing stripped out of the new lua script, it still looks like it works as intended, so I would say you're correct Trenloe.
Happy days if so, saves alot of extra typing :)

Cheers, Steve.
Cool! Sorry I misled you initially. :)

Stv
August 12th, 2020, 19:35
No harm Trenloe, I appreciated the input, also I've leant a lot from the process :)

Cheers, Steve.