Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Strange Bug in my Battle System

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

  • You need to state which Story Format you are using each time you ask a question, as answers can be different for each one.
    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:
    <<if random(1, 6) + $player.STR gte $enemy.AC>>
    <<print random(1, 6), $player.STR, $enemy.AC>>
    ......
    <<if random(1, 6) + $player.STR lt $enemy.AC>>
    <<print random(1, 6), $player.STR, $enemy.AC>>
    
    could equal something like the following:
    
    <<if 1 + $player.STR gte $enemy.AC>>
    <<print 4, $player.STR, $enemy.AC>>
    ......
    <<if 6 + $player.STR lt $enemy.AC>>
    <<print 2, $player.STR, $enemy.AC>>
    
    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:
    <<set $random to random(1, 6)>>
    
    <<if $random + $player.STR gte $enemy.AC>>
    <<print $random, $player.STR, $enemy.AC>>
    ......
    <<if $random + $player.STR lt $enemy.AC>>
    <<print $random, $player.STR, $enemy.AC>>
    

    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:
    <<if $turn is "player">>
    	<<click "Attack" "Combat">>
    		<<set $random to random(1, 6)>>
    
    		<<if $random + $player.STR gte $enemy.AC>>
    			<<print $random, $player.STR, $enemy.AC>>
    			<<set $enemy.H to $enemy.H - $player.STR>>
    			<<set $log to "pAttack">>
    			<<set $turn to "enemy">>
    		<<else>>
    			<<print $random, $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">>
    		<<set $random to random(1, 6)>>
    		
    		<<if $random + $enemy.STR gte $player.AC>>
    			<<set $player.H to $player.H - $enemy.STR>>
    			<<set $log to "eAttack">>
    			<<set $turn to "player">>
    		<<else>>
    			<<set $log to "eMiss">>
    			<<set $turn to "player">>
    		<</if>>
    	<</click>>
    <</if>>
    
  • Your primary problem is that you're calculating the attack chance more than once during each combatant's turn, which will yield odd results. If you need to use the value more than once, calculate it just once and store the result in a variable which you can reuse. In this case, however, you don't need to do that if you use an <<else>> macro. Speaking of which, a secondary problem is that you're using too many <<if>> macros and not enough <<else>> macros—you're overcomplicating your logic.

    Try something like the following:
    <<if $turn is "player">>
    	<<click "Attack" "Combat">>
    		<<if random(1, 6) + $player.STR gte $enemy.AC>>
    			<<set $enemy.H -= $player.STR>>
    			<<set $log to "pAttack">>
    			<<set $turn to "enemy">>
    		<<else>>
    			<<set $log to "pMiss">>
    			<<set $turn to "enemy">>
    		<</if>>
    	<</click>>
    
    	<<click "Run" "Battle Lose">><</click>>
    <<else>>
    	<<click "Continue" "Combat">>
    		<<if random(1, 6) + $enemy.STR gte $player.AC>>
    			<<set $player.H -= $enemy.STR>>
    			<<set $log to "eAttack">>
    			<<set $turn to "player">>
    		<<else>>
    			<<set $log to "eMiss">>
    			<<set $turn to "player">>
    		<</if>>
    	<</click>>
    <</if>>
    
Sign In or Register to comment.