So far my progress with making an rpg has gone steadily. The Battle System is functioning well, but there is a strange bug that is proving to be rather confusing. I know what is causing it but am unsure how to fix it. Below will be all of the code that is causing it. The problem is located in the <<if>> statement, <<if random(1, 6) + $player.STR gte $enemy.AC>>. The bug is causing the click links to essentially negate themselves. I could click them and there is a chance that it won't set the turn variable to enemy. This means players and enemies are able to move twice essentially, which was not intended. The problem more than likely stems from random(1, 6). I noticed that when I changed the + in that statement to "and" the bug disappeared but only one of the outcomes was triggering. That outcome being the player missing their attack.
<<if $turn is "player">>
<<click "Attack" "Combat">>
<<if random(1, 6) + $player.STR gte $enemy.AC>>
<<print random(1, 6), $player.STR, $enemy.AC>>
<<set $enemy.H = $enemy.H - $player.STR>>
<<set $log to "pAttack">>
<<set $turn to "enemy">>
<</if>>
<<if random(1, 6) + $player.STR lt $enemy.AC>>
<<print random(1, 6), $player.STR, $enemy.AC>>
<<set $log to "pMiss">>
<<set $turn to "enemy">>
<</if>>
<</click>>
<</if>>
<<if $turn is "player">>
<<click "Run" "Battle Lose">><</click>>
<</if>>
<<if $turn is "enemy">>
<<click "Continue" "Combat">>
<<if random(1, 6) + $enemy.STR gte $player.AC>>
<<set $player.H = $player.H - $enemy.STR>>
<<set $log to "eAttack">>
<<set $turn to "player">>
<</if>>
<<if random(1, 6) + $enemy.STR lt $player.AC>>
<<set $log to "eMiss">>
<<set $turn to "player">>
<</if>>
<</click>>
Comments
Based on the syntax of your example I am going to assume it is SugarCube 1.x
note: your example is missing an <</if>> at the end of it.
Each time you call random(1, 6) it can return a different result this means the value used in the following four lines within the first <<click>> macro of your example could all be using different values: What you need to do is store the value returned from random(1, 6) within in a variable, and then reference that value within each check/output like so:
Using the above method and replacing the second <<if>> macros within each of the <<click>> with an <<else>> your example would look something like the following:
Try something like the following: