5E Product Walkthrough Playlist
  1. #1

    Join Date
    Mar 2007
    Location
    Vancouver, WA
    Posts
    57

    Getting and manipulating values...

    Hi all,
    I'm just starting out trying to automate fields in my character sheet modification. For the example below I want to get the strength score, multiply it by 0.5 and square the result (stripped out the calculation, formatting, etc. to focus on just getting the score into the field as a first step). Been looking at other code and the reference guide but I'm not really getting a good grasp of how to retrieve an item from the db, modify that item, and place it in a field. Also, what 'handlers' should be in this script. onInit() makes sense because you want to set the value when first created. What about when the source score is updated? How do I handle that? Thanks for any help!
    M


    Code:
    <numberfield name="encumbranceheavyload" source="encumbrance.heavyload">
      <readonly />
      <script>
        function onInit()
          local cr = DB.findNode("charsheet");
          if cr then
            setValue(cr.abilities.strength.score.getvalue());
          end
        end
      </script>				
    </numberfield>

  2. #2
    Ok event handlers are tricky to understand but once you do it will change how you do this forever . Go ahead and read up on the databasenode entry in the Library guide as it's one of the most important ones in there for character sheet stuff.

    Now to make un updater that sets a value use the following type of structure:

    Code:
    <script>
    	function update()
    		base = source.getValue()*0.5
    		setValue(base*base)
    	end
    	function onInit()
    		source = window.strength.getDatabaseNode()
    		source.onUpdate = update
    		update()
    	end
    </script>
    Note I like linking straight to the actual name of the database node instead of using the source but it should return the same value.

  3. #3

    Join Date
    Mar 2007
    Location
    Vancouver, WA
    Posts
    57
    Hmmm. Feel kinda dumb but I'm not getting it. Tried your code as written but didn't work. Looks like it's trying to get the database node for the strength score by looking for a field names 'strength' located on the current window. In my case, though, it's located on a different window.

    How would I just access a db node directly in code? Let's say I want x = 'strength score'? getValue() looks like the method I want but I don't know how to fully specify the name of the node that ends 'abilities.strength.score'.

    Eh. Maybe I'm in over my head. Looking at sample scripts in all of the XML and Lua files hasn't given me a clue how to do this. More samples in the docs would be great - the reference material is good but having a starting point helps.
    M

  4. #4
    Didn't realize it was on another window sorry. The window itself should still return the character sheet node you are on so you can navigate down from there manually if you wanted to. Or you should be be able to access it all in one line with the dot constructors. Try these:

    Long way:
    Code:
    source = window.getDatabaseNode().getChild("abilities").getChild("strength").getChild("score");
    Short way:
    Code:
    source = window.getDatabaseNode().getChild("abilities.strength.score");

  5. #5

    Join Date
    Mar 2007
    Location
    Vancouver, WA
    Posts
    57
    That works perfectly. Thanks!

    So, I'm not getting what "source.onUpdate = update;" does? Is that essentially setting a callback for onUpdate event for strength? Can that node have multiple callbacks? If so, should I call it something specific like "updateHeavyLoad" in case there are multiple update callbacks on that source? Heh, rapidfire questions from the n00b.

    BTW, thanks for all of your help - much appreciated!
    M

  6. #6
    Its basically creating an Event Handler like so: On the source database node (source.), when its updated (onUpdate) call my function (update). So you don't have to call your own function "update" it's just a convention that makes sense.

    A single databasenode can have multiple handlers assigned to it. https://www.fantasygrounds.com/modguide/scripting.xcp is a good thing to read, at the bottom it discusses Event Handlers.

  7. #7

    Join Date
    Mar 2007
    Location
    Vancouver, WA
    Posts
    57
    Thanks. I've been trying to absorb all of the documentation but doing a poor job of it apparently Thanks again for helping me fix this. I've posted the charsheet pages I have finished (well, except for skills) over in the gallery.

    https://www.fantasygrounds.com/forum...5288#post45288

    Been fun working with this but a little frustrating. I've learned to code in other languages (basic, c++) mainly by reading examples and trying things out but some of this stuff - especially the intersection between lua, xml, and database concepts - is a little cryptic to me
    M

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