Spell system, how to start.

Hello,

I wish to implement a spell system. A character can equip/unequip different Spell (fireball, lightning, etc…) from a spellbook (an array containing all the avaiblable spells) and, obvioulsy, use them :wink:

I’m kind of confused and don’t know where to start, should I used a component? An Actor? A sub-object? Other?

Thanks a lot for you help :slight_smile:

I’d suggest taking a look at some of the inventory system tutorials, they’ll give you a good base to work off of. Also you might want to just do it in Blueprint unless you have a good reason to use C++ for the base to start (you can always translate it into C++ later).

Basically, you’ll want to extend the Actor class with a blueprint called Spell, or whatever, and that responds to an input (like clicking the LMB) by doing a line trace to see what you hit, playing any particle effects/animation etc.

Blueprint is what I’ve been using, and its extremely flexible.

I have a series of check in place when attempting to ‘use’ (input via LMB). If you have mana, and you have a spell, and that spell is ‘equipped’, it will attempt to cast the spell. The casting process essentially looks at the mana requirement, checks to see if you’re able to cast (not dead, not paralyzed, not under the affects of a silenced/dispell), and spawns a blueprint actor containing the functions of the spell. The actor is spawned to an offset of the character at the moment, so the spells appear to be casted from the players free hand. Once the spell has been cast, there is a cooldown timer the is applied, where no spells can be cast for the duration of the timer.

Look at the first person BP example; most spells are simple projectiles with various velocities, cast times, damages, and mana costs. A fireball is simply a large ball with a particle emitter that creates a radial force upon impact, and applies damage within an overlap sphere (as well as a camera shake). Magic Missile is another simple projectile that has a trace sphere attached to it, and should any object fitting the traces filter array come within distance of the trace, it moves toward that location.

TL;DR - look at projectiles, and modify their visual appearance to fit your spell. Functions can be added on top of the projectile to make them more spell-like.

Thanks for your answers.

I somehow managed to do something with my spell. I created a class derived from actor, which I spawn whenever the user use the corresponding input. But, this is not generic at all and I think this is not the most efficient way to do so.

If I understand correctly what you said and how the blueprint works, a possibility would be a class Spell derived from Actor which implement all the effects needed and then the character use these blueprints to spawn the spells.

There is how I see it in a rough way.

Please, obviously don’t mind the formating, this is just to illustrate :wink:


Spell : public Actor
{

/* Functions */
CheckCondition
Damage
Heal
Boost
SpawnChicken
...

/* Parameter */
manaCost
power
incantationTime
chicken
...
}

In order to “create” a spell I just have to create a Bluprint based on this class and assemble the differents effects (function) and set the parameters of the spell via the blueprint editor.

Then, to launch a spell :

1- The character spawn the blueprint spell and set the different parameters (the target, the caster mana, etc…)
2- The spell then start, check if the caster has enough mana (and all the other conditions) If the condition are not met, the spell is aborted. Else it call the different effect on the target and then die.

Or something like that. Spawning a spell then check the condition is kind of odd. I would rather check the condition, then spawn the spell. But if the conditions are in the spell, how to get those conditions without spawning the spell first?** Is it possible to have a access to the blueprint value without spawning an instance? **(like a static)

My other question now is :** how to reference the spells in my character?** To spawn a spell I think it is possible to spawn a blueprint with just knowing the name of the blueprint, so I could have an array of string and manipulate those string to equip/unequip/learn new spells. Is it a good way?

Thanks again for your help :smiley:

Late, late, late reply here, but in case anyone else is having trouble with this same topic… I’m currently developing a system whereby you can equip a different weapon at the start of the game and each weapon has a special spell ability you can use when it’s equipped. I’m far from finished, but just wanted to chime in because I’ve got a few pieces to this puzzle sorted out so far:

  1. Data Structures. Each weapon I’m using is housing a custom struct variable being inherited from the weapon parent class. This struct holds everything we need, basically – mesh data, particle/light colors, min/max damage variables, element type, names, etc. The most important one is the custom enumerated list for Element Type which allows us to make quick changes based on which weapon you have equipped (Switch on Enum_ElementType, or something of that nature). The key to all of this is the struct variable and filling it properly, so that we can add inputs/outputs for on various functions and events to carry information all over the place - to enemies, weapon hits, spell casts, player BP, HUD widget, etc.

  2. Order of operations after input. So (this is the part I’m at now, still fleshing it out) when we call the input from RMB for spell casting, the first thing we’re doing is checking… Is a spell available (reset timer)? What weapon am I holding (ElementType enum switch)? Then doing all the casting and line tracing for the various spell BPs, which is what I came here looking for. It doesn’t make sense to me either to spawn a spell then check if a spell is available or not. Seems like a hassle, honestly, especially if you have a reset timer on it because you’re just going to muddy up your logic for handling the timer. Seems much easier to do it all before you spawn the spell object.