How to have a constant variable just inside a custom event and not add it as variable of blueprint

Let’s say that you want an integer inside a custom event that is random in range and you want to use it in multiple locations but you don’t want to add it as a variable inside that blueprint (as example maybe you have 100+ custom events inside the blueprint, and each has 1 or more random numbers, that would mean 100+ useless variables inside the blueprint, which would even be a nightmare for organization), if you are going to do this:

Looking at the blueprint even tho you used “the same number output”, in all of the Print String the Random Integer in Range is called again and will return a new integer for each use.

One way to get around this:

Call the Random Integer in Range function, but don’t use the output where you need, instead first take the output and call the Increment function for that number, and then from the output of the increment, call Decrement so that you reach the number from before and use that output where you need the same number. While in the above example it doesn’t appear to make sense to call decrement right after the increment as the number would be random anyway, well you can get into the rare situation where Random Integer from Range will give you the maximum value, and then because you incremented that by one, you’ll be outside the range you wanted and this can be especially bad in this example:

In this situation it will try to get the value from a position that doesn’t exist inside the array, as random(0, size - 1) + 1 can return the value of 3. But by using both increment and decrement, then it will never try to get a value outside the size of the array, and by using the value from that output, you can always get the value from the same position from inside the array.

If you would have done simply this:

Then you will get in both situation a random value (well there is that slim chance where the random would return same value in both cases, but if that value would have been in the thousands+, then the chance would practically disappear).

This method works with both Int and Float types as they both have the Increment and Decrement functions.

You can use it in combination with something like this:

To have a random rotator that is constant in the event (in this situation you can make the random rotator and apply it to an actor, and get that actors rotator later, and you don’t really need to make that ++ --, but it was an example of a possible combination).

make it a function and you can use a local variable

Add a non-pure function to a library and have a custom event pipe the data in:

Calling the custom event above would print the same random number thrice.

This should make it flexible, does not require an additional variable, can be used everywhere but may not play all that well with latent actions.

2 Likes
  1. Create BlueprintMacroLibrary and add the following macro to it:

  2. Use it anywhere:

Same for arrays:

My Products

3 Likes

This looks nice as it is using wildcard but does it have downsides? How @Everynone mentioned a situation in his version. I can see some variations of this that would be useful in a few functions/events in my game as well.

In case I was too enigmatic :innocent: This will print after 10s, sure. Will it print after 30s, maybe? How about 5 mins? I treat all 3 as unsafe and avoid them.


(as example maybe you have 100+ custom events inside the blueprint, and each has 1 or more random numbers, that would mean 100+ useless variables inside the blueprint, which would even be a nightmare for organization)

What are the chances there is a much neater solution to all of it? For example: why would I have 100 custom events? Why not Event Dispatch 100 things to single event instead?

All I am saying is that you might be experiencing tunnel vision. Hard to advise without knowing more.

1 Like

Oh man… how many times I did something that at the time I thought it was a good solution, only to see it later when doing something else and notice that I can improved it. But if that wasn’t the case then we would not have the word “optimization”. :smile:

One real situation that I had is that in one spot I have multiple triggers, but I want only one of them to get triggered at random. I have them all in a list, and I’m getting from that one of them at random and activate it, but after I want to disable it again, as they can be triggered multiple times, and not only once, if that was the case, I could have simply destroyed the actor, hence I need the same value I used the first time for the get. That is why i needed a way to have a random number, but to be constant later as well.

Not sure if you’re not familiar with available methods, or seriously overthinking… but…

Use a function, rather than an event.

as example maybe you have 100+ custom events inside the blueprint

In general, I would say that’s the first sign that you’re doing something that can probably be simplified.

I can’t use a function, as I need to have a delay in the execution.

That was an exaggerated example to show that maybe you can’t have a variable, and not only that, there are situations if you do use a variable inside the blueprint it may get overridden by a different event by the time it is needed again, and so you need alternatives.

This sounds like “A Variable, But Not A Variable”.

This all sounds like a combination of several bad patterns, all jammed together.

Timed delays are usually a bad thing (you usually want things to be event driven, not time driven), overwriting intermediate results from one call, inside another call is a bad thing, and the designs that allow for that to happen are usually bad things, having tons of custom events, etc.

It would be helpful probably to explain this in a little more detail, and find an approach that doesn’t put you into using a pattern like this.

The way I’m understanding this, is that you have multiple Actors in a single location, that are triggers, but you only want a single one of them to trigger, selected when they first fire, and then that same one will trigger any other time, and the others will be disabled?

