A Neophyte Tackles the FG Extension - Dumping DB's Field Names (2 of 2)
by
, May 12th, 2016 at 20:21 (5968 Views)
This session I will implement some Lua script to dump database field names to the console.
Two housekeeping items:
(1) This blog is very long. I was required to divide it into two parts. This is part 2 of 2.
(2) You should consider making your browser full-screen so that the code displays correctly.
After saving the file, reloading the ruleset, and bringing up the character, my console information is as expected. Next step - implementing a terminating condition. (Note: for brevity I am not going to show the openCharacter() function in the code windows anymore, it's not going to change.)
I only add two statements to my code, and a couple comments. Again, save, reload, open character. Here is my output to the console:Code:function dumpNodeNames(oCurr) -- recursive function!, it will call itself, always providing the next node -- get the path of the provided node and send it to the console, -- the path is just a string listing all nodes to this node local sPath = DB.getPath(oCurr); Debug.console("ZYX:charselect_host_entry.lua:dumpNodeNames():path", sPath); -- if there are nodes below this one process them, but if not stop -- get a count of the child nodes, use it for terminating the end of a branch local nCount = DB.getChildCount(oCurr, ""); Debug.console("ZYX:charselect_host_entry.lua:dumpNodeNames():child count", nCount); end
Okay, there are 6 children. Does that match the data in the db.xml file, just by chance? Sure enough. There are six children under <id-00001>, they are: <coins>, <encumbrance>, <inventory list>, <maincategorylist>, <name> and <token>.Runtime Notice: Reloading ruleset
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001'
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():ch ild count' | #6
I am going to output the paths. I’ll use a Lua “for” loop.
Detour: Think of a “for” loop as the processing of a bag of items. In this example I’ll use a moving truck instead of a bag. As you unload boxes from the truck you process them one at a time. Each one has an index, say 1, 2, 3, or a text index “Kitchen Glassware”, “Bedroom Nightstand”, “Bathroom Cabinet”. Each box also has some contents. Indexes have to be unique, you could label your boxes “Kitchen 1” and “Kitchen 2” but you can’t have two labeled “Kitchen”.
The syntax for the for loop that I employ in the following code is a bit unique, it iterates through the group of items, grabbing both the index and the data item in pairs. Oftentimes when accessing an item in a group of items, using for, you need to get the item using its index. That can be problematic if you don’t know the index. Using the “for”, “in pairs” methodology relieves us of having to know the indexes.
Save, reload, and get character (still no recursion) instead I force the routine to output each child then stop. My output looks great and the strings match up with the XML.Code:function dumpNodeNames(oCurr) -- recursive function!, it will call itself, always providing the next node -- get the path of the provided node and send it to the console, -- the path is just a string listing all nodes to this node local sPath = DB.getPath(oCurr); Debug.console("ZYX:charselect_host_entry.lua:dumpNodeNames():path", sPath); -- if there are nodes below this one process them, but if not stop -- get a count of the child nodes, use it for terminating the end of a branch local nCount = DB.getChildCount(oCurr, ""); Debug.console("ZYX:charselect_host_entry.lua:dumpNodeNames():child count", nCount); -- wrap the recursive call in the terminating condition -- only process children if there are children to process if nCount > 0 then -- there are more nodes, process each one for oIndex,oChild in pairs(DB.getChildren(oCurr, "")) do -- output the node, the documentation states console will attempt to convert to string Debug.console("ZYX:charselect_host_entry.lua:dumpNodeNames():node", oChild); end end end
Final step! Comment out some of the console calls and add the recursion call.
Detour: In code the hierarchical relationship of objects is written using a dot notation, “.” For example: obj1.subobj2.subobj3Code:function dumpNodeNames(oCurr) -- recursive function!, it will call itself, always providing the next node -- get the path of the provided node and send it to the console, -- the path is just a string listing all nodes to this node local sPath = DB.getPath(oCurr); Debug.console("ZYX:charselect_host_entry.lua:dumpNodeNames():path", sPath); -- if there are nodes below this one process them, but if not stop -- get a count of the child nodes, use it for terminating the end of a branch local nCount = DB.getChildCount(oCurr, ""); -- Debug.console("ZYX:charselect_host_entry.lua:dumpNodeNames():child count", nCount); if nCount > 0 then -- there are more nodes, process each one for oIndex,oChild in pairs(DB.getChildren(oCurr, "")) do -- output the node, the documentation states console will attempt to convert to string -- Debug.console("ZYX:charselect_host_entry.lua:dumpNodeNames():node", oChild); dumpNodeNames(oChild); end end end
After my code executes, I end up with about 34 lines in the console. Below is a portion of them. Note how the names of the variables correspond exactly with the tags in the db.xml file.
Until next time keep on role playing.Runtime Notice: Reloading ruleset
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001'
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001.name'
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001.token'
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001.inventorylist'
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001.inventorylist.id-00002'
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001.inventorylist.id-00002.count'
...
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001.coins.slot4'
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001.coins.slot4.amount'
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001.coins.slot6'
Runtime Notice: s'ZYX:charselect_host_entry.lua:dumpNodeNames():pa th' | s'charsheet.id-00001.coins.slot6.amount'