"On value changed" event?

So I wanted to apply a speed boost to the character when they equip the speed boost ability. I could just make it so that if they have it equipped it changes the value to make them faster, but the problem with that is the fact that the “Max Walk Speed” value is changed in multiple blueprints, so I’d have to add that condition every time I want to apply the speed boost. I want to make it so that every time the Max Walk Speed variable changes it multiplies the speed by 1.15, but I couldn’t find a node for this. Please help.

Hey there @Fallerrr! There’s a number of things you can do for this, but the simplest being instead of changing the variable directly, you make it an event or function to change it. Then you can just send a new custom event over (since it’s in the same actor) named UpdatedMovementSpeed. That said, when handling a core variable like this, having some event/function is best due to how messy allowing just any objects to change the speed of the character.

That would work if I did it earlier. I would do that if I knew every place I modified the value. If I only do it the the parts where I remember changing it, then if it changes because I missed a certain bit of code, then there won’t be a speed boost until the code that I modified is executed again.

I understand! Refactoring can definitely be a pain later in a project! Though if you are intending to maintain the project it might save you a major headache in the future to do it before it gets too big, as tech debt can be painful!

Though, according to this post, if your game isn’t multiplayer (and not going to be) you could use the rep notify feature which is basically the same thing, but made for multiplayer replication. Delegates are also a possibility, but that’d also mean you’d have to go back and refactor the same way anyway.

1 Like

it works now, Thanks!

I had never though about using Repnotifies for this however, so keep an eye out of issues it may cause. In theory there shouldn’t be any considering it’s purpose built to fire an event when something changes, but if you ever include any multiplayer components to your game things will definitely be a bit odd.

OnRep still has the “but who triggers the Rep?” problem.
The real solution to this problem is to use the Gameplay Ability Framework.
That’s what it’s for!

I agree! Though setting up GAS would be far more than just a refactor for this user’s use case. That and it still requires C++ to setup, so I figured the shorter refactor or the hacky workaround might work out a bit easier. Though with multiple actors accessing direct variables, there is likely a refactor in the future either way!

Here is a handy macro I use a lot:

you could run this on tick or a timer, then fire event from On Changed execution pins. I can understand the desire to have it handled separately from like a Setter, just because then you have a clear separation of responsibility and more searchable code.

Like if sometimes you handle event dispatches when using a Setter and other times not, then you have inconsistent code and it causes confusion. Whereas if you have like a system which is just for handling dispatches based on variable changes, then you have more of a single source you can go to and find via text search. Of course the key is just to have consistency no matter what approach you take. It won’t always make sense to poll for value changes constantly if they will only ever change from an event but that’s just situational judgement calls to make.

1 Like