PDA

View Full Version : OnInit() not being called for sub_record



Olog
May 24th, 2018, 22:35
Continuing to work on an extension that adds a button to the encounter record. My code looks something like this:


<root>
<base>
<script file="scripts/somescript.lua" name="somescript"/>
<windowclass name="battle" merge="join">
...
<sheetdata>
...
<button_text_sm name"button1">
...
<script>
function onButtonPress()
somescript.somefunction();
end
</script>
</button_text_sm>
</sheetdata>
</windowclass>
<base>
</root>


The somescript.lua file contains an function onInit() that sets up some options. However the function onInit() defined in <windowsclass name="battle_header"> which is a sub_window of the <windowsclass name="battle"> doesn't seem to get called. Have I unintentionally overwritten it with my somescript.lua's onInit()?

- Olog

Trenloe
May 24th, 2018, 22:42
Continuing to work on an extension that adds a button to the encounter record. My code looks something like this:


<root>
<base>
<script file="scripts/somescript.lua" name="somescript"/>
<windowclass name="battle" merge="join">
...
<sheetdata>
...
<button_text_sm name"button1">
...
<script>
function onButtonPress()
somescript.function();
end
</script>
</button_text_sm>
</sheetdata>
</windowclass>
<base>
</root>


The somescript.lua file contains an function OnInit() that sets up some options. However the function OnInit() defined in <windowsclass name="battle_header"> which is a sub_window of the <windowsclass name="battle"> doesn't seem to get called. Have I unintentionally overwritten it with my somescript.lua's OnInit()?

- Olog
"function" is a reserved word in LUA - you don't call something called "function".

Assuming your "Function" was called OnInit, then you'd call it with somescript.OnInit()

Be careful using OnInit though... This is very close to a script event called onInit (Yes, LUA is case sensitive). Which will be executed when the global script package is first loaded.

I'd recommend you use something like function myInit() in scripts/somescript.lua and call this with somescript.myInit()

See info on scripting here: https://www.fantasygrounds.com/wiki/index.php/Developer_Guide_-_Rulesets_-_Scripting

Trenloe
May 24th, 2018, 22:57
Info about the onInit function in a script package can be found here: https://www.fantasygrounds.com/refdoc/script.xcp#onInit

Olog
May 25th, 2018, 15:35
In attempt to generalize code in my original post, I inadvertently used the "function" keyword to represent a particular function. It would be better represented by "somescript.somefunction()".

I also miscapitalized "onInit()" in my post, but not in my actual code. I am refering to the "onInit()" function that is called when the package is evaluated.

I've edited my original post with the above info, though I couldn't edit the title. Can you re-evaluate my question based on the correct info?

To state my question another way (and abstracting a bit):
My extension (myExt) expands a class from coreRPG called "battle".
coreRPG.battle contains an onInit() handler.
My derived class myExt.battle contains a differnt onInit() handler.
How can I call coreRPG.battle.onInit() from within myExt.battle.onInit()?

Trenloe
May 25th, 2018, 16:04
How can I call coreRPG.battle.onInit() from within myExt.battle.onInit()?
OK, right - that's a completely different question! ;)

See the "super" script scope here: https://www.fantasygrounds.com/wiki/index.php/Developer_Guide_-_Rulesets_-_Scripting#Script_Block_Scope

Use super.onInit() to call a function from an higher-up script in the ruleset/extension layering. If you get an error that super is nil, then there is no access to the higher up script. Get around the error by using:


if super then
super.onInit();
end

So you only call the higher up function is the super script scope exists.

Olog
May 25th, 2018, 18:56
Thank you for your help. I did learn quite a few things from your comments and links.

It turns out my issue wasn't really present. I picked the wrong encouters to test with. The encounters from the DMG never had their XP and CR entries filled in. I thought it was my extension that was causing it, but it's just how they are in the DMG.