Checkers game - selecting and deselecting pieces

Hi, I’m a super newbie, and I am making a simple checkers game with BPs only, as an introduction to game-making generally and UE4 specifically.

Currently, all I have is a board and pieces - on pressing a key, 12 pieces per side are spawned in the starting locations, coloring themselves based on the controlling player (which for now is just a label - p1 or p2).

I have posted an image of the Piece BP network, which toggles a glow effect on and off when clicking on that piece. My question is, if I click on one piece and then a different piece, how do I tell the previously selected piece to turn off its glow effect? Presently, the only way to turn off a piece is to click it again, meaning multiple pieces can be selected at once. I’m only dealing with a glow material effect because I recognize that once I have event handling straightened out I’ll be able to deal with everything else about the game logic.

You can see in the image that I have created an event dispatcher and then called and bound it in the same BP. My assumption is that since 24 pieces are spawned as copies of one BP, then 24 instances of that BP are running in the game: 23 instances will respond to the 1 instance that dispatches the event. However, this does not work as I expect in practice; instead nothing happens besides the normal single-target toggle on/off, allowing multiple pieces to still be selected. Is there something I’m missing about the way BPs and events work together? How do I code a BP to interact with other instances of itself, especially since none of the pieces are placed in the level editor (they are spawned at run-time)?

I also have a more general question about data and logic structure in a UE4 project:

  • Eventually, I am going to add other board games like chess and go, as separate levels reusing the board and the base “Piece” class. I would like to keep all the game-agnostic logic, like keeping track of which piece is selected, on the pieces themselves or in the Game Mode BP. However, all the examples of events and BP communication in the docs use the Level BP as part of the communication pathway. Is there a particular reason for using the Level BP instead of the Game Mode BP? I would like to avoid repeating basic interaction code for every game, since each level will be a different game with different rules.

I hope this isn’t too much to ask at once, but I only have limited programming experience, so I’m not familiar with OO design patterns or how to structure anything more complex than a simple python module! Any help getting me over this mental obstacle will be enormously appreciated!!

Ok so I’ve simplified the toggling of checker piece highlighting, and now I think I have the correct event dispatching setup in my Piece class, yet it still does not toggle off the non-selected pieces correctly:

And this is the function for turning highlighting on and off, very basic but again I’m new at this:

With this I would expect the 23 Piece Actors that I haven’t clicked on, to fire off the disable highlight event when I click on the 1 other piece, but instead once a piece is toggled on, it stays on.

I’m not sure what it is that I’m missing, I’m pretty much following the docs event dispatcher example exactly - any help would be greatly appreciated!

What I would do is have every peice an actor or whatever class and OnClicked() (make sure you enable clickevents and cursor in player controller) enable the highlight & set a boolean bIsHighlighted to true and then on Event Tick() do a function timer node that calls the custom event DeHighlight and sets a bIsHighlighted boolean to false

Youll need to delay the tick based on how long the mouse is held down for you could do IsButtonDown(leftMouse) → branch true → bIsHighlighted = true false → bIsHighlighted = false

Otherwise look into blueprint interfaces. This allows you to share functionality between classes that dont inherit directly from eachother. (In a programming sense, BP’s is slightly different)

Success!

I used “Get Hit Result Under Cursor By Channel” in the PlayerController BP, and then just a few branches to decide whether to highlight or not, or to de-highlight previous selections.

In the end, I just had to learn a lot more about what kinds of functions are available in UE4! Hopefully this helps someone else in the future!

Event to select piece

Decisions to highlight or not, or de-highlight previous selection, if there is one

Thank you for the ideas, but after I looked into it I decided tick functions are more than I need for this problem! I also checked out BP interfaces, but there is really only one Piece class, so I wasn’t sure how to use an interface to communicate between multiple instances of the same BP.