Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuple of 2 Int and contain method in Unity C#

Tags:

c#

tuples

unity3d

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));
like image 435
chazefate Avatar asked Oct 24 '17 06:10

chazefate


People also ask

What are unity tuples and how to use them?

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.

What is T in tuple in C?

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

How many elements are stored in tuple in C++?

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

Can a method return a tuple in C?

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.


Video Answer


3 Answers

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:

  • You can use Vector2 or Vector2Int (and the other numbers) if you're just storing floats and ints.
  • I noticed that UniRx has Tuple support, if you're using that o.O
  • Best follow @Jon Skeet's advice and make your own class or struct otherwise, or like he mentioned, to implement equality if necessary.
like image 115
Bilal Akil Avatar answered Oct 15 '22 16:10

Bilal Akil


Change the player settings "API compatibility level" to ".NET 4.x":

Change API compatibility level to .NET 4.x

like image 38
Ariel Avatar answered Oct 15 '22 14:10

Ariel


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

like image 41
hexelpup Avatar answered Oct 15 '22 14:10

hexelpup