Confused between a stuct and a class(Flood fill not working when class)

Hi I’m confused why my floodfill and pathfinding wont work If I change my GridNode from a struct to a class. Information Below, I just want to understand why this is. I never really used struts before this so its new. I looked on google but I think someone looking at my code and giving me info would be the best way to learn why this is happing. I know this can be a lot of code so thanks for your time.

I create my grid nodes like this.
public List<GridNode[,]> gridNodes; //Its a list of a list of multidimensional array gridnodes so I can have grid nodes on different floors so I can path find to different floors(works).

for (int i = 0; i < layers.Count; ++I)
{
gridNodes.Add(newGridNode[width,length]);
}
I set values of a gridNode like this.
Nested For Loops
{
GridNode newGridNode = newGridNode(k, newVector3((i * nodeSize), layers[k].positionY, (j * nodeSize)), newVector2(i, j), initalCost, true);
//Raycast for Height
//Raycast for Position
gridNodes[k][i, j] = newGridNode;
}
```csharp
*using UnityEngine;
sing System.Collections;
using System.Collections.Generic;

public struct GridNode
{
public int layer;
public Vector3 position;
public Vector2 gridNodeIndex;
public GridNode gridNodesNeighbors;
public float cost;
public bool isTraversable;
public bool render;
public enum TileType {none,walk,attack,heal,skill}
public TileType tileType;

public GridNode(int layer,Vector3 position, Vector2 gridNodeIndex,float cost,bool isTraversable)
{
    this.layer = layer;
    this.position = position;
    this.gridNodeIndex = gridNodeIndex;
    this.cost = cost;
    this.isTraversable = isTraversable;
    this.gridNodesNeighbors = new GridNode[0];
    this.render = false;
    this.tileType = TileType.none;
}

public static bool operator == (GridNode first,GridNode second)
{
    if (first.gridNodeIndex == second.gridNodeIndex && first.layer == second.layer)
        return true;
    return false;
}

public static bool operator != (GridNode first,GridNode second)
{
    if (first.gridNodeIndex == second.gridNodeIndex && first.layer == second.layer)
        return false;
    return true;
}

}*
* *csharp
*using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

public class Seeker : MonoBehaviour
{
public Transform target;
public bool seekPath;
public bool eightConnections;
public bool drawGizmos;

Path path = new Path();
List<Expansion> open;
HashSet<GridNode> closed;
GridNode startingNode, targetNode;
Expansion best;
List<bool[,]> visited;

float costToGoal;
float costToNextNode;
float costFromStart;

enum FloodFillType {walk, attack, heal, skill}
FloodFillType floodFillType;
int floodFillDistance;
int floodFillCurrentDistance;

public Path SearchPath(Vector3 startingPosition, Vector3 targetPosition)
{
    open = new List<Expansion> ();
    closed = new HashSet<GridNode> ();
    startingNode = FindClosestGridNode(startingPosition);
    targetNode = FindClosestGridNode(targetPosition);
    best = new Expansion(startingNode,null,0,0);
    best.current = startingNode;
    open.Add(best);

    visited = new List<bool[,]>();
    for (int i = 0; i < GridGraph.instance.layers.Count; ++i)
        visited.Add(new bool[GridGraph.instance.width, GridGraph.instance.length]);

    //SEARCH
    for(;;)
    {
        if (best.current == targetNode && best != null || open.Count == 0)
        {
            Path foundPath = new Path();
            foundPath.gridNodes = new List<GridNode>();
            Expansion iterator = best;
            while(iterator != null)
            {
                foundPath.gridNodes.Add(iterator.current);
                iterator = iterator.previous;
            }
           
            foundPath.gridNodes.Reverse();
            return foundPath;
        }

        //FIND BEST SCORE
        best = null;
        foreach(Expansion expansion in open)
        {
            if (best == null || expansion.toltalCost < best.toltalCost)
                best = expansion;
        }

        ExpandPath();
    }
}

private void ExpandPath()
{
    closed.Add(best.current);
    open.Remove(best);
    if (GridGraph.instance.gridType == GridGraph.GridType.square)
    {
        GridNode expansionNode = best.current;
        expansionNode.gridNodeIndex.y -= 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        expansionNode.gridNodeIndex.y += 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        expansionNode.gridNodeIndex.x -= 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        expansionNode.gridNodeIndex.x += 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        for (int i = 0; i < expansionNode.gridNodesNeighbors.Length; ++i)
            AddToExpansionPath(expansionNode.gridNodesNeighbors[i]);
    }
    else if (GridGraph.instance.gridType == GridGraph.GridType.hex)
    {
        GridNode expansionNode = best.current;
        expansionNode.gridNodeIndex.x -= 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        expansionNode.gridNodeIndex.x += 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        expansionNode.gridNodeIndex.x += 1;
        expansionNode.gridNodeIndex.y -= 1;
        if (expansionNode.gridNodeIndex.y % 2 != 0)
            expansionNode.gridNodeIndex.x -= 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        expansionNode.gridNodeIndex.x -= 1;
        expansionNode.gridNodeIndex.y -= 1;
        if (expansionNode.gridNodeIndex.y % 2 == 0)
            expansionNode.gridNodeIndex.x += 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        expansionNode.gridNodeIndex.x -= 1;
        expansionNode.gridNodeIndex.y += 1;
        if (expansionNode.gridNodeIndex.y % 2 == 0)
            expansionNode.gridNodeIndex.x += 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        expansionNode.gridNodeIndex.x += 1;
        expansionNode.gridNodeIndex.y += 1;
        if (expansionNode.gridNodeIndex.y % 2 != 0)
            expansionNode.gridNodeIndex.x -= 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionPath(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y]);

        expansionNode = best.current;
        for (int i = 0; i < expansionNode.gridNodesNeighbors.Length; ++i)
            AddToExpansionPath(expansionNode.gridNodesNeighbors[i]);
    }
}

public void AddToExpansionPath(GridNode gridNode)
{
    if (!visited[gridNode.layer][(int)gridNode.gridNodeIndex.x, (int)gridNode.gridNodeIndex.y] && gridNode.isTraversable)
    {
        visited[gridNode.layer][(int)gridNode.gridNodeIndex.x, (int)gridNode.gridNodeIndex.y] = true;
        costToGoal = Vector2.Distance(gridNode.gridNodeIndex, targetNode.gridNodeIndex);
        costToNextNode = Vector2.Distance(gridNode.gridNodeIndex, gridNode.gridNodeIndex);
        costFromStart = best.costFromStart;

        open.Add(new Expansion(gridNode, best, costFromStart + costToNextNode + gridNode.cost, costToGoal));
    }
}

public void GenerateMovementGrid(Vector3 startingPosition, int floodFillDistance)
{
    DegenerateGrid();
    floodFillType = FloodFillType.walk;
    startingNode = FindClosestGridNode(startingPosition);
    this.floodFillDistance = floodFillDistance;
    GridGraph.instance.gridNodes[startingNode.layer][(int)startingNode.gridNodeIndex.x, (int)startingNode.gridNodeIndex.y].render = true;
    GridGraph.instance.gridNodes[startingNode.layer][(int)startingNode.gridNodeIndex.x, (int)startingNode.gridNodeIndex.y].tileType = GridNode.TileType.walk;
    GenerateGrid();
}

public void GenerateAttackGrid(Vector3 startingPosition, int floodFillDistance)
{
    DegenerateGrid();
    floodFillType = FloodFillType.attack;
    startingNode = FindClosestGridNode(startingPosition);
    this.floodFillDistance = floodFillDistance;
    GenerateGrid();
}

public void DegenerateGrid()
{
    for (int k = 0; k < GridGraph.instance.layers.Count; ++k)
        for (int i = 0; i < GridGraph.instance.width; ++i)
            for (int j = 0; j < GridGraph.instance.length; ++j)
            {
                GridGraph.instance.gridNodes[k][i, j].render = false;
                GridGraph.instance.gridNodes[k][i, j].tileType = GridNode.TileType.none;
            }
}

private void GenerateGrid()
{  
    floodFillCurrentDistance = 0;      
    open = new List<Expansion>();
    closed = new HashSet<GridNode>();
    best = new Expansion(startingNode, 0);
    best.current = startingNode;
    open.Add(best);
 

    visited = new List<bool[,]>();
    for (int i = 0; i < GridGraph.instance.layers.Count; ++i)
        visited.Add(new bool[GridGraph.instance.width, GridGraph.instance.length]);

    while (true)
    {
        if (open.Count > 0)
            ExpandFloodFill(open[0]);
        else
            break;
    }
}

private void ExpandFloodFill(Expansion current)
{
    open.Remove(current);
    closed.Add(current.current);
    floodFillCurrentDistance = current.currentFloodFillDistance + 1;
    visited[current.current.layer][(int)current.current.gridNodeIndex.x, (int)current.current.gridNodeIndex.y] = true;

    if (GridGraph.instance.gridType == GridGraph.GridType.square)
    {
        GridNode expansionNode = current.current;
        expansionNode.gridNodeIndex.y -= 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionFloodFill(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y], floodFillCurrentDistance);

        expansionNode = current.current;
        expansionNode.gridNodeIndex.y += 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionFloodFill(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y], floodFillCurrentDistance);

        expansionNode = current.current;
        expansionNode.gridNodeIndex.x -= 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionFloodFill(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y], floodFillCurrentDistance);

        expansionNode = current.current;
        expansionNode.gridNodeIndex.x += 1;
        if (PathNodeInBounds(expansionNode))
            AddToExpansionFloodFill(GridGraph.instance.gridNodes[expansionNode.layer][(int)expansionNode.gridNodeIndex.x, (int)expansionNode.gridNodeIndex.y], floodFillCurrentDistance);

        for (int i = 0; i < expansionNode.gridNodesNeighbors.Length; ++i)
            AddToExpansionFloodFill(expansionNode.gridNodesNeighbors[i],floodFillCurrentDistance);
    }
}

private void AddToExpansionFloodFill(GridNode gridNode, int distance)
{
    if (!visited[gridNode.layer][(int)gridNode.gridNodeIndex.x, (int)gridNode.gridNodeIndex.y] && distance < floodFillDistance + 1)
    {
        visited[gridNode.layer][(int)gridNode.gridNodeIndex.x, (int)gridNode.gridNodeIndex.y] = true;
        if (gridNode.isTraversable)
        {
            open.Add(new Expansion(gridNode, distance));
            GridGraph.instance.gridNodes[gridNode.layer][(int)gridNode.gridNodeIndex.x, (int)gridNode.gridNodeIndex.y].render = true;

            if (floodFillType == FloodFillType.walk)
                GridGraph.instance.gridNodes[gridNode.layer][(int)gridNode.gridNodeIndex.x, (int)gridNode.gridNodeIndex.y].tileType = GridNode.TileType.walk;
            else if (floodFillType == FloodFillType.attack)
                GridGraph.instance.gridNodes[gridNode.layer][(int)gridNode.gridNodeIndex.x, (int)gridNode.gridNodeIndex.y].tileType = GridNode.TileType.attack;
        }
    }
}

private bool PathNodeInBounds(GridNode gridNode)
{
    return gridNode.gridNodeIndex.x >= 0 && gridNode.gridNodeIndex.y >= 0 && gridNode.gridNodeIndex.x < GridGraph.instance.width && gridNode.gridNodeIndex.y < GridGraph.instance.length;
}

public GridNode FindClosestGridNode(Vector3 position)
{
    GridNode closestGridNode = GridGraph.instance.gridNodes[0][0, 0];

    for (int k = 0; k < GridGraph.instance.layers.Count; ++k)
    {
        for (int i = 0; i < GridGraph.instance.width; ++i)
        {
            for (int j = 0; j < GridGraph.instance.length; ++j)
            {
                float currentGridNodeDistance = Vector3.Distance(position, closestGridNode.position);
                float nextGridNodeDistance = Vector3.Distance(position, GridGraph.instance.gridNodes[k][i, j].position);

                if (nextGridNodeDistance < currentGridNodeDistance)
                    closestGridNode = GridGraph.instance.gridNodes[k][i, j];
            }
        }
    }
    return closestGridNode;
}

public class Expansion
{
    public GridNode current;
    public Expansion previous;
    public float toltalCost;
    public float costFromStart;
    public float costToGoal;
    public int currentFloodFillDistance;

    public Expansion(GridNode current,Expansion previous,float costFromStart,float costToGoal)
    {
        this.current = current;
        this.previous = previous;   
        this.costFromStart = costFromStart;
        this.costToGoal = costToGoal;
        this.toltalCost = costFromStart + costToGoal;
    }

    public Expansion(GridNode current, int currentFloodFillDistance)
    {
        this.current = current;
        this.currentFloodFillDistance = currentFloodFillDistance;
    }
}

void OnDrawGizmos()
{
    if (path.gridNodes != null && drawGizmos)
    {
        try
        {
            Gizmos.color = Color.green;

            int startingNode = 0;
            for (int i = 1; i < path.gridNodes.Count; ++i)
            {
                int nextGridNode = i + 1;
                if (nextGridNode < path.gridNodes.Count)
                {
                    float currentGridNodeDistance = Vector3.Distance(transform.position,path.gridNodes[startingNode].position);
                    float nextGridNodeDistance = Vector3.Distance(transform.position,path.gridNodes[nextGridNode].position);

                    if (nextGridNodeDistance < currentGridNodeDistance)
                        startingNode = i;
                }
            }

            for (int i = startingNode; i < path.gridNodes.Count; ++i)
            {
                int nextGridNode = i + 1;
                if (nextGridNode < path.gridNodes.Count)
                    Gizmos.DrawLine(path.gridNodes[i].position + new Vector3(0,0.01f,0),path.gridNodes[nextGridNode].position + new Vector3(0,0.01f,0));
            }
        }
        catch{}
    }
}

}*
```
Thanks

A class is a reference type.
A struct is a value type.

http://www.albahari.com/valuevsreftypes.aspx

So a struct works like ints, floats etc.

A class is passed as a reference which means that particular class and not a copy of that class. So i guess you somewhere in your code set class instanceA = instanceB, so when you change a value in one you will also change it in the other class and mess up your system. You can create copy’s of classes in these cases. You can look that up on google.

2 Likes

As noted the key difference is reference versus value.

With structs if you do

structA = stuctB;

You now have two copies of the struct. Changes to structA do not affect structB.

With classes if you do

classA = classB;

You have a single class referenced in two places. Change classA and classB changes along with it.

1 Like

Thanks you two, I feel stupid and sorry for the stupid question.

Don’t. Value versus reference is an odd concept to get your head around the first time you come across it.