Cast event when (Variable change)

My question is simple how Custom Event in this case (Event 1) Can be casted whenever Variable change for example.
Integer (100) When it comes 99,98,97 etc… cast Event 1 for every change cast my event …

Your wording is a little confusing, but I’m assuming you actually want to trigger or fire an event when the variable is changed? Casting is a whole other thing, which you may or may not be aware of, but you do not cast to events, you cast to classes.

In any case, there are different ways of going about doing this, depending on what you’re trying to do. You could set the replication of your variable to ‘RepNotify’ which will create a function for you that will fire every time the variable is changed. Though this method is typically used with networking, so I’m not sure what the result would be if your game is local only. Never thought to try.

If you’re using UMG you can ‘Bind’ a function to a variable, but as far as I’m aware that will call the function every frame, which will get a little heavy depending on what you’re doing.

You could also store the current value of the variable in a second variable, then on tick check to see if they are the same. If your variable changes, call your event and set the second variable to match the first again.

What exactly are you trying to achieve? There may or may not be a better way to accomplish whatever you’re trying to do.

4 Likes

Hi there,

I believe you want a sort of delegate event (“on value changed”) or maybe an “event dispatcher”. If you can give us more details about your logic, with screenshots, that might help other devs to sort things out with you.

For those who, like me, are faced with this task, I can recommend the following :tipping_hand_man:
If you are developing a singleplayer game, you can use the RepNotify for Replication setting of the varible and use generated OnRep_ function to call the event on the varible change.
This method works in single mode because

Standalone: The game is running as a server, that does not accept connections from remote clients. Any players participating in the game are strictly local players. This mode is used for single-player and local multiplayer games. It will run both server-side logic and client-side logic as appropriate for the local players.

However, for those who, like me, are working on a multiplayer project and want to do this only on the client and at the same time not overload the server and network - alas, I don’t have an exact way to solve this yet.
Yes, you can create and use a “set function” instead of a just “Set”, but this option will only work when changing the values (calling the function) ​​​​in the blueprints, and not when the value of the variable is directly changed (like in maping).
And of course, you can always use some C++ custom class and code, but we’re not here for that :sunglasses:

So for now, I can only suggest a method that is used in the Lyra project - create a second variable that will be every tick/period of time, check if its value differs from the target one and when this happens - call the event and update this variable value.
image

2 Likes