Update Grid Graph Rules Texture at runtime

  • A* version: latest
  • Unity version: Unity6 / Unity 2023.3
    Hi,
    we try to create a Splatoon like painting feature in our game. The Ai in the game should be influenced by the color the ground is painted at the moment. In case the player can change the painting on the ground, the penalties should be updated in runtime.
    We saw the Grid Graph Rules provide a Texture feature that would fit our needs.
    The question now is, is it possible and how expensive is it to update the Texture at runtime? It should be updated every time the Ground texture has changed (a player painted something on it). We will use the PAINTZ (Paintz Free | Tools | Unity Asset Store) asset, that is also based on the 4 RGBA channels.

Yours Max

Hmmm, I’m sure it’s possible, but I’m not seeing much on it- it looks like updating the texture and then running a scan is how it would work, but that may also be expensive to run.

I do want to provide a methodology of thinking, just in case that it does work like how I said; if you do have to rescan the graph for every texture change, you can mitigate the performance hit by only scanning once every handful of seconds or so. Maybe every 5. But for the situations where the agents will move onto freshly changed paint and would need their penalties updated, you could have a temp object take the place where the paint is and set a pathfinding tag for a lower penalty on that object’s Unity tag. Then, every 5 seconds you can take all those newly added zones, apply them to a texture, use that texture in the Grid Graph Rule, remove all the temp spots, rescan and done!

On paper that would save a lot of performance, and I’d imagine would work well to keep the game functioning as if it were just a texture.

That idea said, I’ll tag @aron_granberg to see what he thinks would be best here, because I most likely have come up with a roundabout solution to something that already has a proper way of doing it. I’m good at doing that :sweat_smile:

Hi

What you’d have to do is:

  1. Update the texture in whatever way you want
  2. Update the graph in the region you changed using AstarPath.active.UpdateGraphs(bounding box...).

The RuleTexture class will automatically detect that the texture has changed using the Texture.imageContentsHash property. As long as that changes, it will pick it up. Check that PAINTZ actually updates this property, otherwise you’ll have to do it yourself. Even just incrementing it by 1 every time you paint into the texture is fine.

This will be decently fast, but the rule will have to re-read the whole texture to get the new data. This may be expensive, depending on how large the texture is.

I think this should work for your use case, and be performant enough, but I don’t know exactly how expensive it would be.

Wow, thanks for this detailed answer. The AI is primary for small bots that have to cross the map. So they don’t have to be very precisely.
I’ve seen the texture rule is a pro feature. Is there a chance to test this for an amount of time?

The asset store doesn’t really have a way to do trials, I’m afraid.
But if you try it, and it turns out that it doesn’t suit your game, then I can arrange a refund for you.

This is exactly what I meant by me overengineering things that are probably supported better out of the box :joy:

Yeah but who should support edge cases like this?