In the CollectibleObject class's constructor, we make sure that our object is going to be replicated. After that, we create a sphere collider that we tell (via a listener) to call the OnBeginOverlap function when it collides with another object. To do that, we use the OnComponentBeginOverlap function.
After this, inside our OnBeginOverlap function, we first check if we are currently on the server. We don't want things to get called multiple times, and we want the server to be the one that tells the other clients that we've increased our score.
We also call the UpdateScore function. This function has had the following function specifiers added to it:
- Reliable: The function will be replicated over the network and make it so that it is guaranteed to arrive, regardless of network errors or bandwidth issues. It requires us to select either Client or Server as an additional specifier.
- Server: Specifies that the function should only be called on the server. Adds an additional function that has _Implementation at the end of it, which is where the implementation should happen. The automatically generated code will use this function as needed.
- WithValidation: Adds an additional function that needs to be implemented with _Validate at the end. This function will take in the same parameters as the function given, but will return a bool that indicates whether the call to the main function should happen or not.
Calling UpdateScore will, in turn, call the UpdateScore_Implementation function that we created and it will display a message, saying that we've collected the object by printing out some text like we used earlier.
Finally, the UpdateScore_Validate function is required and just tells the game that we should always run the implementation for the UpdateScore function.