======================== CODE SNIPPETS ======================== TITLE: Applying Operators to Vectors in Unity Mathematics C# DESCRIPTION: Illustrates how standard C# operators (+, -, ==, >) are applied component-wise to vectors in Unity Mathematics. It shows examples of vector addition, negation, equality comparison (both single boolean and component-wise boolean vector), and greater-than comparison. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/cheatsheet/mathematics.md#_snippet_2 LANGUAGE: c# CODE: ``` int2 a = new int2(1, 2); int2 b = new int2(3, 4); // Addition. int2 c = a + b; // new int2(a.x + b.x, a.y + b.y) // Negation. c = -a; // new int2(-a.x, -a.y) // Equality. bool myBool = a.Equals(b); // a.x == b.x && a.y == b.y bool2 myBool2 = a == b; // new int2(a.x == b.x, a.y == b.y) // Greater than. myBool2 = a > b; // new bool2(a.x > b.x, a.y > b.y) ``` ---------------------------------------- TITLE: Implementing Custom CollectComponents Function for Ghost Prefab (C#) DESCRIPTION: Provides an example implementation of the `CollectComponents` function pointer used with `GhostPrefabCreation.ConvertToGhostPrefab`. This function allows defining the exact order and list of components to be serialized for a ghost archetype, ensuring consistent indexing into the `componentIndices` list for custom serializers. It populates `componentTypes` and `componentCount` lists for root and child entities. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/NetcodeSamples/Assets/Samples/CustomSerializer/CustomSerializer.md#_snippet_2 LANGUAGE: C# CODE: ``` public static void CollectComponents(IntPtr componentTypesPtr, IntPtr componentCountPtr) { ref var componentTypes = ref GhostComponentSerializer.TypeCast>(componentTypesPtr); ref var componentCount = ref GhostComponentSerializer.TypeCast>(componentCountPtr); //Root componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentCount[0] = 13; //Child 1 componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentCount[1] = 3; //Child 2 componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentTypes.Add(ComponentType.ReadWrite()); componentCount[2] = 3; } ``` ---------------------------------------- TITLE: Serializing Snapshot Data to Stream (Unity NetCode C#) DESCRIPTION: After copying data to the snapshot buffer, serialize it entity by entity into the data stream using generated serializer methods like `SerializeSingleBaseline` or `SerializeThreeBaseline`. This example shows how to call these methods within the custom serializer. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/NetcodeSamples/Assets/Samples/CustomSerializer/CustomSerializer.md#_snippet_4 LANGUAGE: csharp CODE: ``` //With a single baseline compBitSize[0*compBitSizeStride] = default(Unity.NetCode.Generated.GhostOwnerGhostComponentSerializer).SerializeComponentThreeBaseline(snapshotData,baseline0Ptr, baseline1Ptr, baseline2Ptr, changeMaskData, ref changeMaskOffset, ref snapshotOffset, ref predictor, ref writer, compressionModel); //With three baseline compBitSize[0*compBitSizeStride] = default(Unity.NetCode.Generated.GhostOwnerGhostComponentSerializer).SerializeComponentThreeBaseline(snapshotData,baseline0Ptr, baseline1Ptr, baseline2Ptr, changeMaskData, ref changeMaskOffset, ref snapshotOffset, ref predictor, ref writer, compressionModel); ``` ---------------------------------------- TITLE: Scheduling and Completing a Unity Job in C# DESCRIPTION: Demonstrates how to instantiate a job struct, populate its fields with NativeArrays, schedule it using the Schedule() method to put it on the job queue, and wait for its completion using the Complete() method on the returned JobHandle. This moves the computation off the main thread. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/Dots101/Jobs101/Assets/TargetsAndSeekers/README.md#_snippet_1 LANGUAGE: C# CODE: ``` // To schedule a job, we first create an instance and populate its fields. FindNearestJob findJob = new FindNearestJob { TargetPositions = TargetPositions, SeekerPositions = SeekerPositions, NearestTargetPositions = NearestTargetPositions, }; // Schedule() puts the job instance on the job queue. JobHandle findHandle = findJob.Schedule(); // The Complete() method will not return until the job // represented by the handle has finished execution. // In some cases, a job may have finished // execution before Complete() is called on its handle. // Either way, the Complete() call will only return // once the job is done. findHandle.Complete(); ``` ---------------------------------------- TITLE: Creating and Copying Matrices in Unity Mathematics DESCRIPTION: This snippet demonstrates various ways to create matrices, including using identity matrices, explicit component initialization, and diagonal matrices, within the Unity Mathematics library. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/cheatsheet/mathematics.src.md#_snippet_2 LANGUAGE: C# CODE: ``` float4x4 m1 = float4x4.identity; float4x4 m2 = new float4x4( new float4(1f, 0f, 0f, 0f), new float4(0f, 1f, 0f, 0f), new float4(0f, 0f, 1f, 0f), new float4(0f, 0f, 0f, 1f) ); double3x3 dm = new double3x3(1.0); // Diagonal matrix ``` ---------------------------------------- TITLE: Creating and Copying Matrices in Unity Mathematics C# DESCRIPTION: Shows how to create and initialize matrices (int2x3) in Unity Mathematics using row-major order values, initializing with a single scalar, initializing with column vectors, accessing columns using .c0, .c2, etc., and casting between matrix types. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/cheatsheet/mathematics.md#_snippet_1 LANGUAGE: c# CODE: ``` // Values in row-major order. int2x3 m = new int2x3(1, 2, 3, 4, 5, 6); // first row: 1, 2, 3 // second row: 4, 5, 6 // First column: new int2(1, 4) int2 i2 = m.c0; // Third column: new int2(3, 6) i2 = m.c2; // new int2x3(100, 100, 100, 100, 100, 100) m = new int2x3(100); m = new int2x3( new int2(1, 2), // column 0 new int2(3, 4), // column 1 new int2(5, 6)); // column 2 // Converts each int component to a float. float2x3 m2 = new float2x3(m); ``` ---------------------------------------- TITLE: Creating and Copying Vectors in Unity Mathematics DESCRIPTION: This snippet shows how to instantiate vectors of different types (e.g., float3, int2) and how to perform a simple copy operation between vectors using the Unity Mathematics library. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/cheatsheet/mathematics.src.md#_snippet_1 LANGUAGE: C# CODE: ``` float3 v1 = new float3(1f, 2f, 3f); float3 v2 = v1; // Copying int2 iv = new int2(5, 10); ``` ---------------------------------------- TITLE: Creating and Copying Vectors in Unity Mathematics C# DESCRIPTION: Demonstrates various ways to create and initialize vectors (int4, int2) in Unity Mathematics, including direct initialization, using zero, indexing components, swizzling (rearranging components), combining lower-dimension vectors and scalars, and casting from other types. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/cheatsheet/mathematics.md#_snippet_0 LANGUAGE: c# CODE: ``` int4 i4 = new int4(1, 2, 3, 4); // x, y, z, w int2 i2 = int2.zero; // new int2(0, 0); // Index the components like an array. int i = i4[2]; // int i = i4.z i4[0] = 9; // i4.x = 9 // Creating a vector by copying combinations // of values from another vector (swizzling). i4 = i4.xwyy; // new int4(i4.x, i4.w, i4.y, i4.y); i2 = i4.yz; // new int2(i4.y, i4.z); i4 = i2.yxyx; // new int4(i2.y, i2.x, i2.y, i2.x); // Creating a vector from combinations of // lower-dimension vectors and scalars. i4 = new int4(1, i2, 3); // new int4(1, i2.x, i2.y, 3); i4 = new int4(i2, i2); // new int4(i2.x, i2.y, i2.x, i2.y); i4 = new int4(7); // new int4(7, 7, 7, 7); i2 = new int2(7.5f); // new int2((int) 7.5f, (int) 7.5f); // Creating a vector by casting. i4 = (int4) 7; // new int4(7, 7, 7, 7); i2 = (int2) 7.5f; // new int2((int) 7.5f, (int) 7.5f); ``` ---------------------------------------- TITLE: Scheduling Parallel Jobs in Unity Entities DESCRIPTION: This snippet demonstrates the standard way to schedule an IJobEntity job using ScheduleParallel() and how source generation modifies this call to correctly handle job dependencies by passing and returning the system's dependency handle. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/Dots101/Entities101/Assets/Kickball/README.md#_snippet_0 LANGUAGE: C# CODE: ``` myJob.ScheduleParallel(); state.Dependency = myJob.ScheduleParallel(state.Dependency); // result of source-gen ``` ---------------------------------------- TITLE: Scheduling Dependent Jobs for Sorting and Finding Nearest Targets in Unity ECS C# DESCRIPTION: This C# code snippet demonstrates how to schedule a SortJob to sort target positions and a FindNearestJob to search for the nearest target. It shows how to establish a dependency by passing the sort job's handle when scheduling the find job, ensuring the sorting completes before the search begins. The code then calls Complete() on the find job's handle to wait for both jobs to finish. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/Dots101/Jobs101/Assets/TargetsAndSeekers/README.md#_snippet_4 LANGUAGE: C# CODE: ``` SortJob sortJob = TargetPositions.SortJob( new AxisXComparer { }); FindNearestJob findJob = new FindNearestJob { TargetPositions = TargetPositions, SeekerPositions = SeekerPositions, NearestTargetPositions = NearestTargetPositions, }; JobHandle sortHandle = sortJob.Schedule(); // To make the find job depend upon the sorting jobs, // we pass the sort job handle when we schedule the find job. JobHandle findHandle = findJob.Schedule( SeekerPositions.Length, 100, sortHandle); // Completing a job also completes all of its // dependencies, so completing the find job also // completes the sort jobs. findHandle.Complete(); ``` ---------------------------------------- TITLE: Defining Level Sync State Component (C#) DESCRIPTION: This struct holds the necessary data to manage the networked level synchronization process. It includes the current state of the sync, the ID of the current level, and the ID of the next level to load. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/NetcodeSamples/Assets/Samples/LevelSync/README.md#_snippet_0 LANGUAGE: C# CODE: ``` public struct LevelSyncStateComponent : IComponentData { public LevelSyncState State; public int CurrentLevel; public int NextLevel; } ``` ---------------------------------------- TITLE: Scheduling Parallel Job (IJobParallelFor.Schedule) - C# DESCRIPTION: Schedules an IJobParallelFor job for execution. The Schedule method takes the total number of indices to process (SeekerPositions.Length) and a batch size (100). The job system splits the work into batches of this size, which can then be processed concurrently by worker threads. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/Dots101/Jobs101/Assets/TargetsAndSeekers/README.md#_snippet_3 LANGUAGE: C# CODE: ``` // This job processes every seeker, so the // seeker array length is used as the index count. // A batch size of 100 is semi-arbitrarily chosen here // simply because it's not too big but not too small. JobHandle findHandle = findJob.Schedule(SeekerPositions.Length, 100); ``` ---------------------------------------- TITLE: Implementing Custom Bootstrap and Checking Status in C# DESCRIPTION: This C# snippet shows how to implement a custom `ICustomBootstrap` by inheriting from `ClientServerBootstrap`. It demonstrates how to check if automatic bootstrapping is enabled using `DetermineIfBootstrappingEnabled()` before proceeding with world initialization, respecting project-wide or per-scene overrides. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/NetcodeSamples/Assets/Samples/HelloNetcode/1_Basics/01c_DisableBootstrap/DisableBootstrap.md#_snippet_0 LANGUAGE: csharp CODE: ``` // The preserve attribute is required to make sure the bootstrap is not stripped in il2cpp builds with stripping enabled. [UnityEngine.Scripting.Preserve] // The bootstrap needs to extend `ClientServerBootstrap`, there can only be one class extending it in the project. public class MyGameCustomBootstrap : ClientServerBootstrap { // The initialize method is what Entities calls to create the default worlds. public override bool Initialize(string defaultWorldName) { // If the user added an `OverrideDefaultNetcodeBootstrap` MonoBehaviour to their active scene, // or disabled Bootstrapping project-wide via a `NetcodeConfig.Global`, we should respect that here. if (!DetermineIfBootstrappingEnabled()) return false; ... } } ``` ---------------------------------------- TITLE: Using Vector and Matrix Operators in Unity Mathematics DESCRIPTION: This snippet illustrates the use of common arithmetic and comparison operators (+, -, *, /, ==) with vectors and matrices. Note that arithmetic operators perform component-wise operations, while matrix multiplication requires the math.mul function. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/cheatsheet/mathematics.src.md#_snippet_3 LANGUAGE: C# CODE: ``` float3 a = new float3(1f, 2f, 3f); float3 b = new float3(4f, 5f, 6f); float3 sum = a + b; float3 diff = a - b; float3 product = a * b; // Component-wise float3 division = a / b; // Component-wise float4x4 matA = float4x4.identity; float4x4 matB = float4x4.Scale(2f); float4x4 matSum = matA + matB; float4x4 matProduct = math.mul(matA, matB); // Matrix multiplication bool3 comparison = a == b; ``` ---------------------------------------- TITLE: Copying Component Data to Snapshot (Unity NetCode C#) DESCRIPTION: Implement the `CustomChunkSerializer.CopyComponentsToSnapshot` method by copying enable bits and component data using generated serializers and helper methods. This prepares the data in a snapshot buffer for serialization. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/NetcodeSamples/Assets/Samples/CustomSerializer/CustomSerializer.md#_snippet_3 LANGUAGE: csharp CODE: ``` new Unity.NetCode.Generated.GhostOwnerGhostComponentSerializer().CopyComponentToSnapshot(chunk, ref context, ghostChunkComponentTypesPtr, indices[0], snapshotPtr, ref snapshotOffset); new Unity.NetCode.Generated.TransformDefaultVariantGhostComponentSerializer().CopyComponentToSnapshot(chunk, ref context, ghostChunkComponentTypesPtr,indices[1], snapshotPtr, ref snapshotOffset); CustomGhostSerializerHelpers.CopyEnableBits(chunk, context.startIndex, context.endIndex, context.snapshotStride, ref ghostChunkComponentTypesPtr[indices[2].ComponentIndex], enableBits, ref maskOffset); ``` ---------------------------------------- TITLE: Calculating Absolute Value of Vector using math.abs (C#) DESCRIPTION: Demonstrates how to use the `math.abs` method from the Unity.Mathematics library to calculate the absolute value of each component in an `int3` vector. This method supports vector arguments, returning a new vector with the absolute values. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/cheatsheet/mathematics.src.md#_snippet_0 LANGUAGE: C# CODE: ``` math.abs(new int3(5, -7, -1)) ``` ---------------------------------------- TITLE: Generating Random Numbers with Unity.Mathematics.Random in C# DESCRIPTION: This snippet demonstrates how to initialize a Random struct with a seed and generate various types of random values, including integers within a range, float vectors within a range, unit-length direction vectors, and unit-length quaternions. It also shows how to create multiple independent Random instances using CreateFromIndex to ensure distinct random streams. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/cheatsheet/mathematics.md#_snippet_3 LANGUAGE: c# CODE: ``` Random rand = new Random(123); // seed of 123 // [-2147483647, 2147483647] int integer = rand.NextInt(); // [25, 100) integer = rand.NextInt(25, 100); // x is [0, 1), y is [0, 1) float2 f2 = rand.NextFloat2(); // x is [0, 7.5), y is [0, 11) f2 = rand.NextFloat2(new float2(7.5f, 11f)); // x is [2, 7.5), y is [-4.6, 11) f2 = rand.NextFloat2(new float2(2f, -4.6f) , new float2(7.5f, 11f)); // Uniformly random unit-length direction vector. double3 d3 = rand.NextDouble3Direction(); // Uniformly random unit-length quaternion. quaternion q = rand.NextQuaternionRotation(); // Create multiple Random number generators using an incremented seed. NativeArray rngs = new NativeArray(10, Allocator.Temp); for (int i = 0; i < 10; i++) { // Unlike the Random constructor, CreateFromIndex hashes the seed. // If we were to pass incremented seeds to the constructor, // each RNG would produce a similar stream of random numbers // as each other. Because we instead here use CreateFromIndex, // the RNG's will each produce streams of random numbers that // are properly distinct and unrelated to the others. rngs[i] = Random.CreateFromIndex((uint)(i + 123)); } ``` ---------------------------------------- TITLE: Defining FindNearestJob Struct in C# DESCRIPTION: Defines the data structure for the FindNearestJob, holding NativeArrays for seeker positions, target positions, and the resulting nearest target positions. Data must be included in the job's fields and stored in unmanaged collections like NativeArray for Burst compilation and job execution. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/Dots101/Jobs101/Assets/TargetsAndSeekers/README.md#_snippet_0 LANGUAGE: C# CODE: ``` public struct FindNearestJob : IJob { // All the data which a job will access // must be included in its fields. public NativeArray TargetPositions; public NativeArray SeekerPositions; public NativeArray NearestTargetPositions; public void Execute() { // ... the code which the job will run goes here } } ``` ---------------------------------------- TITLE: Scheduling an IJobParallelFor Job (C#) DESCRIPTION: Demonstrates how to schedule an `IJobParallelFor` job instance. It initializes the job with a `NativeArray` and calls the `Schedule` method, specifying the total number of elements to process (`count`) and the `batch size` which determines how the work is divided among threads. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/jobs.md#_snippet_1 LANGUAGE: csharp CODE: ``` // ... scheduling the job var job = new SquareNumbersJob { Nums = myArray }; JobHandle handle = job.Schedule( myArray.Length, // count 100); // batch size ``` ---------------------------------------- TITLE: Registering Custom Ghost Chunk Serializers in Unity Netcode ECS (C#) DESCRIPTION: This snippet demonstrates how to register custom chunk serialization functions with the GhostCollection system. It retrieves the GhostCollectionCustomSerializers singleton and adds an entry mapping a ghost type hash to a GhostPrefabCustomSerializer instance containing the custom SerializeChunk and PreSerializeChunk functions. This registration must occur before prefabs are collected. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/NetcodeSamples/Assets/Samples/CustomSerializer/CustomSerializer.md#_snippet_0 LANGUAGE: csharp CODE: ``` GhostPrefabCreation.ConvertToGhostPrefab(EntityManager, prefab, prefabConfig); var hash = (Unity.Entities.Hash128)EntityManager.GetComponentData(prefab); var customSerializers = SystemAPI.GetSingletonRW(); customSerializers.ValueRW.Serializers.Add(hash, new GhostPrefabCustomSerializer { SerializeChunk = CustomChunkSerializer.SerializerFunc, PreSerializeChunk = CustomChunkSerializer.PreSerializerFunc }); ``` ---------------------------------------- TITLE: Defining Level Sync States Enum (C#) DESCRIPTION: This enum lists the distinct states that the level synchronization process can be in. These states control the flow, indicating whether the system is idle, requesting a load, in progress of loading, or finished loading. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/NetcodeSamples/Assets/Samples/LevelSync/README.md#_snippet_1 LANGUAGE: C# CODE: ``` public enum LevelSyncState { Idle, LevelLoadRequest, LevelLoadInProgress, LevelLoaded } ``` ---------------------------------------- TITLE: Defining a Parallel Job with IJobParallelFor (C#) DESCRIPTION: Defines a job struct `SquareNumbersJob` that implements `IJobParallelFor`. This job processes elements of a `NativeArray` in parallel, squaring each element at a given index. The `Execute` method is called for each index assigned to the job's batch. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/jobs.md#_snippet_0 LANGUAGE: csharp CODE: ``` [BurstCompile] public struct SquareNumbersJob : IJobParallelFor { public NativeArray Nums; // Each Execute call processes only a single index. public void Execute(int index) { Nums[index] *= Nums[index]; } } ``` ---------------------------------------- TITLE: Defining Parallel Job Struct (IJobParallelFor) - C# DESCRIPTION: Defines the FindNearestJob struct implementing IJobParallelFor for parallel processing. It holds input (TargetPositions, SeekerPositions) and output (NearestTargetPositions) data in NativeArrays. The Execute method is called by the job system for each index, allowing concurrent processing of array elements across multiple threads. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/Dots101/Jobs101/Assets/TargetsAndSeekers/README.md#_snippet_2 LANGUAGE: C# CODE: ``` public struct FindNearestJob : IJobParallelFor { [ReadOnly] public NativeArray TargetPositions; [ReadOnly] public NativeArray SeekerPositions; public NativeArray NearestTargetPositions; // Each Execute call processes only an individual index. public void Execute(int index) { // ... } } ``` ---------------------------------------- TITLE: Accessing Component Type Handle for Ghost Serialization (C#) DESCRIPTION: Demonstrates how to retrieve the `DynamicComponentTypeHandle` for a component during ghost serialization by using indices obtained from the ghost collection data structures like `GhostCollectionPrefabSerializer`, `GhostCollectionComponentIndex`, and `DynamicTypeList`. This is necessary to access component data from a chunk. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/NetcodeSamples/Assets/Samples/CustomSerializer/CustomSerializer.md#_snippet_1 LANGUAGE: C# CODE: ``` var typeData = ghostCollectionPrefabSerializer[ghostType]; var componentIndex = typeData.FirstComponent; var index = componentIndices[componentIndex]; var dynamicTypeHandle = dynamicTypeList[index.ComponentIndex]; ``` ---------------------------------------- TITLE: Comparing Unity ECS Version Numbers in C# DESCRIPTION: This C# snippet demonstrates the correct method for comparing two 32-bit signed integer version numbers (VersionA and VersionB) in Unity ECS. Due to the nature of signed integer overflow, subtracting the older version from the newer one and checking if the result is positive is the reliable way to determine if VersionB is more recent than VersionA. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/additional-entities-features.md#_snippet_0 LANGUAGE: C# CODE: ``` // true if VersionB is more recent than VersionA // false if VersionB is equal or less than VersionA bool changed = (VersionB - VersionA) > 0; ``` ---------------------------------------- TITLE: Demonstrating IJobParallelFor Safety Check Exception (C#) DESCRIPTION: Illustrates a safety check restriction in `IJobParallelFor` jobs. Accessing array elements using an index other than the `index` parameter passed to `Execute` (e.g., `Nums[0]`) will trigger a safety check exception to prevent race conditions and ensure data integrity within parallel batches. SOURCE: https://github.com/unity-technologies/entitycomponentsystemsamples/blob/master/EntitiesSamples/Docs/jobs.md#_snippet_2 LANGUAGE: csharp CODE: ``` [BurstCompile] public struct MyJob : IJobParallelFor { public NativeArray Nums; public void Execute(int index) { // The expression Nums[0] triggers a safety check exception! Nums[index] = Nums[0]; } } ```