Optimizing selected spell


I’m hoping I can describe where I am at, where my brain is and what I’m trying to do so it’s understandable. I have playing around with a game for fun and have a very basic setup for left click to charge a spell.

I realize that I have the wrong node for 1. it’s supposed to be “chargeShield”…
I have an integer that I change with the scroll wheel 0,1,2. Depending on which integer is selected will depend on which spell I cast. In prototyping it works. But thinking ahead as I create more spells I need a better way to cast my selected spell but I don’t know what that is. I have ideas for a dozen spells but don’t want a selector that is a dozen deep. Plus I have all the node programming for charging within my players blueprint and adding the node programming for a dozen spells would make the player BP outrageously big.

Like most, I don’t know a whole lot about programming but can get around pretty good within UE4 blueprint. Ideally I see something like each spell contained within it’s own blueprint that has the same function names. If I create ten spells I would only want the player to have access to three or four at a time. So I would want to be able to specify which spells they can use, and which one is the active spell. Then, I assume, through an interface ‘Charge’, ‘cast’, etc the active spell. I don’t know how to tell UE4 “Hey when I left click use interface to tell the ‘Shield_BP’ to charge”.

Does that make any sense? Am I way off in my thinking about how to cast spells?

Create an array of spells.
Each time the mouse wheel moves, increment/decrement the integer of “selected spell,” but don’t let it go less than 0 or greater than (length of array - 1)
When you need to get a particular spell, get the N-th element out of the spells array, where N is the selected spell integer.

Separately, either make each spell implement a Blueprint Interface that contains “Charge” and “Cast” and “HasValidTarget” and whatnot.
Or you make a base blueprint for “Spell” with all the functions you need, and make each of the spells derive from that base, and override those functions.

You want to have a “Class_Skill” which is an actor (or an object) that contains all the “Skill functions” like charging, triggering the cooldown, triggering the effect blabla.

Then you want to have child classes of that class: your spells. Skill_Fireball, Skill_Shield for instance. In these child classes, you want to override some of the functions of the parent class to make it so the skill behaves as you wish (for instance, the TriggerEffect function spawns a projectile with the Fireball class but it spawns a shield with the Shield class). But everything else behave the same between these two classes.

Then on your PlayerController (or Character), you have an array of the type “Class_Skill” which is all the skills of your character (you will first have to spawn these skills in the world in order to use their functions and variables).

Then when pressing Left Click, you cast “TriggerEffect” on the index “SelectedSpell” of the “Class_Skill” array.

Fantastic, Thanks guys. I think I’m on the right track and have a workflow pretty well figured out now I’m just trying to understand some classes.

From doing some internet digging “Actor Components” have the least overhead, correct? The spell data itself doesn’t actually need a location in the game so I figured just using actor components was easiest. But, it appears that unless I figure out C++, which won’t happen, there is no spawn actorComponents based on a class node, correct? I’m reduced to having all my spells be actors.

Which I guess in the long run probably won’t make that much difference because really this is just a game I’m making for fun and probably not something for the world so massive optimization isn’t really a problem. I do like making it run as best as possible though.

Typical case for an Interface.

  1. Create an Interface “Spell” with just the function “cast”
  2. Create classes implementing this Interface: Fireball_Spell, Lightning_Spell etc.
  3. Implement the “Cast” Function the way you want.(Spawn Fireball, heal character etc.)
  4. Create an Array of “Spell” as a variable in your character.
  5. Select the Spell as usual with an index.
  6. To cast a specific spell, just get the Spell from the Array and call the “cast”-Function of this Spell.

This is typically not the kind of case where you want to use an interface. You want to use an interface when you don’t know what is going to be the class receiving the event among the different classes that can receive it.

In our case, we know what is the class of the element receiving the call: the Skill class.

Fantastic, Thanks for the help.
I have an actor component called “Spellbook” that I keep all the spells a given player knows or finds in an array. I have a “SpellBase” ActorComponent class that all spells are children of.

With some help from friends I added c++ code to add any queued spells Actor Components based on class (OMG such a helpfull node) . I can easily spawn and destroy actor components based on the spells you want ready. The spell you are actively using is the “ActiveSpell” and gets all the input commands, with all spells using the same functions I can very quickly switch from one spell to the next and use the same commands/button presses to cast pretty much anything in my arsenal. Time to create some more spells.

Plus I can put the Spellbook on any bot, fill it with some spells and suddenly he becomes a spell caster.

I can’t wait to get a demo going.

If I was you, I wouldn’t use a separate ActorComponent for every single Spell you have, but simply use a variable as the current Spell within your SpellBook Component.
You can expand your “BaseSkill” class with functions like “On Select”, “On Deselect” etc.
This way you can have unique behaviour for every Spell you have when selecting/deselecting.
For example:

Select: Create Fire Particle Emitter on the Hand
Deselect: Delete Fire Particle Emitter and play short Smoke Particle Emitter.

There are many possibilities to implement this, but creating an ActorComponent for every Spell is quite some Overhead if you ask me. Not to mention that a change in the BaseClass would mean that you have to do the Change in every class, too.
A single ActorComponent “CurrentSpell” with a variable pointing to the current Spell would do it, too.