List of Structs variable values not changing
I feel like I'm missing something complete obvious so I apologise in advance if (when?) that is the case. I'm trying to do something really simple, change a bool value in a struct from false to true. Obviously I can't change it directly, so I created a method within the struct that I can call which should change the value there. It doesn't seem to be the case. Here is the code and I'd appreciate any insight;
public void MethodName(ScanLineNode node [...])
{
//This will perform a raycast from the Node's position in the specified direction. If the raycast hits nothing, it will return Vector3.zero ('Row is complete'), otherwise will return the hit point
Vector3 terminationPoint = node.RaycastDirection(node, direction, maxDist, targetRaycast, replacementColour, backgroundColour);
ScanLineNode terminationNode = new ScanLineNode();
//Previously attempted to store a local reference to this row being used, but also did not work
//List<ScanLineNode> rowNodes = allScanLineNodes[node.rowNumber];
[...]
if (terminationPoint == Vector3.zero)
{
//Definitely reaches this point, and executes this function along the row, I have added breakpoints and checked what happens in this for loop. After running 'RowComplete' (which just changes 'rowComplete' from false to true) 'rowComplete' is still false. Just in case I've included the RowComplete() function below.
Debug.Log("Row Complete: " + node.rowNumber);
for (int i = 0; i < allScanLineNodes[node.rowNumber].Count; i++)
{
allScanLineNodes[node.rowNumber][i].RowCompleted();
}
}
}
ScanLineNode Struct -- Most stuff is hidden (that I don't believe is affecting this), I have included the RowComplete() function however.
public struct ScanLineNode
{
[...]
public bool rowComplete;
[...]
public ScanLineNode([...])
{
[...]
rowComplete = false;
[...]
}
public void RowCompleted()
{
rowComplete = true;
}
}
I have also confirmed that RowCOmpleted() does not get called anywhere aside the above location, and 'rowComplete' is only called from the RowComplete() function
Answer by Hamburgert · Feb 05, 2018 at 09:24 AM
Structs are value type objects, which means that accessing a struct in a list returns a copy of the struct. So you are actually modifying a copy of the struct, and the "real" struct stays untouched.
// This is a copy. Calling methods will only call the method on the copy.
allScanLineNodes[node.rowNumber][i];
The usual solution to this is to simply change the struct to a class. The other solution is to overwrite the list index with a modified struct, thus making sure the list to contains the new value(s).
ScanLineNode node = allScanLineNodes[node.rowNumber][i];
node.RowCompleted();
allScanLineNodes[node.rowNumber][i] = node; // overwrite values
And that's how you work with structs. I feel your pain, I've been through the same process of learning the hard way how structs work.. or rather, how they don't.
Yeah I thought it was something like that. I've used structs before, but when things got complicated I just converted them to a class haha but I wanted to stick with structs and learn why this time. I posted the same question on StackOverflow and got a similar answer, something to do with immutable vs mutable. But you explanation was a bit more clear. Thanks!
You should add a link to the stackoverflow answer Perhaps that answer provides more technical information about structs, that people might want to read :)