+1 vote
by (310 points)
So awhile ago I asked about incorporating D&D mechanics like score modifiers into twine game. The response I got was to use one massive if statement for each stat and modifier to each pair of scores up to 20. However, it seems that this system is not communicating at all with the player object. Thoughts?

2 Answers

0 votes
by (8.6k points)

You probably mean this question, right?

While the answer's code is not what I would typically go for calculating the modifier (my preference would be something like Character.mod($hellfox.dex) to get the value), you will have to explain what you mean by "this system is not communicating at all with the player object" exactly. Ideally, with code you are writing and explaining what this code is supposed to be doing, but apparently isn't.

by (310 points)
I am basically running everything through a StoryInit passage so everything has a default setting essentially. I have a player object in there with default values than can be changed via a point buy passage. The stat point buy works fine and stats change no problem. However the player object ability scores are not setting, they remain at a 0. I will post everything below.

<<set $player to {
    name: "Hiro",
    MVIT: 0,
    VIT: 0,
    STR: 10,
    STRmod: 0,
    DEX: 10,
    DEXmod: 0,
    CON: 10,
    CONmod: 0,
    INT: 10,
    INTmod: 0,
    WIS: 10,
    WISmod: 0,
    CHA: 10,
    CHAmod: 0,
    } >>

<<if $player.STR lte 1>>
  <<set $player.STRmod to -5>>
  <<elseif $player.STR == 2 or $player.STR == 3>>
  <<set $player.STRmod to -4>>
  <<elseif $player.STR == 4 or $player.STR == 5>>
  <<set $player.STRmod to -3>>
  <<elseif $player.STR == 6 or $player.STR == 7>>
  <<set $player.STRmod to -2>>
  <<elseif $player.STR == 8 or $player.STR == 9>>
  <<set $player.STRmod to -1>>
  <<elseif $player.STR == 10 or $player.STR == 11>>
  <<set $player.STRmod to 0>>
  <<elseif $player.STR == 12 or $player.STR == 13>>
  <<set $player.STRmod to 1>>
  <<elseif $player.STR == 14 or $player.STR == 15>>
  <<set $player.STRmod to 2>>
  <<elseif $player.STR == 16 or $player.STR == 17>>
  <<set $player.STRmod to 3>>
  <<elseif $player.STR == 18 or $player.STR == 19>>
  <<set $player.STRmod to 4>>
  <<elseif $player.STR == 20>>
  <<set $player.STRmod to 5>>
<</if>>
by (8.6k points)

Basically, it looks like you forget to set the modifiers again after the values were changed.

This is one of the reasons why I prefer my approach - it calculates the modifiers dynamically without a need to re-run anything. All it takes is to add the method definition to the JavaScript portion of your game:

window.Character = {
	mod: function(val) {
		if(val >= 1 && val <= 20) {
			return Math.floor(val / 2) - 5;
		} else {
			return 0;
		}
	},
	// Add other methods and constants as needed
};

Now instead of accessing $player.CHAmod, you access Character.mod($player.CHA), instead of $player.STRmod you use Character.mod($player.STR) and so on.

by (310 points)
So would the Character.mod($player.CHA) stuff get added to the player object? And the Javascript to the Story Javascript window??? I am a bit new at this tbh.
by (8.6k points)
The player object loses all the "mod" attributes, the JavaScript code gets added to the Story JavaScript, and Character.mod($player.CHA) is what you use to get the current CHA mod - the value you previously accessed via $player.CHAmod.
0 votes
by (200 points)
Depending on the version of D&D you use, you should be able to modify based on raw scores on the fly.
 

3rd edition, for example, you could calculate the modifier with floor [(score-10)/2] (Not in twine code)

you would have 18 be a +4, a 15 as +2, 10 is a +0  and 6 would be a -2.  It also means you have near infinite scalability

I would expect it would be a lot easier than trying to have a table with all possibilities.

If later editions used different numbers, its just a matter of finding a mathematical conversion that works
...