PDA

View Full Version : Database and listitems



rule of three
June 24th, 2009, 21:40
Hi. I've some problems with database associated with listitems.
I will take the example of the weaponlist :

---------------------------------------------------------------------
<charsheet>
<id-00001> (player's character 1)
...

<weaponlist>

<holder name="RV" owner="true" />
<id-00001>

<holder name="RV" owner="true" />
<ammo type="number">0</ammo>
...
<damagedice type="dice"></damagedice>
<name type="string">arme A</name>
...
</id-00001>
<id-00002>
...
</id-00002></weaponlist>
</id-00001>
------------------------------------------------------------------

I can access to the datanode of the player (id-01 for example) with :
local node = window.getDatabaseNode();
print (node.getNodeName()) gives "charsheet.id-0001"

I get the good number of child when I use :
local weaponnumber = node.getChild("weaponlist").getChildCount();
print (weaponnumber()) gives "2"

And I seem to get the good table with :
local weaponstable = node.getChild("weaponlist").getChildren();

My problem is that I don't find how to access to the name or damagedice of the differents weapons !

table[1].name.getValue() doesn't work for example. It would be to easy.
for k, v in ipairs (table) doesn't work neither : the windowlist isn't open.

Can somebody help me ?

Tenian
June 24th, 2009, 22:04
A snippet from one of the 4e_JPG lua files:


for k,v in pairs(node_list.getChildren()) do
if NodeManager.getSafeChildValue(v, "active", 0) == 1 then
return v;
end
end


the NodeManager.getSafeChildValue is a function that gets the value or returns a default if it's not found:



function getSafeChildValue(sourcenode, fieldname, defaultval)
if sourcenode then
local srcvalnode = sourcenode.getChild(fieldname);
if srcvalnode then
return srcvalnode.getValue();
end
end

return defaultval;
end

rule of three
June 25th, 2009, 08:13
Thanks Tenian. I hadn't understood than "k" is not a number but the id-xxxxx field. Now it works.

function found_value (list_node, x_id, field_label)

if (list_node) and (field_label) then

local items = list_node.getChildren();
for k,v in pairs(items) do

if k == x_id then
return (v.getChild(field_label).getValue());
end
end
end
return(false);
end

This function will return the value of the field named field_label of item number x in the database.
Example : found_value (window.getDatabaseNode().getChild("weaponlist"), "id-00001", "name") returns "Arme A".
Note that data in the database may be in a different order than in the character's sheet ...

Now, I have to found a script remaking the alphabetical order ...