Variable value inside UI not changing and some weird bug too

This is my code

And what I’m doing is, if player collects coin, I add 1 to the coin collected variable inside player’s BP, I match it with a local variable in HUD and if either is not equal, I set visibility of “COIN COLLECTED” and also set the value of the local variable to the value of the player’s variable so that they become equal again after which, after some kind of delay, I set visibility to false. Problem is, variable value is acting very weird.

My PrintString’s duration is 0 so it should update immediately…?
image

But for some reason, the value is being printed multiple times as if I have multiple PrintStrings running

And when I collect a coin, it shows some values to be changed while some to not be changed.

Basically, this keeps showing me the local variable to keep resetting itself to 0 on every tick somehow

I tried using +1 instead of directly setting the player’s value in the local variable but still nothing.

Basically, this keeps showing me the local variable to keep resetting itself to 0 on every tick somehow

That’s the whole point of using local variables. A function terminates, variable goes out of scope, next time the function executes, the variable is at defaults. Also, you cannot have a latent action (delay) in a function.

the value is being printed multiple times as if I have multiple PrintStrings running

Once you bind a function to a widget field, that’s precisely what will be happening from now on. Those functions do run every frame.

1 Like

If the goal is to collect a coin, pop a counter on the hud and hide it after a few moments, consider the following:

  • this is event driven logic in the player blueprint; there’s nothing in the widget apart from a text block exposed as a variable:

  • we create a widget, store a reference, add it to the viewport and hide it
  • when the player runs into something:
    – ensure it’s a coin and destroy it
    – count it and send the value to the widget, show the counter
    – if no additional coins are collected within X seconds, hide the counter

This way you do not need to run a function every frame, which is considered wasteful, and you do not need to duplicate variables.

I hope I got the intent right!

2 Likes

Thanks!

1 Like