- Home /
The question is answered, right answer was accepted
How to search a certain variable in a list of a created class (C#)
Wasn't to sure how to word the question so here's a description with code below.
I have a list which takes type of Room (a class i created) and this class has a contructor to create many instances of itself with 2 parameters one being a enum called Size and another being a Vector3 called position. What i want to do is make sure when i create a new room its not being created on another at the same position, which i've done sucessfully with many switch statements which yes i know doens't look the prettiest but i'll clean it up later. However I need to check and see if the list "Contains" this position in the Room the problem is the List is of type Room and I need to check a part in it.
Since I cant do if(!rooms.Contains(room.position)) and can only do if(!rooms.Contains(room)) which i dont know if its checking that position variable and am getting a infinite loop, which is why the while loop is marked out. Sorry if the description is poor if you have any questions leave a comment. And here is the code
Room class
public class Room {
public Size roomSize;
public Vector3 roomPos;
public Room(Size zRoomSize, Vector3 zRoomPosition)
{
roomSize = zRoomSize;
roomPos = zRoomPosition;
}
}
MapGenerator Class
-Part i'm having issues with
Room newRoom = new Room (newSize, newPos);
//What I want but can't do :(
if(!rooms.Contains (newRoom.roomPos))
//What i can do but doesn't work how i want because its checking the size also and I only want it to check the position
if(!rooms.Contains (new Room(newSize, newPos)))
{
rooms.Insert (0, newRoom);
}
-Whole code (Yes i'll clean it up later right now i just want it to work and i'm so close :) )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapGenerator : MonoBehaviour
{
public List<Room> rooms = new List<Room>();
public enum Size {small, medium, large};
public int maxRooms;
public GameObject smallRoom;
public GameObject mediumRoom;
public GameObject largeRoom;
void Start()
{
rooms.Add (new Room(Size.small, Vector3.zero));
GenerateMap ();
}
void GenerateMap()
{
//while(maxRooms > rooms.Count)
//{
SetNextRoom (rooms[0], 3);
//}
for(int i = 0; i < rooms.Count; i++)
{
GameObject roomObj = null;
switch (rooms[i].roomSize)
{
case Size.small:
roomObj = Instantiate (smallRoom, rooms[i].roomPos, Quaternion.identity) as GameObject;
break;
case Size.medium:
roomObj = Instantiate (mediumRoom, rooms[i].roomPos, Quaternion.identity) as GameObject;
break;
case Size.large:
roomObj = Instantiate (largeRoom, rooms[i].roomPos, Quaternion.identity) as GameObject;
break;
}
roomObj.name = "Room Number: " + i + " " + "RoomSize: " + rooms[i].roomSize + " RoomPosition: " + rooms[i].roomPos.ToString ();
roomObj.transform.SetParent (this.transform);
}
}
void SetNextRoom(Room previousRoom ,int maxPaths)
{
for (int i = 0; i < maxPaths; i++)
{
int randomIntDirection = Random.Range (0, 4);
int randomIntSize = Random.Range (0, 3);
int offset = 0;
Vector3 newPos = previousRoom.roomPos;
Size newSize = Size.small;
switch(randomIntSize)
{
case 0:
if(previousRoom.roomSize == Size.large)
{
offset = 50;
}
else if(previousRoom.roomSize == Size.medium)
{
offset = 40;
}
else
{
offset = 30;
}
newSize = Size.small;
break;
case 1:
if (previousRoom.roomSize == Size.large)
{
offset = 60;
}
else if (previousRoom.roomSize == Size.medium)
{
offset = 50;
}
else
{
offset = 40;
}
newSize = Size.medium;
break;
case 2:
if (previousRoom.roomSize == Size.large)
{
offset = 70;
}
else if (previousRoom.roomSize == Size.medium)
{
offset = 60;
}
else
{
offset = 50;
}
newSize = Size.large;
break;
}
switch (randomIntDirection)
{
case 0:
newPos.z += offset;
break;
case 1:
newPos.z += -offset;
break;
case 2:
newPos.x += offset;
break;
case 3:
newPos.x += -offset;
break;
}
Room newRoom = new Room (newSize, newPos);
//What I want but can't do :(
if(!rooms.Contains (newRoom.roomPos))
//What i can do but doesn't work how i want because its checking the size also and I only want it to check the position
if(!rooms.Contains (new Room(newSize, newPos)))
{
rooms.Insert (0, newRoom);
}
}
}
}
Answer by ShadyProductions · Jun 28, 2017 at 08:22 AM
You can use LINQ for this.
if(!rooms.Any(f => f.roomPos == newRoom.roomPos)) {
//If there is not any room in the rooms list, that has the same roomsize as newRoom then..
}
or
if(rooms.All(f => f.roomPos != newRoom.roomPos)) {
//If all rooms in the rooms list, don't have the same roomsize as newRoom then..
}
Never heard of LINQ what is it lol? just get
error System.Collections.Generic.List doesn't support a definition of Any()
is it like a refernce i have to add to using or what is it? :) glad you understood though
You have to add
using System.Linq;
Linq are basically a bunch of extension methods that can do alot of things while writing alot less code. https://docs.microsoft.com/en-us/dotnet/csharp/program$$anonymous$$g-guide/concepts/linq/getting-started-with-linq
Follow this Question
Related Questions
Comparing two lists to find the difference 1 Answer
A node in a childnode? 1 Answer
Access list storing custom class variables from another script 1 Answer
How to make a simple gui list 0 Answers
How to create a List containing all the different variables within a script (regardless of type)? 1 Answer