PDA

View Full Version : More scripting questions.....



Doswelk
June 14th, 2007, 14:09
I am still trying to get my head round how FGII works....

I am trying to calculate the fortitude defence value, below are the numbercontrols I am using:



<numbercontrol name="fortdefense" source="defense.fortitude.total">

<numbercontrol name="fortlvlarmor">

<numbercontrol name="fortclassbonus" source="defense.fortitude.classbonus">

<linkednumber name="fortstatbonus" source="abilities.constitution.bonus">

<numbercontrol name="fortmisc" source="defense.fortitude.misc">


Basically I need to set fortdefense to equal
10 + fortlvlarmor + fortclassbonus + fortstatbonus + fortmisc

I had a <script> entry similar to this:



function onInit()
addSourceWithOp("fortlvlarmor", "+");
addSourceWithOp("fortclassbonus", "+");
addSourceWithOp("fortstatbonus", "+");
addSourceWithOp("fortmisc", "+");
end


And it worked but I couldn't work out how to add 10 to that, also if one of the number changed it did not update.

I suspect that fortdefense needs to be a linked number and I need to somehow have a SourceUpdate event as well.

Any help would be appreciated....

On another note what would be nice (IMHO) would be a scripting forum to allow people to post useful scripts.

Dachannien
June 14th, 2007, 15:43
A numbercontrol is just a box on some sheet with a number in it. You can use scripting to set or calculate its value, but it's not linked to the database at all. You need to use a numberfield (or a template that derives from that type, like a linkednumber or modifiernumber, if you're using the d20 ruleset templates) in order to see your changes reflected in the database.

If you are using the linkednumber template, you can specify a custom onSourceUpdate function that calculates the field's value in a special way instead of using the source operations. You can include it inline with the XML like this:



<linkednumber name="fortdefense" source="defense.fortitude.total">
<source><name>defense.fortitude.classbonus</name></source>
<!-- include one source line for each field in the database
used as a source for this field -->

<script>
function onSourceUpdate()
setValue(10 + sources["defense.fortitude.classbonus"].getValue());
end -- include a term in this calculation for each field to be added in
</script>
</linkednumber>


Obviously, this snippet doesn't include everything you need to make it work, but it does show the relevant parts.

By the way, you can use a linkednumber in this way to do arbitrarily complex calculations. The fields you use as sources don't even have to be numbers, as long as you don't specify an operation for them and you define your own onSourceUpdate() function.

You also don't have to use a linkednumber - you can instead just use a numberfield (or a numbercontrol, if you don't want the value saved in the database), but you then have to get handles to all of the objects you're using as sources, and then register a callback function that gets called when that object's onUpdate event is triggered:



local whateverhandle = nil;

function onSourceUpdate()
-- Do something
setValue(whateverhandle.getValue() * 2);
end

function onInit()
whateverhandle = window.getDatabaseNode().createChild("whatever", "number");
if whateverhandle then whateverhandle.onUpdate = onSourceUpdate; end
end

Oberoten
June 14th, 2007, 20:45
On another note what would be nice (IMHO) would be a scripting forum to allow people to post useful scripts.



VERY seconded.

joshuha
June 14th, 2007, 21:09
Thirded! A sub-forum of the Workshop area for scripting would be great.