Best way to have a "node" contain data?

I’m working on a pathfinding algorithm (for my own learning enjoyment) and so far I’ve been able to create a square grid. Which is excellent. But now I’m wondering how to have each “node” on the grid hold a couple of pieces of information (for example, speed and cost, etc) at each grid cell.

In theory, I want some sort of container that holds these pieces of information at each grid cell correct? I’ve come up with several ideas, but the first one I thought of seems like it may not be the best. That is, to create empty game objects at each cell, and attach a script to each empty gameobject that holds the variables I need. However, it seems to me that the component lookup for each script on every cell might be slow compared to other methods?

My second thought is to learn how to use structs (first lol). And then have the information I want at each cell to be within the struct. And create an array of structs. Not sure exactly how that would work.

So all this being said, what is the best “performance critical” way to store information in a node, and then retrieve it as fast as possible as well. I know what I’m asking is somewhat involved, but I’m looking for how people generally do this when they’re looking for performance.

How are you storing your node locations? Why doesn’t that also work for holding cost and speed? Is it because you’re just using an array of locations, and not an array of classes or structs? If so, just change that.

Right now I’m storing them as a built-in array of Vector2’s, so yes just as an array of locations right now. Judging by your answer, I should be doing an array of classes or structs I take it. :slight_smile:

Could you give me an example of how to do an array of structs? Specifically I’m wondering how you access the members of each struct. I’m looking at struct examples right now.

Also, can the data within a struct be changed dynamically without having to create a new struct each time? For example, the speed/cost at each cell will be changing every so often. Is it possible to keep the overall structure of the struct (heheh) and simply change the variables in the struct.

An array of Vector2 objects is an array of structs. :slight_smile: