Hey!
Let me explain. This is an extremely simplified model of how we handle commands received from the network:
void Update() {
var commandsReceived = GetCommandsFromLastPacket();
foreach(var command in commandsReceived) {
CoherenceSync sync = FindReceiverForCommand(command);
ExecuteCommand(command, sync);
}
}
void ExecuteCommand(Command command, CoherenceSync sync) {
try {
sync.bakedCode.ExecuteCommand(command);
} catch (Exception e) {
Debug.LogError($"ClientCore: Exception in handler. Exception: {e}");
}
}
The sync.bakedCode.ExecuteCommand(command);
is where your ApplyDamage
function would be invoked.
As you can see, for every command received, we execute that command in a try/catch block. The reason for this is, if your command code throws an exception, we can still continue operating and handle other commands in the queue.
Your ApplyDamage
(simplified) looks as follows:
public void ApplyDamage(int _damage)
{
// ... some operations ...
hpController.DecreaseAmount(_damage, false);
cacher.sync.SendCommand<NetworkHP>(nameof(UpdateGUI),
Coherence.MessageTarget.Othe, hpController.guiRatio);
}
We could therefore present our code as:
void ExecuteCommand(Command command, CoherenceSync sync) {
try {
// Inlined `ApplyDamage` code
// ... some operations ...
hpController.DecreaseAmount(_damage, false);
cacher.sync.SendCommand<NetworkHP>(nameof(UpdateGUI),
Coherence.MessageTarget.Othe, hpController.guiRatio);
} catch (Exception e) {
Debug.LogError($"ClientCore: Exception in handler. Exception: {e}");
}
}
Your question is, why is the command from the last line of ApplyDamage
is not being sent. The reason for it is, your hpController.DecreaseAmount(_damage, false);
throws an exception, which is caught by our try/catch block.
Virtually it’s equivalent to:
void ExecuteCommand(Command command, CoherenceSync sync) {
try {
// Inlined `ApplyDamage` code
// ... some operations ...
throw null;
cacher.sync.SendCommand<NetworkHP>(nameof(UpdateGUI),
Coherence.MessageTarget.Othe, hpController.guiRatio);
} catch (Exception e) {
Debug.LogError($"ClientCore: Exception in handler. Exception: {e}");
}
}
The cacher.sync.SendCommand
can never execute because exception stops the execution of this functions and starts unwinding the stack.
You’ve also noticed that commenting out your UpdateHUD
code results in command being sent. That’s because you’re preventing the exception from happening, and the execution of ApplyDamage
can continue.
You can achieve the same by applying your own try/catch block in the ApplyDamage
:
public void ApplyDamage(int _damage)
{
// ... some operations ...
try {
hpController.DecreaseAmount(_damage, false);
catch (Exception e) {
Debug.LogError($"Exception in hpController.DecreaseAmount: {e}");
}
cacher.sync.SendCommand<NetworkHP>(nameof(UpdateGUI),
Coherence.MessageTarget.Othe, hpController.guiRatio);
}
There’s nothing that we can do to fix that problem. You need to find out why your code is throwing an exception, which is derailing the execution of ApplyDamage
.
Hope that helps!