How Can We Help?

Mana

By using the include Basic Attributes component, you can support a Mana pool fairly easily. However, there’s two side to this:

  1. Using mana to cast spells
  2. Displaying mana in some kind of UI bar

Basic Attributes

To start, we’ll use the Basic Attributes component to hold our actual Mana value.

That’s just a matter of adding the Basic Attributes component to your character and adding a “Mana” entry of type “Integer”.

Now we can use this as the basis for getting and setting the mana value.

Updating Mana

Now that we have a place to store the mana, we can use it a couple of ways; we could use it to prevent the spell casting motion from even starting the animation or we can change how the spell itself works. 

In this example, we’ll let the spell animation happen. However, we won’t let the spell actually do anything. To do this, we need to modify the spell to test if there’s enough mana to cast the spell. None of the current spells do this. So, you have to copy and modify each spell individually.

In this example, we’ll create a copy of the “Magic Missile” spell and modify it to use mana.

Here, I just copied the “MagicMissle.asset” file and named it “MagicMissleMana.asset”.

Then, I changed the “Name” property to represent that.

The “Utility – Wait for Spell State” node waits for the point in the animation where we throw the magic missile. Once the animation gets to that point, we do a test to see if an NPC is throwing the spell or the character.

I’ve added the “Test Mana” node to see if we have enough mana. So, I add a “Test Attribute Int” node and renamed it. As you can imagine, the attribute we’ll test is if the “Mana” attribute is >= 10.

We remove the link that flows from “Test Attribute Exists” (that checks if we’re an NPC) to “Spawn Particles” and redirect it to our new “Test Mana” node.

If the “Test Mana” node succeeds, we need to reduce the Mana attribute by 10 since we’re casting the spell. We do that with the “Set Attribute” node that I renamed.

Of course we only go there if “Test Mana” was successful. So, we make sure the link has a “Compare Start Node State” that “Succeeded”.

Note, that for this example, I made a change to be able to adjust the attribute value based on its current value. See below for the link to this updated code.

Make sure to press “Save” and that’s it. Now we’ll only cast the spell if we have mana and we’ll reduce our mana when the spell is cast.

If we don’t have enough mana, the spell won’t have a node to go to from “Test Mana” and it just ends. As I said, the animation finishes. However, the spell doesn’t cast.

Updating GUI

To update something like a Mana Bar or counter, we would use the Basic Attributes component again. 

In the “Events” section you can add a function that will get called when an attribute value changes. In this case, we’ll look for “Mana”.

We start by creating a function that can consume the event. It might look like this:

using UnityEngine;
using com.ootii.Actors.Attributes;
using com.ootii.Messages;

public class PrintMana : MonoBehaviour
{    
    public void OnAttributeChanged(IMessage rMessage)
    {
        AttributeMessage lMessage = rMessage as AttributeMessage;
        if (lMessage == null) { return; }

        if (lMessage.Attribute.ID == "Mana")
        {
            Debug.Log("Mana from " + lMessage.Value + " to " + lMessage.Attribute.GetValue<int>());
        }
    }
} 

Then, we add the event and assign the function.

In this case, we put the new “PrintMana” component on our Player and then chose the OnAttributeChanged function.

Now, when we cast the spell and our “Set Mana” node fires we get notified of the attribute change. 

In this case, I’m just printing out the value of the “Mana” attribute. However, we could change the value of Mana Bar or modify a graphic. You can do anything inside your function.

Summary

Of course there’s lot of other things you can do, but hopefully this gives you a good place to start.

As I mentioned, there’s a new “Set Attribute” spell casting node. I’ll put that in the asset the next time I update. Until then, you can download it here: SetAttribute.zip

Just right-click the link above and open in a new tab. That will download the zip and you can extract and replace the existing SetAttribute.cs file.

Page Contents