Quantcast
Channel: Questions in topic: "class instance"
Viewing all articles
Browse latest Browse all 131

How to search a certain variable in a list of a created class (C#)

$
0
0
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 rooms = new List(); 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); } } } }

Viewing all articles
Browse latest Browse all 131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>