Hello there! I have (another :D) a question regarding dots. I have this quite simple script where using jobs and native containers i activate and load chunks based on location, transfer them to one nativelist, then from that one into another, but when i test the .Length of the lists, the first one works fine, but when i change the coordinates at run time, instead of being the same number (adding the new chunks, removing the old) it adds the new ones to the old ones and then lower the total number by 1 every frame. Heres my entire script:
NOTE: In the code I have added if function with two different float2 positions and debug.log with the finalised nativelists for the purposes of testing/debugging. Also i will turn this monobehavior into component system in the fututre, but i am using mono for easier testing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Entities;
using Unity.Jobs;
using Unity.Burst;
using static WorldUtils;
public class WorldMaster : MonoBehaviour
{
public float2 Position;
public int ChunkSize = 20;
public int LoadDistance = 40;
NativeList<int2> ActiveChunks;
NativeList<int2> LoadedChunks;
private void Start()
{
ActiveChunks = new NativeList<int2>(0, Allocator.Persistent);
LoadedChunks = new NativeList<int2>(0, Allocator.Persistent);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.G))
{
Position = new float2(500, 500);
}else if (Input.GetKeyDown(KeyCode.H))
{
Position = new float2(0, 0);
}
ActiveChunks.Clear();
JobHandle activate = ActivateChunks(Position, ChunkSize, LoadDistance, ActiveChunks);
activate.Complete();
JobHandle load = LoadChunks(ActiveChunks, LoadedChunks);
load.Complete();
Debug.Log(ActiveChunks.Length + " " + LoadedChunks.Length);
}
public JobHandle ActivateChunks(float2 Position, int ChunkSize, int LoadDistance, NativeList<int2> ActiveChunks)
{
ActivateChunksJob job = new ActivateChunksJob();
job.Position = Position;
job.ChunkSize = ChunkSize;
job.LoadDistance = LoadDistance;
job.ActiveChunks = ActiveChunks;
return job.Schedule();
}
public JobHandle LoadChunks(NativeList<int2> ActiveChunks, NativeList<int2> LoadedChunks)
{
LoadChunksJob job = new LoadChunksJob();
job.ActiveChunks = ActiveChunks;
job.LoadedChunks = LoadedChunks;
return job.Schedule();
}
}
[BurstCompile]
public struct ActivateChunksJob : IJob
{
public float2 Position;
public int ChunkSize;
public int LoadDistance;
int2 chunk;
public NativeList<int2> ActiveChunks;
public void Execute()
{
for (int x = 0; x < LoadDistance; x++)
{
for (int y = 0; y < LoadDistance; y++)
{
chunk = GetChunkFromWorld(Position, ChunkSize, new float2(0, 0)) - LoadDistance / 2 + new int2(x, y);
if (!ActiveChunks.Contains(chunk))
{
ActiveChunks.Add(chunk);
}
}
}
}
}
[BurstCompile]
public struct LoadChunksJob : IJob
{
public NativeList<int2> ActiveChunks;
public NativeList<int2> LoadedChunks;
public void Execute()
{
foreach (int2 chunk in ActiveChunks)
{
if (!LoadedChunks.Contains(chunk))
{
LoadedChunks.Add(chunk);
}
}
foreach (int2 chunk in LoadedChunks)
{
if (!ActiveChunks.Contains(chunk))
{
LoadedChunks.RemoveAt(LoadedChunks.IndexOf(chunk));
}
}
}
}
I am using my own tiny library of code (thats that using static WorldUtils), from which i am using only one function in this code: GetChunkFromWorld. I am putting the function here as well:
public static int2 GetChunkFromWorld(float2 WorldPosition, int ChunkSize, float2 OriginPosition)
{
int2 ChunkPosition;
ChunkPosition.x = (int)(math.floor((WorldPosition.x - OriginPosition.x) / ChunkSize));
ChunkPosition.y = (int)(math.floor((WorldPosition.y - OriginPosition.y) / ChunkSize));
return ChunkPosition;
}
So, if you would help me debug this code, i would be very happy. Also sorry for spamming the forum. Many thanks