Getting integer value upon HUD button click (BP reference issue)

I have “PuzzleBlock” BP with clickable block mesh. In it I’m using “PlayerTurn” integer, which is set to 1 by default. When a player clicks the puzzle block, system checks if “PlayerTurn” is set to 1 or 2 and behaves according to that.

I also have “DefaultHUD” which displays button with “end your turn” text.

When a player ends the turn, he/she is supposed to click on the button, which then should SET new integer value. Example: “PlayerTurn = 2”.

And the cycle should repeat.

But I’m having trouble making the HUD button change the integer value, something is not right with my BP reference and I can’t determine what.

This is what I’m trying to do but it doesn’t work (however, it displays 0 compiling or runtime errors)

And this is my debugging attempt in which I figured out that the integer value I get/print is always 0 (not the default of 1).

The reference needs to be set when the blueprint is spawned. For blueprint references setting the variable in the variable panel won’t work.

There are multiple ways to do this. A common way is this:

get all actors of class(puzzle block)> get a copy from array> set/promote to variable. Place this code in the beginplay or event construct or anywhere before you try to set the turn integer.

Now when you use the reference it should work, but if you have multiple puzzle blocks it might give you the wrong one. But that’s an easy fix if that is the case. If you have any issues I can make an example for you later.

Thank you both!

@Triv1 I have a few questions about your graph…

  1. I cannot add Event BeginPlay node to the DefaultHUD graph, it is not there on the list (with or without context sensitive box checkmarked).

  2. Do I need to do something specific with the custom event or just create the custom event node and give it a name?

Thanks!

Hmmm … I’m using default puzzle template as a starting point. All the puzzle pieces are spawned upon game start. Is that the problem? Could I fix this by adding delay in my HUD’s graph?

Hey sorry didnt give explanation. That custom event is un-necessary, it’s a habit I do for being able to re-apply references if there is an issue, but I usually also add in ID checks to get the exact reference. Also you can name custom events whatever you want for your project’s organization.

Beginplay isnt in widget blueprints, its called Event Construct there.

I built all that code on the player character. For singleplayer it can be technically built anywhere but for organization purposes its easier to use a defined method. Where as multiplayer has a more strict organizational structure. How you do that is up to you and your project. For example, you have the player’s turn variable located on the puzzle block BP, this can sometimes get confusing, but it can work. Although an easier organizational method would be to put it on the player controller or character because it is their turn right? But that is another discussion. Here is some documentation to help understand that: Gameplay Framework Quick Reference in Unreal Engine | Unreal Engine 5.3 Documentation

No that is not a problem. The Event Beginplay node is code that is run when the object is spawned into the world, which can be game start or later during runtime. For references they need to be set by code called prior to needing the reference, you can create a reference right before using it or set it up when the object is spawned. It depends on what your project needs.

If you are using the puzzle template start it already has an onclicked event for the puzzle blocks, you wouldn’t need a HUD to make that event, unless of course you want to for your project’s needs.

Here is an example using the default selection method in puzzle template.

Only thing extra I did was add an integer variable to the puzzleplayercharacter for player turn and set it to 1. Now anytime you click on a block it will increase the player’s turn by one. There are other ways to do this using interfaces or event dispatchers, but this will work. You can use that same code in your onclicked event in your HUD and it will work as well. This is direct blueprint communication using casting, if you want to do it as a reference you can do it this way:

Essentially they are the same thing, although some can argue that one way is more optimized and efficient than the other. This is also how you set a reference for when the blueprint is spawned. Widget blueprints use Event Construct in place of Event BeginPlay, there are some slight differences between the two but for your purposes it shouldn’t matter. Hopefully this cleared some things up.