How to make a callback function when a UPROPERTY is being replicated in C++

In a previous post, I was speaking about a design problem I had with replication of the hexagonal coordinates of a token. My problem was that I was using a Tick event in Blueprint to compute the target position. Doing this computation each frame was not really efficient and it was not a really good design either. UE4 provides a way to implement a callback in C++ when replication happens, but at first it wasn’t working from me because the callback was never executed on the server. Well… Turned out it was normal because the replication was only happening on the non-authorities, duh !

So here is the new implementation. The solution to the problem is to explicitly call the function to compute the target move location when code is running on the server.

 

In addition, I also had to include a pointer of the board to get access to GetWorldLocationFromHexCoordinate(Coord) . Now in the Blueprint, I only need to use SetHexCoordinate to both trigger the replication of Coord and compute the TargetMoveLocation on every instances, including the authority. Furthermore, I changed Coord to be BlueprintReadOnly to be sure not to update the property without passing through the setter method.

 

The dev.

Leave a Reply

Your email address will not be published. Required fields are marked *