Updating variable in UMG from GI / GM. Why? and How?

Soooo… I’ve done it again. Found a way to get myself stuck and not being able to figure it out :slight_smile:

Short explanation. EndLevel (umg) get’s triggered from GM when the level is finished.
When loading the umg, I do a sequence in the EventConstruct.

  1. I cast to GI and get the previous total points → set a local variable in the umg
  2. I cast to the GM get the current level points # of targets left, # of targets destroyed, time the level was completed in (used as a bonus multiplier) → set local variables.
  3. Calculate the total from the previous total points + new total points (including the bonus) → set local variable and update the grand total in the GI.

The problem is that the grand total gets updated twice. Somehow, at the end of the level, let’s say the previous total was 10,000 and the new total is 560, as soon as the umg loads the total shows 10,560 and after a milisecond it changes to 11,120 (adding the new level total twice).
WHY??? I think I might understand… (probably because I have a function bound to the text box that displays the grand total which refers to UMGGRANDTOTALPOINTS). Since I’m updating the grand total in GI I guess the new local variable gets updated as well.
HOW??? How do I make this work?

Thanks in advance for putting up with my ADHD. I just can’t come up with anything tbh.

Function bind:

Instead of Event Construct try using Event Initialize.

wow, that actually worked. :+1:
why did that work as opposed to the event construct?
what does it do differently? should I always use the event on initialize instead of construct?

OnInitialized runs once during play time, as long as the widget has a valid owner. Construct runs at least once during play time, running again when its slate widget needs to be reconstructed. This happens after OnInitialized but every single time the widget is removed / added or modified in certain ways. Note: PreConstruct works just like Construct but runs after OnInitialized, before Construct, and not only does it run during play time it also runs in the editor. Another thing to be aware of is that some BP properties exposed as pins on CreateWidget are not initialized during OnInitialized (funny I know) but are available during Construct. Things you need to set up only ONCE such as delegate bindings go into OnInitialized. things that update when the slate widget updates (i believe style properties, setting text etc.) go in Construct or PreConstruct.

I’m collecting info on Slate and UMG here:

WIP Guide to Slate / UMG + dealing with current issues.

1 Like

well, I spoke too soon. It seems that it is still happening, but it might be for a different reason.

In my PC event graph, I have the following which I thought I took care of:

Somehow, if the player clicks really really fast the EndLevel gets loaded twice :frowning:

And this is what happens when that happens:

Total points gets added a 2nd time.

My logic was, in PC if EndLevel is called once, set a variable GameOver YN to true, so that if the other branch checks it, on true, it should → Do nothing.

I can’t think of any other way to prevent EndLevel from firing a 2nd time. Any tips???

(P.S. I also tried inside EndLevel, disabling player input completely. it still doesn’t prevent EndLevel from loading twice :frowning: )