How to add "credits" (like arcade game) whenever insert coin is pressed?

I went to project settings/ input and created an “insert coin” action mapping on the controller left special (select) and the keyboard 5 key.

I created a credits widget and placed a horizontal bar in the lower left hand corner. I added the text “Credits” to the left and added the text “0” to the right. I selected the 0 text and chose to bind to the text. It was at this point I discovered that you can’t get variables from the level BP.

Added to viewport in the level BP.
So supposedly an option is to make a variable in third person character bp that you will get access to in the widget using cast. I made an integer variable called “Coin Counter”.

The widget does display but pressing select does not change the zero on screen. I noticed that when I played the game the widget had updated from zero to 1 but it was not progressing further.

Anyone know what I’m doing wrong? Thanks

you need to connect Coin Counter to the Increment(++) node :
image

I just tried that, no effect.

Can you share screenshots of how you’re creating and displaying the widget?

I would suggest using an Event Dispatcher to broadcast whenever you change the Coin Counter Variable, so that the widget can bind to that and update the Text. This would be more efficient than using widget binding.

  1. Create an Event Dispatcher in ThirdPersonCharacter called something like OnCoinCounterUpdated
    image

  2. Select the newly created Event Dispatcher and add an Input to it called CoinCounter and set the type to Integer.

  3. Compile the blueprint, after you increment CoinCounter on Insert Coin Event, also call OnCoinCounterUpdated. Compile again.

  4. In your widget’s Event Construct, do Get Player Pawn → Cast to ThirdPersonCharacter

  5. Drag from as Third Person Character and search for Bind OnCoinCounterUpdated and add it the graph.

  6. drag from the Red Event pin on the Bind Node and do Create Event, hit enter and call it something like “Event_OnCoinCounterUpdated”

  7. from that event use the Coin Counter variable to update the Text.

This way, you only update the widget when the Coin Counter variable changes instead of doing it every frame.

Yes

Why Event Tick? no need to create the widgets every frame, use Begin Play instead. Currently you’re creating a new Coin Counter widget every frame.

1 Like

Ok thanks for the help. I created an event dispatcher in the third person character but I’m not getting the option to name it nor can I change the variable type to integer. Are you using UE5? I’m using 4.

Sorry I don’t know a lot about widgets but I can try begin play.

1 Like

I’m using UE5, but it should be the same on UE4.
You need to press the + button on the top right, just above the “Copy Signature from” button. Is that button missing?

[EDIT] this button :
image

Ok found it and have done most of what you said. Just need to connect text counter in the last pic. That is an obj reference, from what exactly? The counter is an integer?

That’s the text block inside the widget

You need to mark it as a variable to get the reference variable in the graph
image

Ok I need you to explain that some more if you don’t care.

Ok, you don’t need to use the binding function anymore, you can click “Remove Binding”.

Instead enable the “Is Variable” option. This will make a reference variable of “TextBlock_1”.

We can then use that variable to do whatever with the text block. like setting the Text.

The reason I suggest not using the binding is because, that function will get called every frame, but since you’re only updating the Coin Counter variable when clicking a button, we only need to update the widget once, when Coin Counter changes.

This is where Event Dispatchers help. This is more efficient since we’re only updating the widget when the variable updates.

Ok I ticked it and removed the binding, but I can’t seem to find it for that node.

Image6

it’s the Get TextBlock_1 you’re hovering over. the variable in my screenshot is just named differently.

Just found out I had wrong text block node. Now it connected.

1 Like

That’s great. Now, did it work? :sweat_smile:

Unfortunately no, :frowning:

Ok, what doesn’t work? Is it the same as before?