- Home /
Causing a chain reaction and hashtable errors
Hi,
This is somewhat of a followup to my previous question here:
http://answers.unity3d.com/questions/277793/toggling-multiple-button-states.html
It is not about the same topic but I am working on the same UI elements. Of course, I will try to explain my problem so that no background reading is required. Well, here we go!
Project description
I'm making a visual GUI for my game's stat upgrade system. Basically, there is a 7x10 grid of GUI.buttons with no texture on them. Gridcontent info is stored in a 2D array that correspond to the location of the node on the grid. The user is meant to place his/her desired stat blocks on the grid to enhance the power of the main character. But there's more!
Blocks of the same stat can chain together creating "power waves" that originate from a central Generator. Chaining together these stats will create a multiplier effect. For example, think of a match-3 style puzzle game where you have to match the colors of blocks. Except that, the direction of the power wave matters as well.
Progress so far
I am on the final stage of the visual implementation of this idea and I am having trouble creating a chain reaction effect. Since the power wave originates from the generator, the user can put similar blocks next to each other but they will not chain unless they are connected to the generator. Once you do connect them to the generator, there should be some kind of chain reaction effect which spreads the power wave among all the connected nodes.
Here is the code I have so far
if (GUI.Button(new Rect(314, 126, 22, 22), texturestoapply[0], mystyle))
{
if (Event.current.button == 1 && texturestoapply[0] != null)
{
Storepotential(new Vector2(1,1), 0);
removemsg = true;
}
if (clickedon == null)
return;
if (clickedon == "upwards" || clickedon == "downwards" || clickedon == "leftwards" || clickedon == "rightwards")
{
Changewave(clickedon, 1, 1);
updateAdjacentnodes(1,1);
Clearallmouseshit();
return;
}
texturestoapply[0] = textureinsidemouse;
gridcontent[1,1] = clickedon;
checkAdjacentnodes(1, 1);
Chainreaction();
}
Above is an example of one of the buttons on the grid. Once you have chosen a stat, you can place it on the grid (click on the button) and it will place the approptiate texture on the button. Also it will immediately check the adjacent nodes to see if it can make any power wave connections.
void checkAdjacentnodes(int gridlocationx, int gridlocationy)
{
//desclaring adjacent nodes
string abovenode, rightnode, belownode, leftnode;
Vector2 aboveloc, rightloc, belowloc, leftloc;
//finding the adjacent nodes
abovenode = gridcontent[gridlocationx, gridlocationy - 1];
aboveloc = new Vector2 (gridlocationx, gridlocationy - 1);
rightnode = gridcontent[gridlocationx + 1, gridlocationy];
rightloc = new Vector2 (gridlocationx + 1, gridlocationy);
belownode = gridcontent[gridlocationx, gridlocationy + 1];
belowloc = new Vector2 (gridlocationx, gridlocationy + 1);
leftnode = gridcontent[gridlocationx - 1, gridlocationy];
leftloc = new Vector2 (gridlocationx - 1, gridlocationy);
//Find out the current wave direction through gridcontent
string currentwave = gridcontent[gridlocationx, gridlocationy];
//Find out what the stat is and clears the mouse "clipboard"
string currentstat = Queryforstats(gridlocationx, gridlocationy);
//generator check
if (abovenode == "generator")
{
gridcontent[gridlocationx, gridlocationy] = "powerwavedown";
}
if (rightnode == "generator")
{
gridcontent[gridlocationx, gridlocationy] = "powerwaveleft";
}
if (belownode == "generator")
{
gridcontent[gridlocationx, gridlocationy] = "powerwaveup";
}
if (leftnode == "generator")
{
gridcontent[gridlocationx, gridlocationy] = "powerwaveright";
}
if (currentstat == "spd")
{
//check for adjacent active powerwaves
if (abovenode == "powerwavedown")
{
//if its the same stat then you can continue the wave
if (spdnodes.Contains(aboveloc))
{
gridcontent[gridlocationx, gridlocationy] = "powerwavedown";
}
}
if (rightnode == "powerwaveleft")
{
//if its the same stat then you can continue the wave
if (spdnodes.Contains(rightloc))
{
gridcontent[gridlocationx, gridlocationy] = "powerwaveleft";
}
}
if (belownode == "powerwaveup")
{
//if its the same stat then you can continue the wave
if (spdnodes.Contains(belowloc))
{
gridcontent[gridlocationx, gridlocationy] = "powerwaveup";
}
}
if (leftnode == "powerwaveright")
{
//if its the same stat then you can continue the wave
if (spdnodes.Contains(leftloc))
{
gridcontent[gridlocationx, gridlocationy] = "powerwaveright";
}
}
//check for adjacent nodes of just the same thing but no waves
if (leftnode == "spd")
gridcontent[(int)leftloc.x, (int)leftloc.y] = currentwave;
if (rightnode == "spd")
gridcontent[(int)leftloc.x, (int)leftloc.y] = currentwave;
if (leftnode == "spd")
gridcontent[(int)leftloc.x, (int)leftloc.y] = currentwave;
if (leftnode == "spd")
gridcontent[(int)leftloc.x, (int)leftloc.y] = currentwave;
}
}
string Queryforstats(int gridX, int gridY)
{
if (speedon)
{
spdnodes.Add(new Vector2(gridX, gridY));
Clearallmouseshit();
return "spd";
}
if (strengthon)
{
strnodes.Add(new Vector2(gridX, gridY));
Clearallmouseshit();
return "STR";
}
if (viton)
{
vitnodes.Add(new Vector2(gridX, gridY));
Clearallmouseshit();
return "vit";
}
return null;
}
What we have above is the function that checks adjacent nodes and a helper function that queries the current node to see what stat has been added to the grid. If both the stat type and power wave direction match with their peers, it will continue the power wave.
void Chainreaction()
{
foreach (Vector2 gridloc in spdnodes)
{
updateAdjacentnodes((int)gridloc.x, (int)gridloc.y);
}
foreach (Vector2 gridloc in strnodes)
{
updateAdjacentnodes((int)gridloc.x, (int)gridloc.y);
}
foreach (Vector2 gridloc in vitnodes)
{
updateAdjacentnodes((int)gridloc.x, (int)gridloc.y);
}
Debug.Log("firstsquare: " + gridcontent[1,1].ToString() + "\n secondsquare: " + gridcontent[2,1].ToString());
}
This above function is the one that I am struggling with. What I want it to do is check all the nodes on the grid that have a stat block placed on them. And then, check each of THEIR adjacent nodes to see if there are latent connections that can be made. Like when THAT PERFECT BLOCK falls into the perfect place in a match 3 game. Something like this:
http://www.youtube.com/watch?v=56CS2b89pLI
InvalidOperationException: Hashtable.Enumerator: snapshot out of sync.
I am also getting this error but I am not sure whether or not it is stopping the execution of something I have coded. I am also unsure where the error is originating from.
If anyone has any ideas about how to do this better or where the error is coming from, I would love to hear it!
Thanks for reading.