PDA

View Full Version : Getting Button to Run LUA



augustgames
March 27th, 2024, 02:20
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_value").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 60384 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_value").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.

Trenloe
March 27th, 2024, 03:59
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 (https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644909/buttonfield) or a buttoncontrol (https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644896/buttoncontrol)?

getDatabaseNode will only work for a buttonfield control as that is bound to a databasenode, whereas buttoncontrol is not bound.

augustgames
March 27th, 2024, 15:57
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_value").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!

superteddy57
March 27th, 2024, 16:19
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

Trenloe
March 27th, 2024, 19:03
The issue is that you are using a control. Controls are not connected to the database like a field would be.
As mentioned above... ;)

augustgames
March 27th, 2024, 19:15
Thank you so much Trenloe. I appreciate your efforts here. I am having a blast getting under the hood of Fantasy Grounds.

Trenloe
March 27th, 2024, 19:22
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... :D