We'll also be changing some parts of the attack functions to include the timer behavior, but I'll cover that with the changes to accommodate the accuracy check. But before we get to that, we need to add a new function. The accuracy check requires a random number to work, so we'll use a function and a global variable for this. Make sure to add a Global Number called RandomNumber to your event sheet.
Next, we'll use a function to set the variable to a random number whenever we need it:
Condition
On function RandomNumberGen
Action
System ▶︎ Set RandomNumber to int(random(0,101))
Now, whenever we call this function, the RandomNumber variable will be reset.
Like in the last project, the two attack functions are pretty similar, so we'll go over one of them and use a screenshot to illustrate the second. Starting with the OppAtt function:
OppAtt
First, we check if the opponent has any HP – if their HP value is 0, the function will not run.
Condition
On function OppAtt
Action
HitStatus ▶︎ Set text to ""
Function ▶︎ Call RandomNumberGen
Sub-event Condition
Opponent ▶︎ HP > 0
Sub-event Condition
System ▶︎ Is not PlayerMoved
Sub-event Action
System ▶︎ Set NextAction to "PlayerMove"
Sub-event Condition
System ▶︎ Compare two values ▶︎ RandomNumber ≤ Opponent.Acc
Sub-event Action
System ▶︎ Set CurTurn to "Opp"
System ▶︎ Set OppMoved to True
HitStatus ▶︎ Set text to "Attack hit!"
Player ▶︎ Set HP to Self.HP-Opponent.Attack
Player ▶︎ Flash ▶︎ Flash on 0.1s off 0.1s for 1 second
HitStatus ▶︎ Timer ▶︎ Start Timer "NextMove" for 1.0 second
Sub-event Condition
System ▶︎ Compare two values ▶︎ RandomNumber > Opponent.Acc
Sub-event Action
System ▶︎ Set CurTurn to "Opp"
System ▶︎ Set OppMoved to True
HitStatus ▶︎ Set text to "Attack missed!"
HitStatus ▶︎ Timer ▶︎ Start Timer "NextMove" for 1.0 second
As before, the PlayerAtt function is essentially the same but for the player object:
So, with these small changes, we have a slightly more robust battle system that includes the ability to have combatants with different accuracy stats. This introduces a little more randomness into the system to make it a bit more interesting!
The next chapters of the course will look at adding in Damage Calculations to change things up a bit from just using the attack stat as the damage value.