Improbable Icon

Any way to debug a MonoBehaviour script running on the UnityGameLogic worker?

unity

#1

So I was following the Health pick-up tutorial and I added the HealthPickupServerBehaviour. Everything looked fine except the pick-up didn’t consume. I tried a couple of things:

  1. Debugging with Visual Studio. It can attach to the Unity editor process, and enter the breakpoint when my player hits the pick-up, but the healthPickupWriter and healthComponentRequestSender are both null. I assume it’s because the UnityClient worker doesn’t have access to them thus they are not injected, which is on purpose. But how can I attach to the UnityGameLogic worker?

  2. Using log in OnTriggerEnter() callback. The log indicates it’s from the UnityClient worker, so where can I find the log from the UnityGameLogic worker?


#3

Hey @IndieST, based on what you’ve described, which prefab did you put the HealthPickupServerBehaviour on?

Since the healthPickupWriter and healthComponentRequestSender are both null and you are getting a log from the UnityClient worker makes me think its on the UnityClient version of the prefab rather than the UnityGameLogic version of the prefab as described here.

Can you double check this?

  • Jamie

#4

Hi Jamie, you are right! The HealthPickupServerBehaviour was put on the prefab in the UnityClient folder. After I moved it to the prefab in the UnityGameLogic folder, everything works fine! And I can debug on the UnityGameLogic worker too. Thanks a lot!

The tutorial was not very specific about how to read the health from the colliding player in this section. I guess I’ll have to create a MonoBehaviour script like this:
[WorkerType(WorkerUtils.UnityGameLogic)]
public class ReadPlayerHealthBehaviour : MonoBehaviour
{
[Require]private HealthComponent.Requirable.Reader healthComponentReader;
public float Health => healthComponentReader.Data.Health;
public float MaxHealth => healthComponentReader.Data.MaxHealth;
}

And then attach it to the Player prefab, so I can read the properties from colliding player.GetComponent()?

I found another way to do that with less code:
var healthComponents = playerSpatialOsComponent.Worker.GetComponentDataFromEntity<HealthComponent.Component>(true);
var healthData = healthComponents[playerSpatialOsComponent.Entity];

It works for me. But I don’t know which way is more “standard”?


#5

Hey @IndieST!

Getting the list of every HealthComponent on a worker and then finding the data for your player entity within that list is a more round-about way of reading the data, especially given that you already have a reference to the Player GameObject.

The GetComponentDataFromEntity function is also more of an ECS concept, so I would prefer using the Reader/Writer API for MonoBehaviours by adding the ReadPlayerHealthBehaviour script to your Player and then calling player.GetComponent<ReadPlayerHealthBehaviour>() in the collision handling code.

It’s also means that now other scripts can also get the health data of a player - if you so wish to extend your project that way.

Hope that helps, let me know if you have more questions!

Cheers,
Paul