- Home /
Searching exclusively for specific indexes in a array
I'm having troubles finalizing my battle system. Currently its a direct single target, you make your move, they do the same, but that was mostly for testing of other components.
In the end, i would like there to be a 3x3 grid stored in a array. Where enemy's spawn at random on the board. This I can manage.
So just for explanatory reasons lets say bottom left is box 0, middle left is box 3, top left 6 and finishing with the 8th square top right.
Let's say I have a spell that should target in a X formation. This would affect grid locations 0,2,4,6,8. So the first course of action would be verifying if there is a gameobject(enemy) present in only those specific locations.
/ I understand checking through the entire grid array if any of locations are taken. But not how to call upon a specific list of indexes in that array. /
And if so, call the damage method on to those gameobjects. Which is already prepared.
But I can't seem to figure out the targeting system I would like to achieve and then checking if it hits anything.
Any kind of assistance or guidance would be greatly appreciated!
Answer by Llama_w_2Ls · Jun 03, 2021 at 07:40 AM
You should probably use a dictionary with keys of type int - for the grid square index - and values of type game object - for the object at that square. Then, you can create an array of indexes you wish to search through, and check if a game object is at that position. For example:
// ===== Dictionary Initialization =====
var gridDict = new Dictionary<int, GameObject>();
for (int i = 0; i < 9; i++)
{
gridDict.Add(i, null);
}
// ===== Enemy Spawning =====
int randomEnemyIndex = Random.Range(0, 9);
var enemyObj = Instantiate(Enemy, enemySpawnPos, Quaternion.identity);
// Places enemy GO at a random grid position
gridDict[randomEnemyIndex] = enemyObj;
// ===== Cross Attack =====
var crossAttack = new int[] {0, 2, 4, 6, 8};
foreach (var index in crossAttack)
{
// There's an enemy at one of the cross attack positions
if (gridDict[index] != null)
{
// Destroys enemy
Destroy(gridDict[index]);
break;
}
}
Hope that helps @BenoitMenard
That's looks great. Thank you very much.
That being said I understand the majority. How we are spawning, attacking and damaging/destroying.
However I'm not too familiar with dictionaries so in the initialization for example. Most of the game is UI based. So I'm going to have a panel with the 9 possible gameobject spaces/images.
When I'm initializing the dictionary, would I not need a:
[Serializablefield] GameObject[] Board;
So that I can specify what the actual pieces are for reference. As I have a whole UI layout that's ready and scaled for any resolution. (Building for mobile)
I don't feel like I can achieve that by populating the grid inside the script. But probably I'm wrong.
Yes, you would need a game object array if you wanted to set all the values through the inspector. You can still populate the grid through the new method by looping through all the objects and adding them to the dictionary. I don't understand your new problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613034528im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Hex map movement problem 2 Answers
NullReferenceException with MultiDimensional Array 0 Answers
2D Hex/ Square Tile Resource Controller Help 0 Answers
Custom Editor Dynamic 2D array 1 Answer
werid array behavior 0 Answers