View Full Version : Get a database node from a chat command
peterb
February 10th, 2023, 09:47
I'm trying to figure out how I can do the equivalent of "window.getDatabaseNode()" from a chat command.
My end objective is to run an existing global function from a hotkey.
Trenloe
February 10th, 2023, 10:06
The callback function that runs from a chat slash command doesn't have a window scope, so window.getDatabaseNode() won't work - as you're probably aware based off this question.
I think we'll need a bit more information regarding exactly what you're trying to do.
Where is the database node you're trying to access?
Can you access it through the standard database hierarchy? You can use database paths to get to specific nodes in the database: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644468/Ruleset+-+Database#Database-Paths
peterb
February 10th, 2023, 10:36
I'm trying to build a chat command to run the following function, from Basic Roleplaying /scripts/global.lua. I need to get the name of the current character sheet and a databasenode. Missile can be hard coded as true or false.
The reason I want to do this is that this code is accessed from a button so it cannot be dragged to a hotkey. And to run it the player have to switch from the main tab to the armor tab and then navigate to the lower right corner of the sheet, all of which takes time and slows combat down. So the aim is to speed up the flow of combat.
function RollHitLocation(name,listnode,missile)
local cust = {};
local sender;
if type(listnode)~="databasenode" then
return;
end
-- who is invoking this roll?
if Session.IsHost then
sender = GmIdentityManager.getCurrent();
else
sender = User.getIdentityLabel();
end
-- set the narrative
if name==sender then
desc = "Hit location";
else
desc = name.." hit location";
end
if missile then
desc = desc.." (missile)";
end
-- create the custom data
cust.sender = sender;
cust.path = listnode.getPath();
cust.missile = missile;
-- do it!
local rActor = ActorManager.resolveActor(listnode)
ActionHitLocation.performRoll(draginfo, rActor, desc, cust);
end
The buttons code is:
function onClickDown(...)
rollHitLocation(Input.isShiftPressed());
return true;
end
function rollHitLocation(flag)
local charnode = window.getDatabaseNode();
local name = charnode.getChild("name").getValue();
local listnode = charnode.getChild("hitlocations");
if listnode then
Global.RollHitLocation(name,listnode,flag);
end
end
Trenloe
February 10th, 2023, 11:25
Thanks for the extra information. There could be a number of PC sheets open at any one time and so it could be hard to determine which PC sheet to use; maybe the best way to do this is to look at the current active PC for the session - the one that the player has most recently clicked on in the top left of the desktop (the name turns bold). The problem with this is that it won't work for the GM.
So, you can either try to find an open charsheet window using Interface.findWindow: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644632/Interface#findWindow You'll have to use "" for the datasource (as you won't know it), but I'm not sure if this will return one, or more, charsheet open windows.
Or, try using the currently active PC as mentioned above. Use User.getCurrentIndentity: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644683/User#getCurrentIdentity
So, you could get the charsheet database node of the currently active identity for a player instance using DB.findNode("charsheet." .. User.getCurrentIdentity())
superteddy57
February 10th, 2023, 11:31
If you can tell me what control you are trying to make accessible I can see if we can alleviate the combat woes.
peterb
February 10th, 2023, 12:04
If you can tell me what control you are trying to make accessible I can see if we can alleviate the combat woes.
It's the "roll hitlocation" button in the lower right corner of the armor tab. As it's a button, it cannot be dragged to a hotkey. Everytime a players has to roll on the hitlocation table it takes a few seconds, which quickly gets annoying. As a GM I make these rolls outside FG, it's faster.
You could of course write a new hit location generation function that just prints out the hit location. I was just trying to re-use the existing code.
superteddy57
February 10th, 2023, 12:08
So to help, you want to have it available on the main tab?
Like this?
56139
peterb
February 10th, 2023, 12:59
So to help, you want to have it available on the main tab?
Like this?
56139
Yes, that would certainly make things move faster.
superteddy57
February 10th, 2023, 13:06
Ok, I'll see if I can get it into the TEST build.
peterb
February 10th, 2023, 13:50
Great! Thanks.
peterb
August 8th, 2023, 14:48
Thanks for the extra information. There could be a number of PC sheets open at any one time and so it could be hard to determine which PC sheet to use; maybe the best way to do this is to look at the current active PC for the session - the one that the player has most recently clicked on in the top left of the desktop (the name turns bold). The problem with this is that it won't work for the GM.
So, you can either try to find an open charsheet window using Interface.findWindow: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644632/Interface#findWindow You'll have to use "" for the datasource (as you won't know it), but I'm not sure if this will return one, or more, charsheet open windows.
Or, try using the currently active PC as mentioned above. Use User.getCurrentIndentity: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644683/User#getCurrentIdentity
So, you could get the charsheet database node of the currently active identity for a player instance using DB.findNode("charsheet." .. User.getCurrentIdentity())
Thanks Trenloe for the pointer to User.getCurrentIdentity(). While adding a RollHitLoc button on the main tab did speed the flow up, I still think the combat flow could become even faster, if the hit location roll could be stored in a hot-key. The solution I came up with is the following:
--------------------------------
-- roll_hit_location_command.lua
--------------------------------
function onTabletopInit()
addRollHitLocationCommand()
end
function addRollHitLocationCommand()
-- Roll hit location, indicate "melee" for melee hit location roll or "missile" for missile hit
-- location roll, for example: "/rhl missile" or "/rhl melee".
Comm.registerSlashHandler("rhl", rollhitlocation, "[missile] <isMissile>")
end
function rollhitlocation(sCommand, sParams)
-- Everything after "/rhl" command ends up in "sParams".
if string.lower(sParams) == "melee" then
flag = false
elseif string.lower(sParams) == "missile" then
flag = true
else
ChatManager.SystemMessage("The '/rhl' command takes on of two parameters 'missile' or 'melee'.")
return
end
local currentID = User.getCurrentIdentity()
local charnode = DB.findNode("charsheet." .. currentID)
local name = charnode.getChild("name").getValue();
-- If the use hit locations option is not "on" then there are no hit locations,
-- and we will find no "hitlocatio0ns node", so a hit location roll is meaningless.
local listnode = charnode.getChild("hitlocations");
if listnode then
Global.RollHitLocation(name,listnode,flag);
else
ChatManager.SystemMessage("The 'Use Hit Locations' option is not set to 'On'. Rolling for a hit location is not supported.")
return
end
end
Powered by vBulletin® Version 4.2.1 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.