Jump to content
  • Advertisement
Sign in to follow this  
Fl00Fy

Unity Reference/Value types in C#

This topic is 2079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm busy trying to make a game in Unity3D, but haven't had much experience in C# and having some trouble finding any specifics on how value/reference types are handled. I understand the basics of it; that most primitive data types, structs and enums are value type (unless specified by the ref qualifier), and all classes are reference types. Knowing that, I'm assuming the code I've currently got isn't working the way I meant it to because of my lack of knowledge. I'm trying to make a fairly simple card game, and have a Hand class, and a Deck class, both of which are simply containers for Card objects.

public class Deck
{
	private List<GameObject> cards;
	private const int TOTAL_CARDS = 52;
	private int iterator;

        //Constructors...

	public GameObject drawCard()
	{
		if (cards.Count == 0)
		{
			//Merge discard pile with deck
		}
		GameObject temp = cards[iterator];
		cards.RemoveAt(iterator++);
		
		return temp;
	}
}
public class Hand{
	
	private List<GameObject> cards;
	private int numCards;
	private Deck deck;
	
	public Hand(Deck deck)
	{
		this.deck = deck;
		numCards = 0;
		cards = new List<GameObject>(15);
	}

	public void drawCard()
	{
		GameObject card = deck.drawCard();
		Transform cardTransform = card.transform;
		cardTransform.position.Set(1, 1, 1);
		cardTransform.Rotate(new Vector3(0, 1, 0), 180);
		cards.Add(card);
		numCards++;
	}
}

Above is a simplified version of those two classes, the methods the question pertains to is the drawCard() methods in both classes. In the Deck's drawCard method, would removing the object from the List remove a reference or an object; and would the object returned by that method and used in the Hand's drawCard method be a reference pointing to an object that has been deleted(removed from the list) or will it be retained?

 

Thanks for any help. 

Share this post


Link to post
Share on other sites
Advertisement

Judging by your previous posts you seem to have experience with C++. You can think of objects as pointers to objects. When you remove one from List<> you simply remove pointer, but do not delete object itself (memory leak), but in C# garbage collector prevents those leaks. Therefore removing from a List<> will remove only reference.

Share this post


Link to post
Share on other sites
In C#, as long as you still have a reference to an object it won't be deleted. So if your object is in the Hand list as well as the Deck list and you remove it from one list, the reference that the other list holds will prevent the object from being deleted.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!