Trying to make a list tuple of two Integers and after that adding something in it. Then comparing if x,y is one of the tuples in list tuple.
List<Tuple<int, int>> monsterPositions;
I instantly get error like this that it doesn't have Tuple:
Assets/TimeMap.cs(20,7): error CS0246: The type or namespace name `Tuple' could not be found. Are you missing an assembly reference?
I found out that I can add inside tuple like this:
monsterPositions.Add(randomX, randomY);
Then the hardest part is how can I compare the x and y in my Tuple list. I am trying to use Contains
but I don't know what is wrong with it.
monsterPositions.Contains(Tuple(x, y));
What Are Unity C# Tuples? Tuples are just a simple C# data structure used to contain multiple variables. It’s some sort of structure, only that it’s declared inline and used with few variables. By using C# tuples, we aim to reduce code verbosity and increase readability.
The acronym ‘T’ represents multiple datatype that are specified while creating the tuple. The elements stored in tuple are numbered zero to seven, that is any normal tuple holds only 8 elements and if one tries to enter more than 8 elements, compiler throws an error. 2. Create Method C# provides a static Create method to create tuple as follows
The elements stored in tuple are numbered zero to seven, that is any normal tuple holds only 8 elements and if one tries to enter more than 8 elements, compiler throws an error. 2. Create Method C# provides a static Create method to create tuple as follows
In C#, methods are allowed to use tuple as a return type. Or in other words a method can return a tuple as shown in the below example: It is of reference type not of value type. It is limited to eight elements. Means you cannot store more than eight elements without nested tuple.
Pulling out @S.Akbari's comment on the question as I almost missed it when looking through:
Unity does not support
Tuples
.
Still the case in Unity 2017.3.1f1.
Other observations:
Vector2
or Vector2Int
(and the other numbers) if you're just storing float
s and int
s.Tuple
support, if you're using that o.OChange the player settings "API compatibility level" to ".NET 4.x":
This is an ancient post, but in aid of those who may stumble upon it, as of 2020 Unity C# supports built-in tuple construction in a new way.
If you don't need the small overhead of Vector2Int, you can use the following declaration:
private (int,int) monsterPositions = (randomX,randomY); //assuming <T>randomX,Y == int
These int tuples can be stored as coordinate pairs in arrays or hashSets, and can be accessed with simple search or .Contains functions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With