PDA

View Full Version : Could use some help with my LUA Scrip code



Dragon_of_old
April 23rd, 2025, 02:22
Hello everyone,

I'm working on creating a rule set using the Ruleset wizard for the XML part, but I'm having an issue with my LUA script. So I have a window where my players will be able to create custom spells for my game system, and this window facilitates that part.

So I'm trying to do it mechanically, when the player clicks a checkbox for the "random turns" line of the window, the Lua script takes the value of -8 from one number field and places it in the total number field. But I keep getting this error.

64130

I am still very unfamiliar with Lua, so I may be missing an elementary step in making this script work.

64131

Here is my current script.

function onValueChanged()
local infoGet = window.getDatabaseNode();
local RTcheck = infoGet.getChild("r_t_check").getValue();
local ptsDef= infoGet.getChild("defaul_score").getValue();
local ptsVal = infoGet.getChild("2_base_pts_num_fie").getValue();
local putPTS = infoGet.getChild("2_tol").setValue(ptsVal);

if RTcheck == 0 then
RTcheck = ptsDef
elseif RTcheck == 1 then
RTcheck = ptsVal
end
end

Any insight is welcomed, thank you for your time

superteddy57
April 23rd, 2025, 04:31
Let's start by cleaning up the code:


function onValueChanged()

local infoGet = window.getDatabaseNode();
local RTcheck = DB.getValue(infoGet, "r_t_check", ""); -- Not sure of data type but you can change "" to 0 for a nil return
local ptsDef= DB.getValue(infoGet, "defaul_score", ""); -- Same as above
local ptsVal = DB.getValue(infoGet, "2_base_pts_num_fie", ""); -- Same as above
local putPTS = DB.setValue(infoGet, "2_tol", "string", ptsVal);; -- Assuming a string type here


if RTcheck == 0 then

RTcheck = ptsDef
elseif RTcheck == 1 then

RTcheck = ptsVal
end
end


The error is pointing to either not finding the control or the database does not have that child. With the above it will check the database for the child of the parent node and if it's nil, it will return an empty string. You can basically put what you want here, but I would suggest keeping it the same data type. If it's not a string, you would replace "" with 0 and when you set the value it would be "number" instead of "string". This is just based off the error you posted. I would suggest checking out CoreRPG and the other provided rulesets for examples of pulling data from the database or controls. There is also the dev portal on the wiki that will provide more details on client API's: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644285/Developer+Guide

superteddy57
April 23rd, 2025, 04:33
Also, the discord has a coding_help channel you are free to ask away in and we can help in real time.

https://discord.gg/7y2Fs6yq

Dragon_of_old
April 23rd, 2025, 05:52
Thank you for the help and sources.

Dragon_of_old
April 28th, 2025, 21:31
I appreciate your help earlier.

I decided to go in a different direction. The player will have a number field where they can insert a number, which will multiply the score. The total is input in another field.

Here is the new error code I'm getting.
64195

This is the code
function onValueChanged()
local nodeWin = window.getDatabaseNode(); -- This is looking up the window
local buyPTS = DB.getValue(nodeWin, "1_buy", 0); -- This is getting the value from the player
local ptsTOT = math.floor(10*buyPTS); -- This is calculating the score value against the player's input

nodeWin.getChild("1_tot").setValue(ptsTOT); -- This is placing the results of the previous line in the "Pts cost" number field
end

Any insight would be helpful.

superteddy57
April 28th, 2025, 22:01
nodeWin.getChild("1_tot").setValue(ptsTOT);

-->

DB.setValue(nodeWin, "1_tot", "number", ptsTOT);

Dragon_of_old
April 28th, 2025, 22:31
thank you

superteddy57
April 28th, 2025, 22:54
I would suggest checking out the wiki for more information on using the API's and such for the program
https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644582/DB