How can you dynamically choose an object?

I’m not exactly sure how to ask this question as I don’t know the proper words to use, so I’ll use examples.

Basically, I have 40 objects and each of the 40 objects has a matching array; object01, array01, object02, array02, etc. The arrays hold a specific pattern of 6-7 objects, namely adjacent objects.

I don’t have the script written yet as I’m trying to bypass writing lengths of code to match these together, which I’d likely do with an obnoxiously long switch. My goal is that I wish to click object05, and have a few variables for object05 changed, as well as the objects in array05. The only way I know to do that is to store the clicked object in a variable, check it in a switch, run a for loop to check and switch the variables matched to those objects on their respective individual scripts. It just feels like there’s a better way to do it, more efficient for me the programmer, and for the game to process, too. So, I wonder, is there a function or something I can write to determine that can basically match object01 to array01 without manually entering it in a switch? The naming and numbering conventions match.

Think of… a 4x4 grid of cubes, if we pick cube06, we have… cube02, cube05, cube07, and cube10 adjacent to it. When you click cube06, various identical things are to be done to all adjacent cubes.

If there’s a better way to accomplish what I’m doing, I’m open to suggestions!

Thanks!

Write a script component called “AdjacencyManager” which holds an array of adjacent objects. At start up you will want to fill the array with adjacent objects. How you do this will depend on the data structure of your game. Using a physics.OverlapSphere would even work. Later you find the AdjacencyManager object and iterate through all objects in the list.

It may be a better option to design a grid-based data structure for your game if you are making a grid -based game. Create Grid objects that each contain a coordinate, a reference to the object at its location, and a reference to all adjacent Grid locations. This could also generalize to non-square grid types easily.

Developing a data structure that represents your game state and can efficiently complete common game tasks is a core aspect of your game design. Do not use a giant switch statement. That would be fragile, difficult to change, and a pain to work with.

I was recommended using classes on the Answers site, seconds before it was closed for being a design question, and was subsequently recommended to post here. I’m not sure about how that would work or anything.

I also read about Jagged Arrays, it might work for me to do something like that, an array of the 40 of my 6-7 length arrays. I don’t mind manually typing the arrays in and already have even; the 40 arrays are filled with the game objects in which are adjacent to each specific of the 40 objects. It’s faster (game-wise) to do something like this manually rather than making the program find them, right? How do you feel about using jagged arrays for this situation? It’s not a grid-based game sadly; your idea for that seems good.

The actual spacial location of the objects isn’t important, it’s just which items are linked that matter, and it doesn’t change at all in the game; it’s a small puzzle game.

Thank you for your help, by the way!

Any more suggestions, anyone?

I’ve been trying to mess with jagged arrays now because I thought I understood it, but I can’t seem to get it to work; everything I’ve tried results in a different error.

Could you describe your issue more clearly, or perhaps with more detail? For instance, you mention that you have a set of objects and, for each object in that set, a set of other objects that are adjacent. This suggests some spatial relationship, such as a grid, but later you mention that spatial location is not the property that matters.

Can you define what you mean by adjacent and linked? In other words, for each object what determines how the set of “adjacent” objects is to be populated? Off the top of my head, it seems you need to build some key-value mapping. The .NET libraries have a few options for you in that case, such as a Dictionary.

Well, it’s a puzzle game that I’m making. It consists of a sphere, where you click parts of the object and the nearby objects change. You’ll move the object around to see all parts of it. It has different shapes on there, some objects have 6 adjacent, and some objects have 7 adjacent. The literal location isn’t necessary to keep track of, because they’re all separate child objects tied to a parent game object that is the sphere, and they will always be in the same spot in relation to each other.

I’m not trying to find the objects in the game to see which are adjacent, I have it 100% already mapped out with separate arrays for each of the individual 40 objects, that part is done, and working even, now. The problem is how to go about accessing them properly. I could put a unique script on each of the 40 objects with their own array and go from there, but that seems pretty inefficient on all fronts. CaptainScience said that using a Switch to determine which array to use based on which object was clicked would be bad to do too.

From what I’ve found, jagged arrays seem to be the answer, since you refer to it based on just the location in the array rather than a set array name like with individuals, but I can’t seem to get it working with GameObjects.

I’m perfectly opened to other suggestions though, as I definitely don’t know what all of my options even are, let alone the best choice for what I’m doing.

I think I understand now, and I definitely think the solution is a collection supporting a Key-Value lookup, like a Dictionary. This seems to be what you would be implementing with jagged arrays, in any case. At least, generating the proper index into a jagged array would be more complicated than choosing a suitable key for a table lookup (in my opinion).