PDA

View Full Version : masterindex_buttons Button DB Node



Mike Serfass
March 18th, 2024, 05:20
If I add a button using

LibraryData.addIndexButton("npc", "button_makenpc")
how do I get the DB node of the list window that button is on?

I add this button to the NPCs list window.
I want the "npc" node in db.xml. I'd like to use

window.getDatabaseNode().getNodeName()
rather than hard-coding "npc".

But

window.getDatabaseNode()
returns nil, because window is an instance of masterindex_buttons.
I need the container of masterindex_buttons but I can't figure out how to get it.


Thanks for the help.

superteddy57
March 18th, 2024, 06:13
The npc node is a parent node under the root in the database. The quick and not secure way is to useDB.findNode("npc") or DB.createNode("npc") to access the parent node. You can also use LibraryData.getRootMapping("npc") as well to get the string instead of the node and then throw the return into DB.findNode or DB.createNode

Mike Serfass
March 18th, 2024, 18:05
The issue is getting the node a list window is bound to; the node for which it's listing items.
In this case, I know the "npc" node is the one being used.
If I want to write shared code for a button that can be put on any list window, I need to use getDatabaseNode. Otherwise, I use an unbound button template then have specific overridden buttons for each list window I use it on.
If a node name in db.xml changes, the button breaks until I change the hard-coded value.
It seems I can't use getDatabaseNode any longer in some cases with the recent update.
I can't figure out how to get to the container, which does have a bound node I can reference with getDatabaseNode.

Thanks for the LibraryData.getRootMapping("npc"). I didn't know about that function. Still, I need to know the node ahead of time and hard code that parameter. But I suspect that will catch node name changes in db.xml.

superteddy57
March 18th, 2024, 18:07
Well if you are in the window itself, then you can look at the masterindex_window.lua script file as that has the functions available that can assist. The one that comes to mind would get getRecordType() if you are generalizing the code. This will return the root type that can be used in the other function calls I listed.

Mike Serfass
March 18th, 2024, 18:22
I'm not in masterindex_window. That's the problem.
I'm in masterindex_buttons.

Debug.chat("window", window)
returns
s'window' | windowinstance = { class = masterindex_buttons, node = nil, x,y,w,h = 0,0,430,27 }

Here's the button:


<template name="button_makenpc">
<button_text_hotkey>
<anchored to="buttonanchor" width="70">
<top />
<left anchor="right" relation="relative" offset="5" />
</anchored>
<state>
<textres>npc_button_makenpc</textres>
</state>
<tooltip textres="tooltip_makenpc" />
<type>makenpc</type>
<icon>button_export</icon>
<script>
function onButtonPress()
-- this returns masterindex_buttons
Debug.console("window", window)
-- this is the call I want to make; but it returns nil and errors
--NPCMaker.makeNPCFromClipboardData(window.getDataba seNode().getNodeName())
-- this is the call I have to make for it to work
NPCMaker.makeNPCFromClipboardData("npc")
end
</script>
</button_text_hotkey>
</template>

Trenloe
March 18th, 2024, 18:29
Assuming masterindex_buttons is a subwindow, then you can use parentcontrol (window.parentcontrol) to traverse up the GUI hierarchy. Mentioned here: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996645196/subwindow