View Full Version : Ruleset modification question.
Bill Thomas
July 28th, 2007, 16:58
I'm attempting to learn the ruleset creation by screwing around with the .xml & LUA.
A simple change I'd like to make the D20 ruleset is to change the ability score modifiers but I can seem to find out where to change the value of the modifiers based on the stat value.
Can some kind soul point me in the right direction?
Thank you
Bill Thomas
July 28th, 2007, 19:30
Ok... I stumbled upon it in the charsheet_templates.xml.
I've changed the constant (e.g. from /2 to /3) to see how it works.
It would appear that if you want a different scaling to the modifers other than by a factor of 2, the coding would become complicated (e.g. currently the ability score of 12~13 = +1 mod, 14~15 = +2 etc..)
What I would like to see is an example of code that would allow the following ability score modifers:
ability 1~3 = -3
ability 4~5 = -2
ability 6~8 = -1
ability 9~12 = +0
ability 13~15 = +1
ability 16~17 = +2
ability 18 = +3
Guidance from anyone would be appreciated.
Dachannien
July 28th, 2007, 21:18
One way would be to define a table that has the "answer" values in it, and use the ability score as the key (or index) into that table to look up the answer.
function abilitytomodifier(ability)
local abilitymodtable = { [18] = 3, [16] = 2, [13] = 1, [9] = 0, [6] = -1, [4] = -2, [1] = -3};
for k,v in pairs(abilitymodtable) do
if ability >= k then
return v;
end
end
end
Thus, changing the boundaries becomes a matter of changing the abilitymodtable. You put them in descending order, using as the keys the smallest integer that corresponds to a particular value.
Oberoten
July 28th, 2007, 21:46
... aight. :)
This is EXACTLY what I need.
I'd kiss ya if I knew wether or not ya wanted it. :)
Bill Thomas
July 28th, 2007, 21:58
Absolutly brilliant Dachannien ....
However my tiny mind is clueless as to where to paste your code example into the charsheet_templates.xml
Could you please pick-up a baseball bat and pound it into my skull? :confused:
Bill Thomas
July 28th, 2007, 23:22
Here is the code I want to modify with your example. I've tried a few different ways with no success.
<template name="abilitybonus">
<modifiernumber>
<anchored>
<position>right</position>
<offset>9,0</offset>
<size>
<width>36</width>
</size>
</anchored>
<frame>
<name>bonus</name>
<offset>3,5,3,5</offset>
</frame>
<font>sheetnumber</font>
<hideonvalue value="0" />
<displaysign />
<script>
function onSourceUpdate()
setValue(math.floor((sources[scorefield[1]].getValue() - sources[damagefield[1]].getValue() - 10) / 2) + getModifier());
end
function onInit()
addSource(scorefield[1]);
addSource(damagefield[1]);
super.onInit();
end
</script>
</modifiernumber>
</template>
Dachannien
July 29th, 2007, 02:51
You would add the abilitytomodifier() function in the script block, and then replace everything in the setValue() call in onSourceUpdate() with a call to the abilitytomodifier() function, plus the result from getModifier(). The value you pass to that function should be the total of the things that go in to make the ability total.
setValue( abilitytomodifier(sources[scorefield[1]].getValue()
- sources[damagefield[1]].getValue())
+ getModifier() );
Bill Thomas
July 29th, 2007, 13:30
I'm still doing something wrong. The only two modifiers I'm getting is a "-3" for an ability score between 1~15 and "+2" for 16 and above.
<script>
function abilitytomodifier(ability)
local abilitymodtable = { [18] = 3, [16] = 2, [13] = 1, [9] = 0, [6] = -1, [4] = -2, [1] = -3};
for k,v in pairs(abilitymodtable) do
if ability >= k then
return v;
end
end
end
function onSourceUpdate()
setValue( abilitytomodifier(sources[scorefield[1]].getValue()
- sources[damagefield[1]].getValue())
+ getModifier() );
end
function onInit()
addSource(scorefield[1]);
addSource(damagefield[1]);
super.onInit();
end
</script>
Dachannien
July 29th, 2007, 21:18
Oh, crap, I just realized something: when using numeric keys, Lua will stick the keys into their numeric order, which makes the list incorrect.
Try replacing the left and right brackets in the number list with quotes, and a couple lines down where it says "if ability >= k then", replace the "k" with "tonumber(k)".
I'm getting kind of sloppy, I know ;)
Bill Thomas
July 29th, 2007, 21:43
Thanks Dachannien, I'll give it a try very shorty :)
Bill Thomas
July 29th, 2007, 22:01
No luck...
Did I mis-understand your instructions?
<script>
function abilitytomodifier(ability)
local abilitymodtable = { "18" = 3, "16" = 2, "13" = 1, "9" = 0, "6" = -1, "4" = -2, "1" = -3};
for k,v in pairs(abilitymodtable) do
if ability >= tonumber(k) then
return v;
end
end
end
function onSourceUpdate()
setValue( abilitytomodifier(sources[scorefield[1]].getValue()
- sources[damagefield[1]].getValue())
+ getModifier() );
end
function onInit()
addSource(scorefield[1]);
addSource(damagefield[1]);
super.onInit();
end
</script>
Dachannien
July 29th, 2007, 22:59
Yeah, you got what I was putting down, but I made two mistakes. One is that if you're using strings as keys, you have to put them in square brackets and in quotes as well, like
["18"] = 3
The other is that, apparently, Lua doesn't guarantee that the elements of a table will be put into that table in the order you specify, unless you are using integer keys and using ipairs instead of pairs, in which case they end up in numeric order by key. Neither of those is what we want. Personally, I consider this to be fairly obtuse, but I come from a C/C++ background, where you don't have to contend with this sort of issue.
Instead of trying to do this the clever way, then, you can do it the bulky and annoying way:
function abilitytomodifier(ability)
local modifier = 0;
if ability >= 18 then modifier = 3;
elseif ability >= 16 then modifier = 2;
elseif ability >= 13 then modifier = 1;
elseif ability >= 9 then modifier = 0;
elseif ability >= 6 then modifier = -1;
elseif ability >= 4 then modifier = -2;
elseif ability >= 1 then modifier = -3;
end
return modifier;
end
Anyway, I broke down and finally installed a Lua interpreter on a Linux box I use, so I actually tested this code snippet. It should work this time! ;)
Bill Thomas
July 30th, 2007, 23:08
Success!!
Dachannien... many thanks for your helping hands (and brain) :)
Dachannien
July 31st, 2007, 00:22
No problem. Glad it works :)
Powered by vBulletin® Version 4.2.1 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.