Thread: Bit of programming help
-
October 21st, 2014, 00:32 #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?
-
October 21st, 2014, 00:53 #2
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>
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");
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!
-
October 21st, 2014, 00:57 #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.
-
October 21st, 2014, 01:01 #4
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!
-
October 21st, 2014, 01:02 #5
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!
-
October 21st, 2014, 01:28 #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.
-
October 21st, 2014, 01:40 #7
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!
-
October 21st, 2014, 04:35 #8
- Join Date
- Jun 2013
- Location
- Isanti, MN
- Posts
- 2,922
-
October 21st, 2014, 12:10 #9
ololol - i was wondering where this was going!
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks