I have a huge dictionary, where the key is a Vector3I, and the values are chunk node (for a procedural generation system). I was wondering if the Where function is efficent enough to use to filter out the chunks in the render distance (i have a function for that) so i dont iterate trough hundreds of chunks, from which most are outside the render distance and pointless to iterate trough. Heres my code
private Godot.Collections.Dictionary<Vector3I, ChunkData> chunkData = new();
private Godot.Collections.Dictionary<Vector3I, ChunkNode> chunkNodes = new();
foreach (Vector3I cc in chunkData.Keys.Where(isWithinBounds))
{
if (chunkNodes.ContainsKey(cc)) continue;
// create the chunk node
ChunkNode chunkInstance = new ChunkNode();
// create the chunk with the proper data
chunkInstance.Initialize(chunkData[cc]);
chunkInstance.Position = CCToWorld(cc);
// add it to the scene
AddChild(chunkInstance);
// save it in the chunk nodes to be updated and deleted later
chunkNodes.Add(cc, chunkInstance);
}
first: this isn’t a dictionary. you are iterating Keys, which is an Array. but this is C# so I don’t know, your IDE would tell you.
second: Array doesn’t have a Where() method. the only coincidence is a link to Enumerable.Where. so this is a C# thing? and not a godot thing?
third: I don’t know what isWithinBounds does. what are you trying to do?
if you need to iterate over nodes within a range, this should be an array. it would be massive, but the advantage is it’s better for things like calculating distance.
only worry about performance when it starts to matter. specially in this case where you are dealing with algorithms. computers are fast.
that said, this would not work for mobile or low end devices. you need an actual voxel system, this is a very inefficient way of doing it because you have a node for each block when they should instead be data and only a few should be entities.
godot has a voxel fork that is updated alongside the main engine code. use it if you are making minecraft or space engineers:
1, Indeed, the Where function is not in the Dictionary class. Its in the Godot.Collection.Array. It is part of the System.Linq.Enumerable class. (look it up). Also ides dont have anything to do with anything dude. If i wanted to i couldve used notepad or vim, and it wouldve been itentical.
Array DOES have a Where function. I dont know where you searched for it, but it does.
isWithinBounds is a function, that takes in a Vector3I in chunk coordinates, and returns true if the coordinate is within the render distance. The Where function takes in a predicate (function that returns a bool), that takes as arguments the data in the Array. What i wrote is shorthand for the lambda `Where((cc) => isWithinBounds(cc)). If you are familiar with JS you should know what these are.
In my testing i found that the function does help with performance, since i dont have to iterate trough the entire Dictionary, testing key by key if it is within bounds. Most of the chunks,are outside the render distance, so i shouldnt iterate trough them at all if i can help it. If you are familiar with SQL you should know that there is a WHERE function, that filters the elements of the table.
The project is still verry much in the testing phase, so ofc not everything is super optimized. This is just to see that it works efficently enough. I did do a optimisation where the ChunkData class is replaced by a single uint, to reduce on ram and cpu usage. (Dont worry about why a uint.)
If i can help it, i dont want to use pre made assets, or AI for that matter. To truly understand the code, I personally need to have wrote each line, to understand its value and need in the code. Plus, these “Chunks” are not going to stay as chunks for much longer. (Dont worry about it.)
My question still stands. Is the Where function efficent to use? bc when i asked the AI, it told me that using a Linq function is highly inefficent, but not using it causes the processing time to double with the key count.
this takes us to the C# API differences, and from there to C# array which is collections:
apparently your Where() method is a C# equivalent of the filter() method in godot:
in godot it calls a function for each element of the array and returns a new Array.
it feels inefficient, though C# has some tricks during compilation, so IDK.
you are still creating a new array and calling a function for each element, so maybe a simple expression would be better?
foreach (Vector3I cc in chunkData.Keys)
{
if (isWithinBounds(cc))
{
//etc
}
}
yeah, I don’t use AI, you are on a good path with that mentality.
godot is a highly optimized program that keeps getting more optimized with every release, so using the tools provided is always a good idea, but it depends on the case.
for this it sounds like you need to process a huge amount of data, so you need as much performance as possible, but using C# just like gdscript can have limitations compared to raw C++. that said, I haven’t ran into problems that I could not work around in gdscript, and I made some very complex algorithms like procedural generation, pathfinding and AI and there was no performance impact.
you won’t see a problem with iterating through a list in 2025, if there is it will come from spawning and deleting nodes and things like that. again, computers are fast.
as for the AI, I don’t thrust it at all for anything, the chatbots can pass a turing test because they learned to lie.