Twine Q&A - Recent questions tagged math
http://twinery.org/questions/tag/math
Powered by Question2AnswerNeed Some Math Help
http://twinery.org/questions/52873/need-some-math-help
<p>So in my game I have a lot of values that are on a sliding scale from 0-100, with different breakpoints on these scales that denote different tiers in that attribute.</p><p>For example...</p><pre><code><<set $dex = 22>>
<<set $dexTier = 0>>
<<set $dexDesc = "">>
<<if $dex > 99>>
<<set $dexTier to 5>>
<<set $dexTier to "Lightspeed">>
<<elseif $dex > 75>>
<<set $dexTier to 4>>
<<set $dexTier to "Super Sonic">>
<<elseif $dex > 50>>
<<set $dexTier to 3>>
<<set $dexTier to "Pretty Fast">>
<<elseif $dex > 25>>
<<set $dexTier to 2>>
<<set $dexTier to "Kinda Slow">>
<<elseif $dex > 1>>
<<set $dexTier to 1>>
<<set $dexTier to "Sloth">>
<<elseif $dex <= 0>>
<<set $dexTier to 0>>
<<set $dexTier to "Standing Still">>
<</if>></code></pre><p>I can't help but think there HAS to be a better way to do this! Prefereably, I'd have the descriptions in an array with a way to parse the marker from math, but my math skills are... questioneable.</p><p>Any insight here?</p>http://twinery.org/questions/52873/need-some-math-helpSat, 11 May 2019 00:49:00 +0000Math percentage
http://twinery.org/questions/48621/math-percentage
A question about Math and percentage in sugarcube, I would like to know how to increase or decrease values in percentages.<br />
<br />
E.g. | Imagine that the protagonist gains an "elite warrior" skill from this skill each monster that he defeats give him more 10% the value of the experience. These values are extremely variable, from 100 to 300.<br />
<br />
This also applies to lower values, such as 5% reduction of damage when using a shield.http://twinery.org/questions/48621/math-percentageMon, 01 Apr 2019 01:14:01 +0000Simple math error in Sugarcube 2 (Twine 1.4.2)
http://twinery.org/questions/17523/simple-math-error-in-sugarcube-2-twine-1-4-2
<p>I am using Sugarcube 2 in Twine 1.4.2.</p><p>I am running a sequence where the player can buy something with the money they have. As a test, I set the starting money to $50 and the cost of the item as $36.42. I want the screen to display the money, the cost of the item, and the money the player has left after subtracting the cost from the money.</p><pre><code><<set $money = 50.00>>\
<<set $totalprice = 36.42>>\
Total cost: $<<print $totalprice>>
Money you have: $<<print $money>>
Money left to spend: $<<print ($money - $totalprice)>></code></pre><p>However, that returns the following:</p><pre><code>Total cost: $36.42
Money you have: $50
Money left to spend: $13.579999999999998</code></pre><p>I tried changing the numbers, but I can't figure out why the "money left to spend" displays as such a long number and not $13.58. Even more confusing to me is why it only happens to certain numbers and not others:</p><pre><code>$<<print (50.00 - 36.49)>>
$<<print (50.00 - 36.48)>>
$<<print (50.00 - 36.47)>>
$<<print (50.00 - 36.46)>>
$<<print (50.00 - 36.45)>>
$<<print (50.00 - 36.44)>>
$<<print (50.00 - 36.43)>>
$<<print (50.00 - 36.42)>>
$<<print (50.00 - 36.41)>>
$<<print (50.00 - 36.40)>></code></pre><pre><code>$13.509999999999998
$13.520000000000003
$13.530000000000001
$13.54
$13.549999999999997
$13.560000000000002
$13.57
$13.579999999999998
$13.590000000000003
$13.600000000000001</code></pre><p>This seems like an elementary problem but I can't seem to find anything online about it. Any help will be appreciated, thank you.</p>http://twinery.org/questions/17523/simple-math-error-in-sugarcube-2-twine-1-4-2Fri, 15 Jun 2018 00:01:56 +0000Creating macros/functions for non-linear stats changes (Twine 2, Sugarcube 2.18) using JavaScript
http://twinery.org/questions/1531/creating-macros-functions-linear-changes-sugarcube-javascript
<p>I'm trying to change stats in a non-linear way, which would make it easy to surpass mediocrity, but difficult to master something. I'm hoping this will lead to a more dynamic user experience (note: this will have the opposite effect and make users 'average' if there isn't an internal game mechanic which encourages min/max playing).<br><br>This is how I envision it working - a percentage change (bound between 0 and 1) will increase the percent left to attain, or decrease the percent already attained.<br><br>For example, if a stat $honor is currently 80 and we want to modify it by +30% for a heroic act, it would only increase 6 percentage points to 86/100 ((100-80)*0.3)+80=86. (an honorable person doing a heroic act? kind of expected).<br>Whereas if the $honor stat was 20, and the user decided to perform an unexpected act of heroism with a +30% modifier, the stat would increase 24 percentage points to 44/100 ((100-20)*0.3)+20=44. (a coward performing a heroic act? How noble of you!)<br><br>A decreasing modifier will do the opposite. In the above case, an 80 stat modified with -30% will decrease 24 percentage points to 56/100, and be calculated as such: 80-(80*0.3)=56. (a heroic person act as a coward? that's a big tarnish on your record!)<br>Whereas a stat of 20 with a -30% modifier would only decrease 6 percentage points to 14/100, and be calculated: 20-(20*0.3)=14. (a coward taking the easy way out? That's as expected.)<br><br>As long as your starting value is between 1 and 100 (inclusive), this system will bound your results between 1 and 100 (inclusive), thus no need for clamping a stats value.<br><br>The only catch is, I have no idea how to write this in JavaScript.<br>This is the point I've been able to logic/google myself to...<br><br><span style="text-decoration: line-through;">set $tempvariable = $statvariable. [Call it a]<br>set b to percent modifier<br>try {<br> macros.add("percentincrease", {<br> handler: function(a, b) {<br> return (((100 - a) * b ) + a);<br> }<br> }<br>} catch(e) {<br> return this.error("percentincrease Error: " + e.message);<br>}<br><br>set $statvariable = a<br><br>try {<br> macros.add("percentdecrease", {<br> handler: function(a, b) {<br> return (a - (a * b));<br> }<br> }<br>} catch(e) {<br> return this.error("percentincrease Error: " + e.message);<br>}<br><br>set $statvariable = a</span></p>http://twinery.org/questions/1531/creating-macros-functions-linear-changes-sugarcube-javascriptSun, 10 Sep 2017 17:13:18 +0000Sqrt: and Pow: not working in Harlowe
http://twinery.org/questions/1530/sqrt-and-pow-not-working-in-harlowe
<p>I can't get the sqrt or pow macros to work.</p><p>For example</p><pre><code>(set: $num to (sqrt: 25) )</code></pre><p>Just stores a 0 in $num. No matter what I give it as input, sqrt seems to evaluate to zero.</p><p>Likewise, even though the documentation says pow should be used like (pow: 5, 2), it complains that pow only takes 1 argument. Giving it 1 argument evaluates to zero. </p><p>I've tried testing in a few different browsers and with the online and downloadable version of Twine. </p><p>I'm not sure what I am doing wrong with these functions. Any help is appreciated.</p>http://twinery.org/questions/1530/sqrt-and-pow-not-working-in-harloweSun, 10 Sep 2017 16:10:15 +0000Is it possible to do math with the textinput macro
http://twinery.org/questions/1248/is-it-possible-to-do-math-with-the-textinput-macro
Is it possible to do math with the textinput macro? Because as far as I can tell, by default its datatype is always a string. So am I missing something (like another input macro or some kind of trick) or is it possible to change what type of input textinput accepts / uses?<br />
<br />
<br />
<br />
Basically what I want is to be able to use the basic mathematical operators (primarily multiplication and addition) with the input users provide, and I'm having trouble with getting that to work. If someone could help, that would be great. Just tell me what you need in order for you to help me.http://twinery.org/questions/1248/is-it-possible-to-do-math-with-the-textinput-macroThu, 24 Aug 2017 06:16:02 +0000