Thread: Getting Button to Run LUA
-
March 27th, 2024, 02:20 #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
-
March 27th, 2024, 03:59 #2
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!
-
March 27th, 2024, 15:57 #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 >= 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
</script>
</buttonfield>
Thank you!Last edited by augustgames; March 27th, 2024 at 16:19. Reason: Clarifation
Daniel A George
-
March 27th, 2024, 16:19 #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 >= 1 and sStr <= 3 then
nMod = -4 + sPwr;
...
end
DB.setValue(nodeChar, "str_mod", "number", nMod);
endDominic 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
-
March 27th, 2024, 19:03 #5Private 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!
-
March 27th, 2024, 19:15 #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
-
March 27th, 2024, 19:22 #7Private 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