Welcome back.
In this video, we're going to take a closer look at clamping and get a bit of a deeper understanding
about how it works in gas.
Now, we've clamped the changes to our health and our mana.
And if we go back to our attributes set to our pre attribute change function, we see that we're getting
the new value and we're clamping the new value.
So where does this new value come from?
Well, it comes from the calculation performed by our modifier in a gameplay effect when a given attribute
changes.
Now, of course, this function is called even when attributes are set directly, but if this is from
a gameplay effect, that modifier returns the result of a calculation and this is receiving that.
And of course we're checking its value, we're clamping it and making sure it doesn't go below zero
or above the max value.
Right?
But let's see how this works with a little bit of testing.
And you may have discovered this problem that we're about to see by Playtesting on your own.
So I'm going to show debug.
Right.
And we have 50 health and our max health is 100.
Now if we step into the fire, we've now made our fire tag five every second.
Okay.
So far so good.
But what if we go and.
Increase our health here.
We'll pick up a health crystal.
It's going up to 61.
And let's pick up another one.
Okay.
It's going up to its 100 and let's pick up another one.
And even though we are clamping our health, we see that our health is not higher than 100.
But I'm going to step into the fire and our health isn't changing.
So what's going on here?
Why is our health not changing?
Well, I'm going to close the editor and come back here and take a look at post gameplay effect execute.
And I'm going to check to see if the attribute that's being changed is our health attribute.
We can check that by saying if and we can take data dot evaluated data.
So we're getting it from our data here dot attribute that gives us a gameplay attribute and we can compare
that with get health attribute.
So if we make it into this if statement, we know that the attribute changed is the health attribute.
So what I'd like to do here is I'd like to see what that value is here in post gameplay effect execute.
So let's go ahead and just print it to the screen.
We're going to say, Gee, engine add on screen debug message and we'll just give it the key of one
time to display three point f seconds f color red and f string print f and we'll format a string that
says health percent f followed by get health.
Okay, so we're going to print a string that says health and the value of our health.
Let's run this in debug mode.
Debug mode is always more useful because we can place breakpoints and check the values of things.
So we're going to run in debug and let's see what happens here.
Okay.
So I'm going to show debug ability system and now I'm noticing that there's red text up here and we
chose red for our color for the debug message.
So we're going to have to try to strain our eyes a little bit to see what it says.
But we see health is 45 and right here health, same value basically is what the bottom line is.
We're getting consistency, right?
But let's see what happens when we do what we did before.
Now, how did we discover that bug where we stopped taking damage from that fire area?
How do we repro that issue?
Well, remember, I picked up a couple health crystals.
I let my health go all the way up to 100, and then I picked up more health crystals.
Okay.
And then I stepped in the fire.
Okay.
So we see in our message and on show debug, we're getting 100 and we're not going down.
So it's the same value here both in post gameplay effect, execute and here in pre attribute change.
So how come we stopped getting damage?
Well, what I'd like to take a look at is the value here of new value before we clamp it.
So I'm going to repro the bug by going and picking up a couple health crystals and then going way up
past 100 or at least trying to and then going and stepping in the fire.
And then I'm going to stick a breakpoint right here and we're going to hit that breakpoint because we're
in the fire.
Now let's hover over new value.
We see that it's 160 and that is not what we expected, right?
Because we should be clamping every time.
So why is it 160?
Well pre attribute change is getting new value from our modifier on the gameplay effect that's being
applied.
And then we're clamping it before the attribute changes.
So that's great.
But this clamp here is not going to permanently change that modifier.
It just changes the value that is being returned from querying that modifier.
And if we have some other gameplay effect that's going to have its own modifier queried, it's going
to recalculate the value from current value.
So it's kind of like the value was never actually clamped.
We clamped it before the attribute changed, but that modifier recalculates.
So if we continue this and then we hit our next breakpoint, we see that it's 155.
So we are going down thanks to that fire area.
But it shouldn't be 155 in the first place, should it?
Now we can properly clamp this by clamping again over here and post game play effect execute because
this happens after the gameplay effect has already been applied, we can clamp that value again and
we don't have to worry that since we're changing it again, maybe it's going to replicate again down
to clients.
No, we can change it here and there will only be a single replication resulting from changing the attributes
value.
So I'm going to remove that breakpoint and continue.
And we'll close out of the editor and here in post gameplay effect execute.
If health is the value being changed, then we're going to say set health equal to F math clamp, get
health, we're going to clamp between zero and get max health.
So let's run and debug mode and see what happens now.
So let's press play and we'll pick up a couple health crystals, go up to 100 health.
We'll pick up another health crystal and go way beyond that.
And we'll go step in the fire and look at that.
We see that our health is going down to the correct values.
Why?
Because we're clamping in post gameplay effect execute and we're actually setting our health.
We're not just changing new value, which is what we were doing up here in pre attribute change.
This just changes the value returned from querying the modifier.
But here in post gameplay effect execute we're actually setting the health.
So this is a legit set of the health attribute.
So yes, we can clamp here in pre attribute change.
That's basically all that this function is good for.
But remember that all this does is clamp what's returned from querying the modifier.
Anything else that queries the modifier again is going to recalculate what's returned from the modifier
of any given effect.
We are going to actually set the value to a clamped value here in post gameplay effect execute.
And when all is said and done, the actual value will be set here.
Okay, so I can go ahead and remove this onscreen debug message.
And now that we understand clamping a little better, let's go ahead and clamp our mana here as well.
So if the attribute is get mana attribute, then we're going to go ahead and clamp it between the value
of get mana and get max Mana.
And of course we're not calling set health, we're calling set mana and now we're clamping mana as well.
So great job.
I'll see you in the next video.