Welcome back.
Now that we have a magic circle class, it's time to show these on demand.
I'd like to have easy to use functions to show and hide a magic circle and have it follow the mouse.
Now, this is something I'd like to do on the player controller.
The player controller will be valid locally, and we should be able to show these magic circles if we
were to say spawn one.
So I'd like to have some variables on our player controller.
For that, I'm going to close the editor and go ahead and open my public folder, go to player and open
or a player controller.
And we're going to add a couple variables to this.
Now here in the private section, I can add a couple things.
For one, if I'm going to want to spawn one of these Magic Circle actors, I need a Uclass to spawn.
So I'm going to make a subclass of to specify the class to spawn, and this is going to be an a magic
circle actor.
Now we can forward declare magic circle here.
So I'm going to go ahead and do that class a magic circle.
As that's the name of our Magic Circle class.
And we have the you class.
And it looks like I misspelled it there.
There it is a magic circle.
Let's make sure I didn't misspell it up here.
Looks good.
And I'm going to call this Magic Circle class.
And this one will set the class in blueprint.
So it's going to be edit defaults only.
And in addition to Magic Circle class, we're going to want to be able to spawn one and keep a pointer
to that magic circle.
So I'm going to also create a t object pointer of type a magic circle.
And this is going to be the magic circle itself.
And this gets a new property as well, but we don't really need to expose it to blueprint.
And for our magic circle, I'd like a function that we can use to spawn this thing.
And that function I think, can be public.
That way we can access the player controller and call this from outside the class, and I'm going to
have a void show magic circle.
And we'll go ahead and have a void.
Hide magic circle.
So we can go ahead and define these functions.
And I'd like to lazily spawn our magic circle, because chances are you won't be always using the Arcane
Shard spell.
Unless, of course, it's your favorite spell or something.
But if you're not using arcane shards that much, there's no point in a magic circle even existing.
Especially if the magic circle is going to use things like tick.
If it's going to be updated every frame, and it will because we need to move it according to our cursor
location.
So for that reason, if we have a magic circle, yes, we can update it every frame.
But if we're not even showing the magic circle, I'd like to just destroy it and we'll just create one
whenever we need one in Show Magic Circle, and we'll set our magic Circle pointer, which we have down
here.
So at any point in time, this magic circle could be null or it could be valid.
So if we call show Magic Circle, I'm going to want to spawn a new magic circle, which means I'm going
to use get world spawn actor.
And I'm going to spawn an actor of my magic circle type a magic circle, and I'm going to specify the
you class, which is going to be my Magic circle class.
And if I spawn this, then my magic circle is going to be valid because I'm going to store this in my
magic circle pointer.
Now, I don't need to use the spawn actor overload that takes in a location or a transform or a rotation.
I'm going to set all of that stuff if my magic circle is a valid pointer, if it's actually in existence,
if we've spawned one and in Hyde Magic Circle, I'm going to destroy that magic circle.
So I'm going to take Magic Circle and call destroy on it.
Like this.
Now I'm going to first check if Magic Circle is valid.
So is valid.
Magic circle.
Because if it's pending kill, I don't want to try to destroy it because it's already being destroyed.
So we're going to make sure that it's valid first and then call destroy on it.
And when we're actually spawning an actor, we should only spawn one if the magic circle is not valid,
if it's not in existence.
So we can say if not is valid.
Magic circle.
In that case, we'll go ahead and spawn a new one.
Now, if we have a magic circle and it's valid, we should be updating its location.
And every frame in player tick, we're performing a cursor trace and we're caching our trace destination,
our cached destination right here in our cursor hit.
In fact, we're just saving the entire hit result and cursor hit.
So what we can do if we want to set our magic circle's location is we can set it using that hit result
every frame.
So I'm going to make a function here.
It can be private I'm going to call it void update magic circle location.
And this can be called in tick.
And every frame.
As long as we have a magic circle we can go ahead and update its location.
And for that reason I'm going to check if is valid magic circle.
That way we don't have to worry about this.
If the magic circle isn't valid, and most of the time this is going to fail.
So what we'll do is we'll take our magic circle.
And we're going to update its location.
We're going to call set actor location and we need to take our cursor hit.
Dot impact point.
We can use that.
So we're updating that location.
We can call Update Magic circle location in our tick function player tick.
And most of the time the circle won't be valid.
Unless, of course, we're showing it.
Now show magic Circle and hide magic circle could both be blueprint callable functions, just so we
can easily show and hide the circle from blueprint.
And so I'd like to do that.
I'm going to make them both a blueprint callable function.
And this allows testing to be a lot easier for this.
So with all that, let's go ahead and compile and launch so that we can test out showing that circle
by calling a blueprint callable function.
Okay.
So we can go ahead and open up that magic circle.
There's really nothing to do here though.
We want to go into blueprints player and open up Aura Player controller so that we can set some things
here.
For one, we need to set that class for our magic circle so we can find that magic circle variable.
Here it is magic circle class and we can set it to Beep Magic Circle.
And with that we can just call Show Magic Circle on begin play and we can hide it.
We can test that out by just using a delay.
And we can delay for about, say, 5 or 6 seconds.
And then we can call hide magic circle in which our magic circle will be destroyed.
So let's try this out.
We'll go ahead and compile.
We'll save all and we'll press play.
And here's our magic circle.
It's following our mouse location.
And in five seconds there it goes.
It goes away.
And we can try bringing it back again after another delay.
I'm just going to duplicate these nodes.
I'm also going to duplicate that delay.
And I'll make it a one second delay.
And after we hide the magic circle we'll wait for a second.
And then we'll show the magic circle for another five seconds so we can try turning this on and back
off.
So it's on for five seconds.
And we can now see that it's gone.
And the second later it's back.
Five seconds after that, we should see it go away.
And there we go.
So we only have a magic circle in existence once we've called show magic circle.
And when we hide the magic circle, we destroy that actor so it doesn't exist anymore.
And it's not doing things, say, in its tick function, for example.
And that's really nice, because now we can do things in the Magic circle's tick and not worry that
they'll be ticking like that when we're not even showing that magic circle.
So we could take tick in our magic circle.
We could take our magic circle decal if we like.
We can call add local rotation if we wanted to, and we can just rotate the magic circle.
We can go ahead and split the struct pin and we can take delta seconds, multiply it by some value,
say ten or maybe eight something small and we can change its pitch.
Now remember, if we show that magic circle, if we were changing its your it would be rotating about
its z axis.
But if we're changing its pitch, we're actually going to be rotating it about its y axis.
So actually I think it's the role that we need to change.
So we're going to actually add rotation to the role.
And Magic Circle decal actually is not exposed to the event graph.
So if we're going to want to play around here in the event graph with it, we're going to have to expose
it so we can go ahead and close the editor.
We can go to Magic Circle and take that decal and give it blueprint read only.
So we can go ahead and launch the editor again.
And with that, we should no longer have that error.
We can compile just fine.
And if we press play and now we see our decal is rotating and it looks great.
Awesome.
So I'm going to go ahead and remove this decal.
I can't really select it.
So I'm going to have to search for Magic Circle.
Select that in the outliner remove that.
And now we just have that decal and it's being spawned by our player controller.
And of course it goes out and back in based on those delays that we have here.
We're just playing around in blueprints at this point, but we now have the ability to show and hide
the magic circle, and that's going to be crucial for our Arcane Shards ability.
So excellent job, and I'll see you in the next video.