so, you have one space that the player can enter (or shoot at, or whatever), and it will fire one of a random selection of effects, reset, and then all future times it is triggered, it will fire the same effect?

am i understanding that correctly?

Let’s take as example a situation where you have a trigger box, but you don’t want something to happen at the exact time a player enters it, but soon after that, to maybe have more players in that area, or to punish someone for camping that spot. And so you have to wait a certain period of time before something happens, be it by using a delay or a timer by event (which also can’t be used in a function), either way you go about it, a waiting period must occur.

I’m not using that, it was a reason why you should not use a variable inside the blueprint in some situations. Like the one I mentioned with the list of triggers. If the value for index in get is a variable of the blueprint, it may get overridden by another event if you used the same variable on multiple places and not made another one, which would be useless as you only need that value local to the event, and not for the entier blueprint. While functions can have local variables that isn’t the case for events, and in some situations you need an event.

In my posts I try to give bad/good reasons as well, as in general the threads aren’t useful only for the author but others with maybe same uncertainty/problem, and beside what the thread is about, can learn something else while reading about it that are kinda related, and I also try to keep a common tone and not use a professional one with lots of words that only make you sound smart (with this I’m talking in general, REALLY don’t take it as I’m talking about anyone in this thread), and in the end may make me sound that I’m really bad. :smile:

Yeah, that is correct, that is the exact effect that I want to achieve, and what it does. That is where I used the ++ – method, so I can have the same index from that array, first time to trigger the effect, and second time to deactivate the trigger as it did it’s job and next time maybe it will get triggered again, or by random another effect, so it shouldn’t be active all the time.

I don’t know, this entier thread has been taken a bit way to serious in a way. Yeah maybe I did exaggerated with the 100+ events, but I wanted to show a theoretical situation where using a variable isn’t the right thing to do, but also what I posted wasn’t a “cry for help” (this also sounds bad, but something else didn’t came to mind right now) as the solution that I’m using is working fine, it was more of a “Here is what I’m using, maybe there is a better way to do it, if not is fine as it is not a performance intensive solution anyway”.

But also in a way it is good that it was taken serious because it shows that the members of this forum does show it’s support for their fellow developers, and it is way better then other forums where if you ask something that for a professional sounds to be stupid, then they start giving you - on the thread without any explanation, which I think it is a stupid mechanism as you don’t know why you get that -, what is bad, and the only thing that it does to many (and I know a real case, where a friend stopped trying to learn programming because of said website) is to demoralize them.

Sorry for the sad/annoying or any other sentiment this thread may have caused. I’m really grateful for all the help I received in this forum, and the help that I will receive in the future as well, as this is the first time I used Unreal Engine to make a game (at least I’m coming from a Java background so I have good knowledge about programming but not the engine), as for sure there will be stuff that I’ll get stuck at.

:bowing_man: :beers: :grinning:

so, probably way i’d do that is have a single trigger, a variable that stores which one should fire, set it on BeginPlay (or when triggered initially…), and just do that.

be it by using a delay or a timer by event (which also can’t be used in a function)

ok, but you can call a function from an event

well i’m trying to understand what you’re getting at doing, because writing additional code to avoid declaring a variable is a good point to do some course correction on SOME part of the process. Whether it be learning about a feature that wasn’t known, approaching the problem from a way that makes it simpler to handle, or finding a better way to achieve the overall … because I’d say that this is all pretty hacky, and the reason there’s no way to do what you asked directly, is because it’s just not necessary.

I don’t want to discourage, I want to help… but there’s not a way to do what you’re directly asking about.

For the overall problem, using a trigger with multiple effects, i would…

  1. have a variable that remembers which effect was last used
  2. have a single function that sets that variable
  3. only call that function from a single space, such as beginplay, or when it hasn’t been set and it’s called by the Trigger event (and then if you need to reset it completely, you can just clear that variable)
  4. When triggered, query the variable, and follow the path for that specific trigger effect.

IF there’s a possibility that there could be a delay between trigger and effect, and that the effect value could be changed between trigger and effect, then spawn an actor that does the effect, or pass it off to some other thing to deal with.

I just want to add, that such solutions are what i would call “a hack”.
There is risk that they (Epic) change such blueprint behavior a bit in future (for eg. new optimization, or merging stuff with Verse). And this method will not work. In a year or two your game prototype could stop working, and You may forget about this nice hack.
So imo. it is better to think what you want to do, why you need it, and what is “unhacky” solution. Less hassle with maintaining code later.

1 Like