DICE PACKS BUNDLE
  1. #1

    Join Date
    Jun 2013
    Location
    Isanti, MN
    Posts
    2,922

    Bit of programming help

    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?

  2. #2
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,404
    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:
    Code:
    <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":
    Code:
    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/refdo...e.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.
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  3. #3

    Join Date
    Jun 2013
    Location
    Isanti, MN
    Posts
    2,922
    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.

  4. #4
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,404
    Quote Originally Posted by Andraax View Post
    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.
    Code:
    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
    Last edited by Trenloe; October 21st, 2014 at 01:09.
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  5. #5
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,404
    Thinking of creating a welcome banner? https://www.fantasygrounds.com/forum...n-They-Connect
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  6. #6

    Join Date
    Jun 2013
    Location
    Isanti, MN
    Posts
    2,922
    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.

  7. #7
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,404
    Quote Originally Posted by Andraax View Post
    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/forum...l=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.
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  8. #8

    Join Date
    Jun 2013
    Location
    Isanti, MN
    Posts
    2,922
    Quote Originally Posted by Trenloe View Post
    Sorry, too slow...
    Yeah, dang job taking up all my time today. :-)

  9. #9

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
FG Spreadshirt Swag

Log in

Log in