5E Character Create Playlist
  1. #1

    OnInit() not being called for sub_record

    Continuing to work on an extension that adds a button to the encounter record. My code looks something like this:
    Code:
    <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
    Last edited by Olog; May 25th, 2018 at 15:18.

  2. #2
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,409
    Quote Originally Posted by Olog View Post
    Continuing to work on an extension that adds a button to the encounter record. My code looks something like this:
    Code:
    <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/...ts_-_Scripting
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  3. #3
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,409
    Info about the onInit function in a script package can be found here: https://www.fantasygrounds.com/refdoc/script.xcp#onInit
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  4. #4
    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()?

  5. #5
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,409
    Quote Originally Posted by Olog View Post
    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/...pt_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:

    Code:
    if super then
      super.onInit();
    end
    So you only call the higher up function is the super script scope exists.
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  6. #6
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
DICE PACKS BUNDLE

Log in

Log in