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