PDA

View Full Version : Question about DB.import



Phystus
August 21st, 2012, 03:19
I'm trying to create an extension for 3.5e to add a slash command to import NPC's, similar to the one to import characters. I've got it calling the file dialog, and returning a message that it has imported the NPC, but nothing shows up in the db.

Here's the code I've got:

In manager_chat.lua, I register the slashhandler, and add the process it defines.



function onInit()
if User.isHost() then
Module.onActivationRequested = moduleActivationRequested;
end
Module.onUnloadedReference = moduleUnloadedReference;

Comm.registerSlashHandler("die", processDie);
Comm.registerSlashHandler("mod", processMod);

if User.isHost() then
Comm.registerSlashHandler("importchar", processImport);
Comm.registerSlashHandler("exportchar", processExport);
Comm.registerSlashHandler("importnpc", processImportnpc);
end
end

...

function processImportnpc(sCommand, sParams)
CharManager.importNPC();
end



In manager_char.lua I define the importNPC function:



function importNPC()
local sFile = Interface.dialogFileOpen();
if sFile then
DB.import(sFile, "npc", "category");
ChatManager.SystemMessage("Imported NPC(s) from: " .. sFile);
end
end


I get the file dialog and the system message just fine, but nothing is saved in the database. So I think the 3rd argument of the DB.import call is incorrect, but I can't find what the correct value would be. Any ideas?

Thanks in advance,

~P

Zeus
August 21st, 2012, 10:01
I believe the 2nd and 3rd parameters reference the class and database root node of the data you want to import. In the case of 3.5e and 4e rule sets try "npc" and "npc".

E.g.

function importNPC()
local sFile = Interface.dialogFileOpen();
if sFile then
DB.import(sFile, "npc", "npc");
ChatManager.SystemMessage("Imported NPC(s) from: " .. sFile);
end
end

Moon Wizard
August 21st, 2012, 19:54
Here is some more info on using DB.import.

Parameter 2 = target node in database under which data will be imported.
Parameter 3 = source child record path in import file (optional)

If there are only 2 parameters, then all the child nodes in the source XML file under the "root" tag will be imported as child nodes of the target node.

If there are 3 parameters, then each child node in the source XML file that matches the 3rd parameter path will create a unique child node under the target node, and all children of the source child node will be added under the newly created unique target node.

The 3 parameter version will most likely be used, since it can import unique sub-records for Story, NPCs, PCs, Items, Notes, etc. The 2 parameter version is provided in case someone wants to do a direct XML import (campaign import/export, calendar import/export, etc.)

Regards,
JPG

Phystus
August 22nd, 2012, 01:42
Hmmm, perhaps my problem is my xml file containing the NPC. What exactly is assumed about the input file? Right now mine only contains what would fall between the <id-xxxxx> </id-xxxxxx> tags (but not the tags, nor the rest of the structure of a db file).

That gives me something to play with at any rate. I'd already tried using "npc" as the first parameter without success, but it never occurred to me the parameters referred to the structure of the input file as well as the output. I'll test it out and report back.

Thanks!

~p

Moon Wizard
August 22nd, 2012, 03:17
Check out the format of a character export file, and compare to the DB.import call for the /importchar function. That should give you a better idea. Remember that a character export file can contain one or more "character" records.

So, your file you want to import for the NPC should have a "root" tag at the highest level, and wrap the NPC details in an "npc" tag.

Cheers,
JPG

Phystus
August 22nd, 2012, 03:19
So once I re-read Moon's advice a couple times I finally got it through my thick head. :rolleyes: The correct value for the 3rd argument depends on the structure of the input file, not the structure of db.xml.

Got it working! Thanks for all the help! :D

~p