✅ World generation optimization
Hey guys, i'm having issues with world generation for my game, do you guys know how i could optimize it? it takes 15s for the window to load rn
code.cs4.01KB


async Task thoughjs for example is so much simplerTask which basically defines the internal workings on asynchronous programmingTask means asychronous. This means you can switch between asynchronous and synchronous in C#Task.Run, due to the way asynchronous works with threadsTask.Run ensures it guarantees that it's run in the backgroundGenerateWorld(). You don't need the return typeGenerateWorld is not asynchronousasync TaskjsTask.RunTask.RunGenerateWorld()GenerateWorld public Tile(MainWindow window, double x, double y, TileType type) : this(window)
{
X = x;
Y = y;
Type = type;
List<Tile> tiles = _MainWindow.Display.Children.OfType<Tile>().ToList();
tiles = tiles.Where(r => r.Type == Type && r.Source != null).ToList();
if (tiles.Count>0)
{
Tile tile = tiles.First();
Source=tile.Source;
}
else
{
ImageBrush brush = new ImageBrush();
try
{
brush.ImageSource = new BitmapImage(new Uri(GetImagePath(type), UriKind.RelativeOrAbsolute));
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
Source = brush.ImageSource;
}
MouseLeftButtonDown += TileClicked;
}public MainWindow()
{
var generate = GenerateWorld()
}
private async Task GenerateWorld()
{
//code here
}private async void GenerateWorld()
{
List<Tile> tiles = new List<Tile>();
for(int i=0;i<WorldHeight;i++)
{
for(int j=0;j<WorldWidth;j++)
{
//populate list
tiles.Add(new Tile(i,j))
}
}
await Dispatcher.InvokeAsync(()=>{
foreach(Tile tile in tiles)Display.Children.Add(tile)
})
}