PDA

View Full Version : Confusing behavior in super/self code layers



Minty23185Fresh
October 27th, 2017, 20:33
I am working on an extension for the "master index" Library editors. In my initial explorations I've come across some perplexing behavior that I'm hoping someone might be able to explain (or confirm my interpretation of it).

I'm looking at code existence and scope within multiple extensions operating on the same ruleset code.

The first few lines of the masterindex windowclass definition in the
rulesets\CoreRPG\campaign\campaign_masterindex.xml file are:
(NOTE: I removed a few lines of code from below, just before <script>, because they are not relevant to this discussion)


<windowclass name="masterindex">
<script file="campaign/scripts/masterindex_window.lua" />
<sheetdata>


The rulesets\CoreRPG\campaign\scripts\masterindex_wind ow.lua file contains an onInit() function, and I have placed a Debug.console() statement in it.

Here is the first try of my extension's modification of the windowclass:


<windowclass name="masterindex" merge="join">
<script merge="join">
function onInit()
Debug.console("Ext | campaign_masterindex.xml | masterindex.onInit() | arrived");
end
</script>
</windowclass>


When I brought up the Spells editor it was completely blank, just a basic skeleton of a dialog, leading me to believe that the ruleset's onInit() function is not being called (since my extension's onInit() probably has replaced it).

So, putting my new knowledge of the existence of code layering into practice I tried to access the ruleset onInit() with this modification of my extension's windowclass:


<windowclass name="masterindex" merge="join">
<script merge="join">
function onInit()
Debug.console("Ext | campaign_masterindex.xml | masterindex.onInit() | arrived");
if super.onInit() then
Debug.console("Ext | campaign_masterindex.xml | masterindex.onInit() | calling super.onInit()");
super.onInit()
else
Debug.console("Ext | campaign_masterindex.xml | masterindex.onInit() | no super.onInit()");
end
end
</script>
</windowclass>


NOW FOR THE PERPLEXING PART. Here is the console output when I brought up the Library Spells editor:


Runtime Notice: Host session started
Runtime Notice: s'Ext | campaign_masterindex.xml | masterindex.onInit() | arrived'
Runtime Notice: s'Core | masterindex_window.lua | onInit() | arrived'
Runtime Notice: s'Ext | campaign_masterindex.xml | masterindex.onInit() | no super.onInit()'


My extension's onInit() started. Execution started in the CorePRG ruleset onInit(). When execution came back to my extension's onInit() the if statement's else clause executed to inform me that the Core's onInit() doesn't exist. (Yet it already RAN!!!) BTW, the editor came up fully populated and in functioning order.

I interpret this as follows:
To evaluate the if clause, i.e. the existence of "super.onInit()", lua had to actually "instantiate" it and in the process of doing so executed it. Once the ruleset's .lua script existed it was replaced by my onInit() (and other code?) and so the if evaluated as false.

Does this sound reasonable? Are there other explanations?

Moon Wizard
October 28th, 2017, 01:25
That's because your check for super.onInit includes parentheses; which means that instead of checking for the existence of the function like you thought you were doing, you are actually executing the function and the return value is used for the if statement (which is always nil).

Try this:


<windowclass name="masterindex" merge="join">
<script merge="join">
function onInit()
Debug.console("Ext | campaign_masterindex.xml | masterindex.onInit() | arrived");
if super.onInit then
Debug.console("Ext | campaign_masterindex.xml | masterindex.onInit() | calling super.onInit()");
super.onInit()
else
Debug.console("Ext | campaign_masterindex.xml | masterindex.onInit() | no super.onInit()");
end
end
</script>
</windowclass>


Regards,
JPG

Minty23185Fresh
October 28th, 2017, 01:45
That's because your check for super.onInit includes parentheses..

Doh!!! I hate it when I do something stupid like that. I guess I was half (-assed) right, just too dumb to recognize why!

Thanks Moon Wizard!

Moon Wizard
October 29th, 2017, 00:09
No worries. Every coder makes these kinds of mistakes. It’s part of the process. :)

Cheers,
JPG