I am using Unity 2018.3.14. I am trying to create an IJob
.
Here is how I call the new job:
var Job = new IncomingDataTCPJob
{
worldServer = this,
data = sdata
};
JobHandle handle = Job.Schedule();
handle.Complete();
sdata
is string, worldServer
is instance of class.
Here is my declared struct
:
public struct IncomingDataTCPJob : IJob
{
public string data;
public ClientWorldServer worldServer;
public void Execute()
{
worldServer.OnIncomingData(data);
}
}
When I try to run I got this error:
InvalidOperationException: IncomingDataTCPJob.data is not a value type. Job structs may not contain any reference types.
Any idea why I get this error? I am really very new to Unity's Job system.
Jobs cannot receive data that is not blittable.
According to the Jobs Safety documentation:
The way the C# Job System copies data means that a job can only access blittable data types.
This is done to avoid race conditions like parallel threads trying to access the same memory reference.
I do not know why you're getting an error on your "string data" field, but it is likely a red herring. Unity's job system cannot interact with reference type elements. Aside from the TransformAccessArray (or other specialized classes), the jobs system can only be used to parallel process raw data. You cannot pass the custom "WorldClientServer" class to your job struct because it is not primitive (and therefore not blittable). You will need to convert everything to raw data prior to sending it to the job and convert it back to its referenced object in order to leverage the Jobs system.
The recommended way to get data back from your job is to use a NativeQueue or other NativeContainer (which are threadsafe). The Jobs system is much more restricted than standard threading, but protects you as a developer from causing nightmare-level race condition bugs and memory leaks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With