UNity3D/C#: Battle Conditions

In playing an RPG you’ll find that your characters can get certain battle conditions from certain abilities, but how would you actually code this? I’ve got my own system worked out for it. In Abeyance you’ll be able to enfeeble enemies or boost your own stats, so what you need is certain conditions. You could try just using a command that says, “okay, when I use that, I’ll need to boost the character’s Agility by 15”. The problem is that the agility will remain 15 points higher and you could just spam that move and end up with an infinite number, essentially breaking the game.

So in this tut I am taking this move from Abeyance:


And you’ll see Keats Agility go from:


To this:


And not stack further.

All of my battle mechanics are coded in a single script/class, so all I need to use is a couple of private variables to start with. It’s very simple:

private bool agiboost = false;

private float oldagi;

The boolean will be used to determine whether the effect is turned on or off. oldagi will store the character’s base Agility.

For your ‘OnStart()’ method, you’ll need to record the default Agility.


void OnStart()
        oldagi = Stats.AGI;

‘Stats’ is the script attached to my player object, which contains the player’s stats.

Then on your ‘endbattle’ conditions (basically anything you do once the battle is finished), you’ll need to reset everything. Or if you’ve got a certain number of turns you may wish the effect to wear off. For this it’ll be at the end of the battle as it’s how I’ve set up Abeyance’s dynamics.

void BattleEnd()
        agiboost = false;
        Stats.AGI = oldagi;

Next, you’ll need the actual battle conditions.

if (agiboost == false)
                                        Stats.AGI += 25;
                                        agiboost = true;

In my actual system I have other conditions, but as normal I’ve just cut out the important bits. Basically, it’ll check if the ‘agiboost’ effect is active and if it isn’t, it’ll apply it, by increasing the Agility by 25 and telling the program agiboost is now active. This will stop it stacking. If you wanted you could make a condition where the player can’t use the Agility boost skill (Shadowblend) if the player has an Agility boost, I haven’t because I think they should be free to make mistakes.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s