PDA

View Full Version : Getting values from data nodes.



GMBazUK
June 1st, 2011, 16:20
After carefully wrapping my non programmer brain cells around the Anatomy of a Rule set tutorials, I began work on a rule-set last week, but now find myself entering the murky waters (for me at least) of LUA.

Problem:
I have set up my characteristics xml exactly as the Anatomy of Rule set tutorial, with one exception, I have added a new column of numberfields which will generate a saving throw value.
This value (saving throw) is derived from the characterstics current value (that I can handle) plus the current Level of the character.
I have defined a numberfield for the character Level, unfortunately it does not reside within the Windowclass that contains the characteristic current value.

I know how to reference a data node inside the same windowclass, and I know how to reference a data node outside the windowclass, but I can't figure out how to reference both at the same time.
Having read through all the reference sources, and browsed the forum posts, Im afraid my programmer logic is still a work in progress.

Thanks in advance.

Moon Wizard
June 1st, 2011, 19:11
If you want a field that adds everything together as a single number, you are best off using the linked_number template from one of the existing rulesets (d20, d20_JPG, 3.5E, 4E)

Here is more detail in case the linked_number template won't work for you.
In general, the best way to do this is to use the databasenode and DB package functions to traverse the database.

* For each stringfield/numberfield, the getDatabaseNode function returns the databasenode object for that field. To get the parent window database node, you can call getDatabaseNode().getParent() or getDatabaseNode().getChild("..") or window.getDatabaseNode().

* For each stringcontrol/numbercontrol, they don't have an associated database field, so you can use window.getDatabaseNode to get the databasenode object for the parent window.

* The character sheet pages typically all have the same parent database node. Once you get the character database node, you can use getChild to access any fields within the character data.

If you are still having challenges, copy the portion of the code for the saving throw and level fields, and put it in this thread. We'll take a look.

Regards,
JPG

GMBazUK
June 1st, 2011, 19:48
Thank you for your prompt response Moon Wizard.

Here is my code.

With reference to the Anatomy of a rule-set:

First Base saving throw (statbasest). This resides after the numberfield "current" but also within the windowclass "charsheet_characteristic" which forms part of charsheet_listclass.xml


<numberfield name="statbasest">
<anchored>
<to>statcurrent</to>
<position>right</position>
<offset>20</offset>
<size>
<width>30</width>
</size>
</anchored>
<font>sheetnumber</font>
<frame>
<name>modifier</name>
<offset>5,4,5,4</offset>
</frame>
<script>
function refresh()
local save;
local lvlcurr = window.getDatabaseNode().getChild("attributes.lvl.current").getValue();
save = window.statcurrent.getValue() + lvlcurr;
setValue(save);
end

function onInit()
refresh();
window.statcurrent.getDatabaseNode().onUpdate = refresh;
window.getDatabaseNode().getChild("attributes.lvl.current").onUpdate = refresh;
end
</script>
<readonly/>
</numberfield>

Secondly the numberfield for level (lvlcurrent). This resides in the charsheet_main.xml, inside the sheetdata for windowclass "charsheet_main" but outside the windowlist "characteristics".


<numberfield name="lvlcurrent" source="attributes.lvl.current">
<anchored>
<to>level</to>
<position>righthigh</position>
<offset>10,-5</offset>
<size>
<width>35</width>
<height>25</height>
</size>
</anchored>
<font>sheetnumber</font>
<frame>
<name>modifier</name>
<offset>5,4,5,4</offset>
</frame>
<keyeditframe>
<name>sheetfocus</name>
<offset>5,4,5,4</offset>
</keyeditframe>
</numberfield>

I hope this is sufficient, and thank you once again.

Baz.

Moon Wizard
June 2nd, 2011, 00:48
I just took a quick look at Foen's article on the wiki, which you are referring to. I would expect that you should be getting script errors in the FG console when the code loads the character sheet.

Basically, you have to look at the database structure that is being generated by the controls you are using to understand how to use database paths to get where you want.

In this case, inside the db.xml file for a campaign with your ruleset, you would be something like this for your character data.



<root>
<charsheet>
<id-00001>
<attributes>
<lvl>
<current type="number">10</current>
</lvl>
</attributes>
<characteristics>
<STR>
<statbasest type="number">80</statbasest>
</STR>
</characteristics>
</id-00001>
</charsheet>
</root>


Let me go through what is happening using my example XML above:

When you call window.getDatabaseNode() in the statbasest control, you are getting a database node with the path "charsheet.id-00001.characteristics.STR"
When you call window.getDatabaseNode().getChild("attributes.lvl.current"), you will get an empty return value (nil); because the path "charsheet.id-00001.characteristics.STR.attributes.lvl.current" does not exist in the database.
When you call window.getDatabaseNode().getChild("attributes.lvl.current").getValue(), you are getting a script error because you are calling a function on an empty object.


To get where you want, you need to use relative path "go up" indicators in the getChild call. These are commonly used when dealing with XML in programming or in file systems. For example, getChild("..") is the same as getParent(); and getChild("...") is the same as getParent().getParent(); and so on.

Your call should look something like:
window.getDatabaseNode().getChild("...attributes.lvl.current").getValue();
and
window.getDatabaseNode().getChild("...attributes.lvl.current").onUpdate = refresh;

Regards,
JPG

GMBazUK
June 2nd, 2011, 07:04
Thank you so much Moon Wizard. Believe it or not I understood that you have to navigate away from your current position in the database rather than assuming you are starting from the upper most level, and working down, I just didnt understand the use of the little "..."
I will go and read the post again and pay more attention.
Thank you.
Baz.

Moon Wizard
June 2nd, 2011, 20:30
No worries. I just figured it would be best to provide more information, than less information, to make sure that you got what you needed.

Cheers,
JPG

GMBazUK
June 2nd, 2011, 20:49
Spot on. At this stage, the logic is the hardest part, so anything that can reinforce my general impression is most appreciated.

I have another question, but I will post it as another thread, as it may help others to find it more easily.

Baz.