Parameter passed by value changing calling class.

Recommended Posts

I’m sure the answer to this is quite simple; however, it has got me completely baffled. I’m currently writing a Tetris clone in C# using Monogame. I have run into a problem while trying to implement a wall kick for shape rotations. When a shape needs to be rotated, a new temporary shape is created from the original shape and then rotated. This rotated temporary shape is then used to check whether or not the rotation is valid by checking to see if any part of the shape covers any of the non-falling squares on the board or is out of bounds. If either is true, it tries to kick the shape left, if it’s still invalid it kicks it to the right and so on.

The kick operation is performed using a method named TryKick which accepts the temporary rotated shape as a parameter named aRotatedTetrimino. When trying to kick the shape, aRotatedTetrimino is passed by value to a method named CanKick which determines whether or not the shape can be kicked in the direction specified. This is where my problems lie. When aRotatedTetrimino is passed to CanKick, changes that are made to it are being applied in TryKick. I have no idea how this is happening.

Here is the code:

[spoiler]

/// <summary>
/// Tries to rotate the tetrimino shape.
/// </summary>
public void RotateTetrimino(List<Square> aNonFallingSquares)
{
if (!IsRotateOnCooldown())
{
List<Square> rotatedTetrimino = GetRotatedTetrimino();
bool rotateSuccessful = false;

if (IsRotationValid(rotatedTetrimino,
aNonFallingSquares))
{
Console.WriteLine("Good Rotation");

rotateSuccessful = true;
}
else
{

rotatedTetrimino = TryKick(rotatedTetrimino,
aNonFallingSquares,
ref rotateSuccessful);
}

if(rotateSuccessful)
{
squares = rotatedTetrimino;

currentShapeVariaton = IncrementShapeVariation();
timeSinceLastRotate = 0;
}

timeSinceLastRotate = 0; //Remove when rotatedTetrimino problem fixed.
}
}

/// <summary>
/// Tries to kick the tetrimino received in the parameter left, then right, and then down.
/// If the kick is valid, sets the successful rotation flag to true and saves and returns
/// the kicked shape, otherwise it returns the original tetrimino shape received in the
/// parameter.
/// </summary>
/// <param name="aRotatedTetrimino">Tetrimino to kick.</param>
/// <param name="aNonFallingTetrimino">Non falling tetriminos on the gameboard.</param>
/// <returns>A tetrimino shape.</returns>
public List<Square> TryKick(List<Square> aRotatedTetrimino, List<Square> aNonFallingSquares,
ref bool aRotationSuccessful)
{
Console.WriteLine("Kicking");

if (CanKick(aRotatedTetrimino,
aNonFallingSquares,
-1.0f, //Left
true))
{
aRotationSuccessful = true;
Console.WriteLine("Kick Left Good");
}
else if (CanKick(aRotatedTetrimino,
aNonFallingSquares,
1.0f, //Right
true))
{
aRotationSuccessful = true;
Console.WriteLine("Kick Right Good");
}
else
{
aRotationSuccessful = false;
}

if (aRotationSuccessful)
{
aRotatedTetrimino = tempSquares;
}

return aRotatedTetrimino;
}

/// <summary>
/// Tries to kick a tetrimino in the direction specified in the parameter up to two spaces
/// and returns the result.
/// </summary>
/// <param name="aRotatedTetrimino">Tetrimino to kick.</param>
/// <param name="aNonFallingTetrimino">Non falling tetriminos on the gameboard.</param>
/// <param name="aDirection">Direction to kick (-1=Left/Down, 1=Right/Up)</param>
/// <param name="HorizontalKick">Whether to kick horizontally or vertically.</param>
/// <returns></returns>
public bool CanKick(List<Square> aRotatedTetrimino, List<Square> aNonFallingSquares,
{
List<Square> tempRotatedTetrimino = aRotatedTetrimino;

bool canKick = false;

//Kick up to two times.
for (int a = 0; a < 2; a++)
{
Console.WriteLine("Num of Kicks {0}: {1}", aDirection == 1 ? "Right" : "Left", a);

foreach (Square thisFallingSquare in tempRotatedTetrimino)
{
if (HorizontalKick)
{
Console.WriteLine("Original X coord: {0}", thisFallingSquare.X);
Console.WriteLine("Kicked X coord: {0}", thisFallingSquare.X);
}
else
{
}
}

if (IsRotationValid(tempRotatedTetrimino, aNonFallingSquares))
{
tempSquares = tempRotatedTetrimino;
canKick = true;
break;
}
else
{
canKick = false;
}
}

return canKick;
}



[/spoiler]

Here is the output from a shape trying to be kicked (Important lines in blue):

[spoiler]
Kicking
Num of Kicks Left: 0
Original X coord: -40
Kicked X coord: -80

Original X coord: 0
Kicked X coord: -40
Original X coord: 40
Kicked X coord: 0
Original X coord: 40
Kicked X coord: 0
isValid: False
Num of Kicks Left: 1
Original X coord: -80
Kicked X coord: -120

Original X coord: -40
Kicked X coord: -80
Original X coord: 0
Kicked X coord: -40
Original X coord: 0
Kicked X coord: -40
isValid: False
Num of Kicks Right: 0
Original X coord: -120
Kicked X coord: -80

Original X coord: -80
Kicked X coord: -40
Original X coord: -40
Kicked X coord: 0
Original X coord: -40
Kicked X coord: 0
isValid: False
Num of Kicks Right: 1
Original X coord: -80
Kicked X coord: -40
Original X coord: -40
Kicked X coord: 0
Original X coord: 0
Kicked X coord: 40
Original X coord: 0
Kicked X coord: 40
isValid: False
[/spoiler]

The way I expect it to work is the first “Original X coord” value after “Num of Kicks Left: 0” and “Num of Kicks Right: 0” should be the same because they are each from their own method calls which should be using the same value for their parameter. However, as the output shows the first “Original X coord” value after “Num of Kicks Right: 0” is the value that was set on the last kick left ("Num of Kicks Left: 1").

What I need to know is why are the changes being applied to the calling method when it is passed by value?  I would be very grateful if somebody could explain this to me.

Share on other sites
.Net's List<T> class is a reference type, not a value type. [tt]List<Square> tempRotatedTetrimino = aRotatedTetrimino;[/tt] doesn't copy the list, it creates a reference to the original list. If you want a copy you need to explicitly use [tt]new List<Square>[/tt].

Share on other sites

You are not actually passing the object by value at all. You are passing a reference to an object by value. So you have two references pointing to the same object (one in TryKick and one in CanKick). If you mutate that object those changes are visible when accessed from either reference.

In C# a class is a "Reference Type", so you pass references around to it; there is no pass-by-value for a class object. There are also "Value Types", which are structs and primitive types (which themselves are implemented as structs); these actually will be passed by-value unless you pass them by "ref" or "out".

Share on other sites

Very good to know. Guess I need to pay a little more attention in the future. Thanks for all the help and quick replies.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627655
• Total Posts
2978459

• 10
• 12
• 22
• 13
• 33