I’m doing something wrong, but I don’t know what:
In a grid graph on a terrain, I want to set for all nodes for all 8-neighborhood connections of a node the cost of the connection according to some function of the height difference of its two nodes.
I tried doing that with a rule in the postprocess and afterconnection pass, and as a work item in the start of a monobehavior.
I know that I can clear the “normal” gridgraph connection with node.ClearConnections(true)
, then I set the new cost with GraphNode.Connect(gridNode, neighborGridNodeBase, newcost, OffMeshLinks.Directionality.TwoWay);
Could someone explain to me what is missing? Ideally I would like to update the connections in a custom rule.
Also, I understand that in the context of the rule, I can see the nodes and connections as some nativearray number values. But can I access a gridgraph in a rule as well, because there I get all the neighbourhood helper functionality.
I might have some misunderstanding, what is available when on startup (nodes array is sometimes null), and how to clear and add connections, how to deal with custom connections specific to one node vs the general direction costs (1000/1400) for the whole grid graph, it seems.
I have read this thread: Grid graph ignoring AddConnection cost and the documentation about connections and rules.
Thanks for any and all hints, explanations, code snippet recipes
Are you calling this within a work system? I believe if you’re calling this during runtime it may not work unless done in a work item. From the Graph Updates documentation:
Graph data must only be modified when it is safe to do so. Pathfinding might be running at any time so it is necessary to first pause the pathfinding threads and then update the data.
Ah, I see. I thought that if you do something within a rule, this is already taken care of. I will try to put my graph manipulation that relies on the whole graph datastructure within a workitem within the rule. Thanks!
Ah I missed this part, I’m sorry. Do let me know if that rule + work item plan works or doesn’t!
Graph data is safe to modify in the PostProcess stage of rules. Not sure why it’s not working for you. What is the result?
Rule + workitem works!
In this variant I cannot access the grid nodes as GridNodeBase:
[Pathfinding.Util.Preserve]
public class RuleHikePathConnections : GridGraphRule
{
public override void Register(GridGraphRules rules)
{
rules.AddMainThreadPass(Pass.PostProcess, context =>
{
// context.nodes is not null, but that's the data
// without the utility functions like AddConnection
//(now GraphNode.Connect(...))
GridGraph gridGraph = context.graph; // not null
GridNodeBase[] gridNodes = gridGraph.nodes; // this is null
});
}
}
In this variant I can access the nodes conveniently:
[Pathfinding.Util.Preserve]
public class RuleHikePathConnections : GridGraphRule
{
public override void Register(GridGraphRules rules)
{
rules.AddMainThreadPass(Pass.PostProcess, context =>
{
AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
GridGraph gridGraph = context.graph; // not null
GridNodeBase[] gridNodes = gridGraph.nodes; // not null
}));
});
}
}
If there is nothing wrong using it like this, I’m ok with this solution.
Thanks for your support!