Networking /
: Android
Mac
- *ARDK version 2.3
- *Unity version 2020.3.36f1:
I’m trying to use the HLAPI Pong example as a base. I’ve rewritten part to use raycasts to destroy the ball and am trying to update the score depending on who destroys the ball. Since the score should only be updated by the host. I assume a message has to be passed when the peer destroys the ball. In reading through the HLAPI documentation and trying to replicate the other messages in the controller code I’ve added :
private MessageStreamReplicator _whoScored;
and written the following in the OnDidConnect :
_whoScored = new MessageStreamReplicator<string>("whoscored", authToObserverDescriptor, group);
_whoScored.MessageReceived += (args) =>
{
Debug.Log("peer scored");
gameController.GoalScored(args.Message);
};
and am calling it with:
public void SetScore()
{
if (_isHost)
{
Debug.Log("host Scored");
GoalScored("red");
}
else
{
Debug.Log("peer scored");
_whoScored.SendMessage("blue", _auth.PeerOfRole(Role.Authority));
}
}
But the intended result isn’t happening. Only the host score of red is getting incremented. Am I sending the message properly?
Hi @Andrew_R_Towl,
It’s tough to determine what the issue could be without seeing more of the code that interacts with your intended feature. However, I did notice the “blue” received message is calling the GoalScored()
method – with parameter args.Message
– on a gameController
instance, while the “red” event is simply calling the GoalScored()
method that belongs to the same class instance. Is it possible that this could be the source of the issue? Does the issue persist if simply calling GoalScored(args.Message);
during the message received event?
Hi Rob,
Thanks for the reply. I tried changing that but no luck. A little more info; I’m using the HLAPI game controller and trying to send a message from the peer when the peer destroys the ball. Below is the method I’m trying to send a message to since only the host should call it.
// Reset the ball when a goal is scored, increase score for player that scored
// Only the host should call this method
public void GoalScored(string color)
{
// color param is the color of the goal that the ball went into
// we score points by getting the ball in our opponent’s goal
if (color == "red")
{
Debug.Log
(
"Point scored for team blue. " +
"Setting score via HLAPI. Only host will receive this log entry."
);
//gets the balls component and network destroys it
// _ball.GetComponent<NetworkedUnityObject>().NetworkDestroy();
_blueScore += 1;
}
if (color == "blue")
{
Debug.Log
(
"Point scored for team red. " +
"Setting score via HLAPI. Only host will receive this log entry."
);
_redScore += 1;
}
_scoreText.Value = string.Format("Score: {0} - {1}", _redScore, _blueScore);
}
Setscore gets called when a ball is destroyed on tap with a raycast. When it’s the host, it works just fine and updates the score. I thought what I wrote would send a message from the peer to the host with the message of “blue” so the host can update the score but I’m not sure if I set up the message correctly.
Got it sorted. I was sending the wrong type of message.
changed:
_whoScored = new MessageStreamReplicator(“whoscored”, authToObserverDescriptor, group);
to:
_whoScored = new MessageStreamReplicator<string(“whoscored”,_arNetworking.Networking.AnyToAnyDescriptor(TransportType.ReliableOrdered);
and it works now.
Glad you were able to determine the root of the issue! Let us know if you run into problems again in the future.