FG Spreadshirt Swag
  1. #1

    Getting Button to Run LUA

    So I am still learning and playing and I wanted to make a calculation of one field affect another. I used onValue change for strength_value. This works PERFECTLY.

    function onValueChanged()
    local sPwr = getDatabaseNode().getParent().getChild("pwr_value" ).getValue()
    local sStr = getDatabaseNode().getParent().getChild("strength_v alue").getValue()


    -- strength_value sStr
    if sStr >= 1 and sStr <= 3 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-4 + sPwr)
    elseif sStr >= 4 and sStr <= 5 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-3 + sPwr)
    elseif sStr >= 6 and sStr <= 7 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-2 + sPwr)
    elseif sStr >= 8 and sStr <= 9 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-1 + sPwr)
    elseif sStr >= 10 and sStr <= 11 then getDatabaseNode().getParent().getChild("str_mod"). setValue(0 + sPwr)
    elseif sStr >= 12 and sStr <= 13 then getDatabaseNode().getParent().getChild("str_mod"). setValue(1 + sPwr)
    elseif sStr >= 14 and sStr <= 15 then getDatabaseNode().getParent().getChild("str_mod"). setValue(2 + sPwr)
    elseif sStr >= 16 and sStr <= 17 then getDatabaseNode().getParent().getChild("str_mod"). setValue(3 + sPwr)
    elseif sStr >= 18 and sStr <= 19 then getDatabaseNode().getParent().getChild("str_mod"). setValue(4 + sPwr)
    elseif sStr >= 20 and sStr <= 21 then getDatabaseNode().getParent().getChild("str_mod"). setValue(5 + sPwr)
    elseif sStr >= 22 and sStr <= 23 then getDatabaseNode().getParent().getChild("str_mod"). setValue(6 + sPwr)
    elseif sStr >= 24 and sStr <= 25 then getDatabaseNode().getParent().getChild("str_mod"). setValue(7 + sPwr)
    end

    end

    However, if I run the same code on the same page and run it from a button (+-) in pic Capture.JPG I get an error that says "[3/26/2024 6:16:08 PM] [ERROR] Script execution error: [string "C:charsheet_main:update_stat"]:3: attempt to index a nil value". I have tried to research this with no answer. Idea?

    function onButtonPress()

    local sPwr = getDatabaseNode().getParent().getChild("pwr_value" ).getValue()
    local sStr = getDatabaseNode().getParent().getChild("strength_v alue").getValue()

    -- strength_value sStr
    if sStr >= 1 and sStr <= 3 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-4 + sPwr)
    elseif sStr >= 4 and sStr <= 5 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-3 + sPwr)
    elseif sStr >= 6 and sStr <= 7 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-2 + sPwr)
    elseif sStr >= 8 and sStr <= 9 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-1 + sPwr)
    elseif sStr >= 10 and sStr <= 11 then getDatabaseNode().getParent().getChild("str_mod"). setValue(0 + sPwr)
    elseif sStr >= 12 and sStr <= 13 then getDatabaseNode().getParent().getChild("str_mod"). setValue(1 + sPwr)
    elseif sStr >= 14 and sStr <= 15 then getDatabaseNode().getParent().getChild("str_mod"). setValue(2 + sPwr)
    elseif sStr >= 16 and sStr <= 17 then getDatabaseNode().getParent().getChild("str_mod"). setValue(3 + sPwr)
    elseif sStr >= 18 and sStr <= 19 then getDatabaseNode().getParent().getChild("str_mod"). setValue(4 + sPwr)
    elseif sStr >= 20 and sStr <= 21 then getDatabaseNode().getParent().getChild("str_mod"). setValue(5 + sPwr)
    elseif sStr >= 22 and sStr <= 23 then getDatabaseNode().getParent().getChild("str_mod"). setValue(6 + sPwr)
    elseif sStr >= 24 and sStr <= 25 then getDatabaseNode().getParent().getChild("str_mod"). setValue(7 + sPwr)
    end

    end

    ignore the space between the v and a in "strength_value". I am not sure why it is showing up that way.
    Last edited by augustgames; March 27th, 2024 at 02:21. Reason: clarify
    Daniel A George

  2. #2
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,470
    Assuming the code you list is the whole <script> code for the button, then the error is being caused at line 3 - probably because getDatabaseNode() doesn't return a valid database node.

    How is the button control defined? Is it a buttonfield or a buttoncontrol ?

    getDatabaseNode will only work for a buttonfield control as that is bound to a databasenode, whereas buttoncontrol is not bound.
    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
    Got it!

    It worked

    <buttonfield name="button_stats">
    <bounds>83,41,19,25</bounds>
    <allowdoubleclick />
    <icon normal="button_action_effect" />
    <script>
    function onDoubleClick(x, y)

    local sPwr = getDatabaseNode().getParent().getChild("pwr_value" ).getValue()
    local sStr = getDatabaseNode().getParent().getChild("strength_v alue").getValue()

    -- strength_value sStr
    if sStr &gt;= 1 and sStr &lt;= 3 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-4 + sPwr)
    elseif sStr &gt;= 4 and sStr &lt;= 5 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-3 + sPwr)
    elseif sStr &gt;= 6 and sStr &lt;= 7 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-2 + sPwr)
    elseif sStr &gt;= 8 and sStr &lt;= 9 then getDatabaseNode().getParent().getChild("str_mod"). setValue(-1 + sPwr)
    elseif sStr &gt;= 10 and sStr &lt;= 11 then getDatabaseNode().getParent().getChild("str_mod"). setValue(0 + sPwr)
    elseif sStr &gt;= 12 and sStr &lt;= 13 then getDatabaseNode().getParent().getChild("str_mod"). setValue(1 + sPwr)
    elseif sStr &gt;= 14 and sStr &lt;= 15 then getDatabaseNode().getParent().getChild("str_mod"). setValue(2 + sPwr)
    elseif sStr &gt;= 16 and sStr &lt;= 17 then getDatabaseNode().getParent().getChild("str_mod"). setValue(3 + sPwr)
    elseif sStr &gt;= 18 and sStr &lt;= 19 then getDatabaseNode().getParent().getChild("str_mod"). setValue(4 + sPwr)
    elseif sStr &gt;= 20 and sStr &lt;= 21 then getDatabaseNode().getParent().getChild("str_mod"). setValue(5 + sPwr)
    elseif sStr &gt;= 22 and sStr &lt;= 23 then getDatabaseNode().getParent().getChild("str_mod"). setValue(6 + sPwr)
    elseif sStr &gt;= 24 and sStr &lt;= 25 then getDatabaseNode().getParent().getChild("str_mod"). setValue(7 + sPwr)
    end


    end

    </script>
    </buttonfield>

    Thank you!
    Last edited by augustgames; March 27th, 2024 at 16:19. Reason: Clarifation
    Daniel A George

  4. #4
    The issue is that you are using a control. Controls are not connected to the database like a field would be. In your code, you are navigating to the str_mod by the database with getDatabaseNode() on the original control that does appear to be a field connected to the databaase. So for controls you would need to find a field/window to pull the database data to locate what you are trying to get.

    Example
    function onButtonPress()
    local nodeChar = window.getDatabaseNode(); -- this gives you the parent window's node
    local sPwr = DB.getValue(nodeChar, "pwr_value", "");
    local sStr = DB.getValue(nodeChar, "strength_value", "");

    local nMod = 0;
    if sStr &gt;= 1 and sStr &lt;= 3 then
    nMod = -4 + sPwr;
    ...
    end

    DB.setValue(nodeChar, "str_mod", "number", nMod);
    end
    Dominic Morta
    Ruleset Developer
    Smiteworks

    How to zip up your campaign if the Developers ask for it-How to zip up your campaign if the Developers ask for it

    How to provide an Unity Connection issue?-Connection Issues and What to Provide

    Unity Updater issue?-Updater Issues

    Classic and Unity Port Forwarding?-Fantasy Grounds Connections Explained

    Comcast or Cox ISP User?-Comcast XFinity and Cox Users

    Have a suggestion?-Feature Request

  5. #5
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,470
    Quote Originally Posted by superteddy57 View Post
    The issue is that you are using a control. Controls are not connected to the database like a field would be.
    As mentioned above...
    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 so much Trenloe. I appreciate your efforts here. I am having a blast getting under the hood of Fantasy Grounds.
    Daniel A George

  7. #7
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,470
    Quote Originally Posted by augustgames View Post
    Thank you so much Trenloe. I appreciate your efforts here. I am having a blast getting under the hood of Fantasy Grounds.
    You're very welcome. Glad we sorted this one out and also that you're enjoying discovering the mysteries of FG coding...
    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!

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