PDA

View Full Version : Bit of programming help



Andraax
October 21st, 2014, 00:32
Up until now, I have been mostly making changes to the interface, windows, etc. Now I want to write some stuff that actually works with the database for a campaign.

Can someone (Moon Wizard, Trenloe, et al) post a code snippet that will retrieve and access the elements of, say, a particular "notes" node? Say I had a note called "Note 1" that I wanted to retrieve the text from in LUA code?

Trenloe
October 21st, 2014, 00:53
Just as an FYI: notes will contain a a big full of formatted text and it's not a good idea to access and mess around with this using code.

That aside, you'll need to firstly get an idea of how the campaign database is structured and how the database nodes within are referenced. Some info here: https://www.fantasygrounds.com/modguide/database.xcp

And, one of the best things you can do is open up a db.xml file in a campaign directory and look it over. Here is a small example from the notes section of a campaign database with one note in it:

<notes>
<id-00001>
<locked type="number">0</locked>
<name type="string">One</name>
<text type="formattedtext">
<p>This is some text...</p>
<p>A list:</p>
<list>
<li>One</li>
<li>Two</li>
<li>Three.</li>
</list>
<p></p>
</text>
</id-00001>
</notes>
The <notes> section of the database has one entry in it - <id-00001> (this is the common way for FG to reference database records in a list) which has a name field, locked field and text field (of type formatted text).

If you wanted to get access to the <name> field of this notes entry you'd use notes.id-00001.name to refer to the database node <name type="string">One</name>.

You can work on these nodes in two ways - using the DB package from the FG reference: https://www.fantasygrounds.com/refdoc/DB.xcp or the databasenode object: https://www.fantasygrounds.com/refdoc/databasenode.xcp

Usually the DB package is used for higher level operations, or where you know the exact name of the database node you want to operate against. The databasenode object is much more commonly used as this give direct access to a specific databasenode and generally has more API operations against a specific node.

But, say you wanted to access the above notes node and change the name of it to "New Name":

local notesNameDBNode = DB.findNode("notes.id-00001.name");
notesNameDBNode.setValue("New Name");
The first line used a DB package API call to get the database node at notes.id-00001.name: https://www.fantasygrounds.com/refdoc/DB.xcp#findNode
Then, using the database node returned, the <databasenode.>.setValue API command is used to set the "New Name" value: https://www.fantasygrounds.com/refdoc/databasenode.xcp#setValue

Like with most programming, there are different ways to do this and you could have done it all in one line using DB.setValue("notes.id-00001.name", "string", "New Name"); https://www.fantasygrounds.com/refdoc/DB.xcp#setStatic But, I just wanted to provide this as an example of getting a databasenode and then operating against it.

Andraax
October 21st, 2014, 00:57
I need the code to find the node if I know the name (not the nodeid). In the case of your sample, I know "One" but nothing else.

Trenloe
October 21st, 2014, 01:01
I need the code to find the node if I know the name (not the nodeid). In the case of your sample, I know "One" but nothing else.
Oh sorry - I thought you wanted general database coding guidance.

Use this code to search through all of the notes entries and return the databasenode in nodeNote with the first notes node that has a name of One.

local nodeNote = "";
for kNode,vNode in pairs(DB.getChildren("notes")) do
Debug.console("Searching " .. kNode .. " for One");
if DB.getValue(vNode, "name", "") == "One" then
nodeNote = kNode;
Debug.console("Found notes node of: " .. kNode);
break;
end
end

Trenloe
October 21st, 2014, 01:02
Thinking of creating a welcome banner? https://www.fantasygrounds.com/forums/showthread.php?22365-Any-Way-to-Create-a-Welcome-Banner-to-Players-When-They-Connect :)

Andraax
October 21st, 2014, 01:28
That's what I'm working on. I've got it working well with a "fixed" message, but I want it to be able to be easily customized for each campaign.

Trenloe
October 21st, 2014, 01:40
That's what I'm working on. I've got it working well with a "fixed" message, but I want it to be able to be easily customized for each campaign.
Sorry, too slow... ;)

https://www.fantasygrounds.com/forums/showthread.php?22365-Any-Way-to-Create-a-Welcome-Banner-to-Players-When-They-Connect&p=189839&viewfull=1#post189839

The issue you would have had is how to pass the databasenode ID of the story (or note) to the players as the players can't freely search the database for the name of a story (or note).

You'll see in my extension that I use OOB messaging to run the story search and then return the node ID of the story called "MOTD" and pass this via OOB messaging to the user who has just logged in.

Andraax
October 21st, 2014, 04:35
Sorry, too slow... ;)

Yeah, dang job taking up all my time today. :-)

damned
October 21st, 2014, 12:10
ololol - i was wondering where this was going!