STAR TREK 2d20
View RSS Feed

Minty23185Fresh

A Neophyte Tackles the FG Extension - Dumping DB's Field Names (2 of 2)

Rate this Entry
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.)

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
I only add two statements to my code, and a couple comments. Again, save, reload, open character. Here is my output to the console:

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
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>.

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.

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
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.

Final step! Comment out some of the console calls and add the recursion call.

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);
  
  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
Detour: In code the hierarchical relationship of objects is written using a dot notation, “.” For example: obj1.subobj2.subobj3

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.

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'
Until next time keep on role playing.

Submit "A Neophyte Tackles the FG Extension - Dumping DB's Field Names (2 of 2)" to Digg Submit "A Neophyte Tackles the FG Extension - Dumping DB's Field Names (2 of 2)" to del.icio.us Submit "A Neophyte Tackles the FG Extension - Dumping DB's Field Names (2 of 2)" to Google Submit "A Neophyte Tackles the FG Extension - Dumping DB's Field Names (2 of 2)" to Facebook Submit "A Neophyte Tackles the FG Extension - Dumping DB's Field Names (2 of 2)" to Twitter

Updated May 18th, 2016 at 02:12 by Minty23185Fresh

Categories
Uncategorized

Comments

  1. Ellspeth's Avatar
    I am one of the neophytes this addresses I think. I was in the generation that parents fought allowing calculators in school (watching young people struggle at the cash register if power or programming fails makes me wonder if they should have lost that argument). I took the liberty of pasting it to a word document so I can mark it with notes and reference wherever I go.
    I strongly advocate lifetime learning, this is a chance to go back and try something my generation was just a bit behind with. Especially looking forward to how to do skins and decals for the tabletop.
    Haven't worked with it much just yet, everything to do with RPG's and Fantasy Grounds is still a huge learning curve for me, though in 2 and a half years I made massive progress, 40 years woth of material I won't live long enough to go through all of it.
  2. Minty23185Fresh's Avatar
    Thanks Ellspeth. I'm looking forward to you teaching me about skins and decals!
DICE PACKS BUNDLE

Log in

Log in