PDA

View Full Version : Update a linked field from another one



sbocquet
March 30th, 2008, 17:16
Hi all,

Here is a little problem I have... that need a little help !

I'd like to update the load fields of my charsheet when the strength ability is updated.
I've wrote this little piece of code in the charsheet_inventorylist.lua file.

function calculateLoads(strength)
local data = loaddata[strength];

encumbrance.lightload.setValue(data.light);
encumbrance.mediumload.setValue(data.medium);
encumbrance.heavyload.setValue(data.heavy);
encumbrance.liftoffground.setValue(data.heavy);
encumbrance.liftoverhead.setValue(data.heavy);
encumbrance.pushordrag.setValue(data.heavy);
end

-- Carrying capacity
loaddata = {
[1] = {
light = 3, medium = 6, heavy = 10
},
[2] = {
light = 6, medium = 13, heavy = 20
}
}

The problem is : how do I call my function in an Update event of the strength field and is it the best way to do it ?

Surely it's an easy one, but can't get it work !!! :confused:

joshuha
March 30th, 2008, 18:06
This thread may help:

https://fantasygrounds.com/forums/showthread.php?t=6775

sbocquet
March 30th, 2008, 19:26
I saw this thread... write this... but I have questions :

<script>
function onInit()
getDatabaseNode().onUpdate = calculateLoads;
end

function calculateLoads(source)
local data = loadsdata[source.getValue()];
window.getDatabaseNode().getChild("encumbrance.lightload").setValue(data.light);
end
</script>

1 - I use a "table" of values in another file. Is this going to work correctly or is there anything else I have to do ? How can I reach it ?
2 - The strength field already have an onInit() function in the template "abilityscore", witch is a genral one for all the ability scores. Is my onInit() function will add to the existing one ? Don't think so...

In fact, I have an error message in the console when running it : "attemp to index global 'loadsdata' (a nil value)"

Sorry for all the questions... I'm just beginning with FG2 and lua !
Thanks for your help...

Foen
March 30th, 2008, 22:21
How close is the ruleset to d20? Does it share the same basic layout (main vs inventory tab) and same rough database structure?

Cheers

Stuart

sbocquet
March 31st, 2008, 07:43
Hi,

My ruleset is very very close to the original d20 (same database, same tabs). In fact, I've just translate it in French, and deleted the spells tab and code, because my adventures will take place in the modern world without magic or FX abilities (based on the Jeremiah story).

In fact, I think that I'm close to it, but can't reach my reference table loadsdata in my lua file to catch the datas.

If some are interested, I will post it later...

Here is my code :

<numberfield name="encumbrancelightload" source="encumbrance.lightload">
...
...
...
<script>
function onInit()
source = window.getDatabaseNode().getChild("abilities.strength.score");
source.onUpdate = update;
update();
end

function update()
local data = loadsdata[source.getValue()];
setValue(data.light);
end
</script>
</numberfield>

sbocquet
March 31st, 2008, 09:37
Ok, got it work...

If someone is interested, I can post the solution here...

Thanks for your help,
Stéphane

Oberoten
March 31st, 2008, 15:24
Solutions are always of interest. :)

sbocquet
March 31st, 2008, 16:05
So, here is the solution...

charshett_encumbrance.lua


function onInit()
source = window.getDatabaseNode().getChild("abilities.strength.score");
source.onUpdate = update;
update();
end

function update()
local data = loadsdata[source.getValue()];
window.encumbrancelightload.setValue(data.light);
window.encumbrancemediumload.setValue(data.medium) ;
window.encumbranceheavyload.setValue(data.heavy);
window.encumbranceliftoverhead.setValue(data.heavy );
window.encumbranceliftoffground.setValue(data.heav y*2);
window.encumbrancepushordrag.setValue(data.heavy*5 );
end

-- Carrying capacity
loadsdata = {
[1] = {
light = 3, medium = 6, heavy = 10
},
[2] = {
light = 6, medium = 13, heavy = 20
}
and so on...
}


and I've added that line in the charsheet_inventory.xml


<numberfield name="encumbrancelightload" source="encumbrance.lightload">
...
<script file="scripts/charsheet_inventoryencumbrance.lua"/>
<readonly/>
...
</numberfield>


and the <readonly/> tag on all the fields that now don't need to be updated (to be perfect, you also need to rearrange the tab order between the fields in order that the loads fields cannot be selected by the keyboard too ;) ).

Simple... once you got it works ! lol

Bidmaron
December 23rd, 2008, 12:53
Question for sbocquet:
Shouldn't the encumbrance also be affected by strength damage? I think you'd have to make a computed field, like effected strength that never gets displayed and base encumbrance on an update of that field (which, in turn, gets updated on a change to strength or strength damage).