17.1 C
New York
Tuesday, April 29, 2025

Monogame – Perceive C# duties


I take advantage of plenty of route discovering in my Monogame challenge that has effects on the body, as a result of within the worst case you will discover a path can take 200 ms. So I sought to make use of duties to unfold the computing search route in a number of footage and should even calculate them concurrently.

This required me to rewrite the route search system in a model that I think is slower and which positively I ought to use extra reminiscence. Nevertheless! Once I did to work, the route discovery now takes a most of 8 ms, and the usage of reminiscence based on Visible Studio has fallen into ~ 100 MB.

That’s good, after all, however solely with the curiosity of understanding what is occurring, can anybody clarify why that is?

The adjustments I made earlier than afterwards had been principally to remove static route search information, so that every process can have its personal copy, after which put the route discovering the calls in a Lambda and fed them to the duties.

Earlier than:

whereas (requestQueue.Depend > 0)
{
    PathRequest request = requestQueue.Dequeue();

    Listing path = AStar.FindPathList(map, request.origin, request.goal);
    if (path == null)
    {
        Debug.Warn("Unable to seek out path.");
    }
    else
    {
        path = AStar.SimplifyPath(path);
    }
}

After:

whereas (requestQueue.Depend > 0)
{
    PathRequest request = requestQueue.Dequeue();

    Process pathTask = Process.Run(() =>
    {
        Listing path = AStar.FindPathList(map, request.origin, request.goal);
        if (path == null)
        {
            Debug.Warn("Unable to seek out path.");
        }
        else
        {
            path = AStar.SimplifyPath(path);
        }

        request.returnAction(path);
    });
}

How do I make requests:

Tile origin = map.tiles.Get(/* ... */));
Tile goal = map.tiles.Get(/* ... */));

pathManager.MakePathRequest(ReturnAction, origin, goal);
travelShip.isAwaitingPath = true;

void ReturnAction ( Listing path )
{
    agent.isAwaitingPath = false;

    if (path == null)
        agent.remodel.WorldPosition = new Vector2(map.BorderSize * 4, map.BorderSize * 4);
    else
        agent.path = path;
}
```

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles