PDA

View Full Version : getDice() Unexpected Results



Myrddin
April 4th, 2013, 11:49
I am trying to create a dice field dice_basedamage, which is calculated as the sum of two abilities (size & strength) divided by 6, plus any dice from another dice field (religion).

I want the dice_basedamage field to be dynamically linked to update whenever any of the three source fields are updated.

I have added script as follows to the template for the dice_basedamage field.


function onInit()
local sources = {}

sources[1]=window.getDatabaseNode().getChild("abilities.size.bonus");
sources[2]=window.getDatabaseNode().getChild("abilities.strength.bonus");
sources[3]=window.getDatabaseNode().getChild("religion.damage.dice");

sources[1].onUpdate = update
sources[2].onUpdate = update
sources[3].onUpdate = update

update()

end

function update()
local dice = {};
local rDice = {};

rDice = window.religiondamagedice.getDice();

for k,v in pairs(rDice) do
table.insert(dice, v);
end

xDice = math.floor(0.5 + (window.getDatabaseNode().getChild("abilities.size.bonus").getValue() + window.getDatabaseNode().getChild("abilities.strength.bonus").getValue())/6);
for v = 1, xDice do
table.insert(dice,"d6");
end

setDice(dice);

end

However, the link to the religiondamagedice field is not quite behaving as it should. When the first die is dropped into religiondamagedice, it does not update the dice_basedamage field. If a subsequent die is added to religiondamagedice, then dice_basedamage updates for the second die, but not the first and so on for any additional dice added. If, at any time, either of the two ability fields are subsequently updated then dice_basedamage updates correctly to include the dice from the religiondamagedice field.

Please help. I have been puzzling over this for hours and can not see where I am going wrong.

Zeus
April 4th, 2013, 19:53
Hmm, can't see whats wrong here, all looks ok to me.

Couple of comments:

Would it be simpler to write onInit as follows as sources is only used locally and onInit seems to only apply onUpdate handler to the three nodes.


function onInit()

window.getDatabaseNode().getChild("abilities.size.bonus").onUpdate = update;
window.getDatabaseNode().getChild("abilities.strength.bonus").onUpdate = update;
window.getDatabaseNode().getChild("religion.damage.dice").onUpdate = update;

update();
end

For update() try adding in Debug.chat() calls so that we can determine in update() is called after the initial dice are dropped. It could be that onUpdate isn't being called (maybe as the database has not been updated by the windowcontrol at that point in time - remember the asynchronous nature of database updates) or it could be that getDice() maybe returning nil. Adding Debug.chat as follows will confirm whats getting called and returned.



function update()
local dice = {};
local rDice = {};

Debug.chat("Update Fired!");

rDice = window.religiondamagedice.getDice();

for k,v in pairs(rDice) do
table.insert(dice, v);
Debug.chat(v);
end

xDice = math.floor(0.5 + (window.getDatabaseNode().getChild("abilities.size.bonus").getValue() + window.getDatabaseNode().getChild("abilities.strength.bonus").getValue())/6);
for v = 1, xDice do
table.insert(dice,"d6");
end

setDice(dice);
end


Add the Debug calls and re-run your test. If you get Update Fired message on the first drop, we know onUpdate is firing correctly. If not thats the cause. If you do see the message note the chat output, it should correspond with the dice type your dropping each time. Let us know what you get.

Myrddin
April 4th, 2013, 22:49
Thanks DrZeuss for your help. I have modified the onInit code as you suggest...much more elegant and streamlined.

I have added the debug lines to the update function and rerun my tests. The onUpdate does appear to be firing correctly as I do get the Update fired message on the first and any subsequent drops.

However, I do not get a report of the new die added after drops, only after updating the ability fields or dropping a subsequent die. This leads me to believe the getDice() is the problem.

For example:

Start with religiondamagedice empty.
Drop 1 d6 in religiondamagedice.

s'Update Fired!'

Drop 2nd d6 in religiondamagedice.

s'Update Fired!'
s'd6'

Drop 3rd d6 in religiondamagedice.

s'Update Fired!'
s'd6'
s'd6'

etc, etc

It seems the getDice() is running one die in arrears. Although, when I update either of the two ability fields the whole thing rights itself.

Any further pointers? All help gladly received.

Zeus
April 5th, 2013, 00:05
Have you tried using ipairs to iterate through rDice as opposed to pairs? getDice() returns an integer indexed table of dice.

Aside from that it sounds like it might be a bug. I would post a new thread in the house of healing for JPG to take a look at getDice().

In the interim and as a workaround you could also try using the die field/controls onValueChanged event method as opposed to the underlying database nodes onUpdate handler to manage updates from the diecontrol.

Myrddin
April 5th, 2013, 03:00
Yes, I tried using iPairs to start with, then switched to pairs in case that was the problem.

I have posted a new thread in the HoH as you suggest and I'll try building the onValueChanged event method as a work-around instead. Failing that, I'll just 'dumb down' the religiondamagedice to be a number entry field, which I know I can control with onUpdate.

Thanks for your help.