Quantcast
Channel: Questions in topic: "dynamically"
Viewing all articles
Browse latest Browse all 152

Removing null entries from a lists that is itself part of a list.

$
0
0
Hello, I have a grid in which the user can place items, and lists are created whenever items of the same type are placed next to each other. Due to the grid size, many of these lists are dynamically created, and then stored in another larger list. The items can be destroyed during play, and these need to be removed from the lists so the count can be updated. So basically, I need to remove null items from lists inside a list, but cannot do so with a for each method because it cause an enumeration error. Any suggestions appreciated, I'm sure the answer is simple and I just don't know what I'm doing. Thanks. Below is the full script requested. Im all too aware of the innefficent techniques used, but time restraint prevents me from rescripting. Update() contains the start of what I'm trying to implement. It will be moved to its own function, but update is easiest for testing. I want to check each List in listOfShipLists for empty values and remove them, so the count functions at the bottom of the script are accurate. Assuming count even counts empty values, in which case this whole thing is a waste of time and something else is wrong. using UnityEngine; using System.Collections.Generic; public class ArrayHolder : MonoBehaviour {. GameObject[] gridArray = new GameObject[64]; List> listOfShipLists = new List>(); void Start () { //adds grid spots to gridArray gridArray = (GameObject.FindGameObjectsWithTag("GridSpot")); } void Update () { foreach(List listRemove in listOfShipLists)//remove null entries. { } } public void shipPlaced(int x, int y, GameObject gridShip, string gridShipType) { List shipList; bool adjacentShipFound = false; foreach(GameObject item in gridArray) //Look at every element in grid { GridSquare currentSquare = item.GetComponent(); int x2,y2; //Read x2 y2 from item name x2 = currentSquare.X; y2 = currentSquare.Y; //Is this item adjacent to the just placed ship if(x2 == x+1 && y2 == y || x2 == x-1 && y2 == y || x2 == x && y2 == y+1 || x2 == x && y2 == y-1) { if(currentSquare.currentShipType == gridShipType) //FIXED: sees if it matches the type { //Find the list this found ship exists in already FindListForShip(gridShipType, gridShip); //Record we found something adjacentShipFound = true; } } } if (adjacentShipFound == false) { shipList = new List(); //creates new ship list shipList.Add(gridShip); // and adds ship to its own list listOfShipLists.Add (shipList);//Add this list to master list } } public void FindListForShip(string shipScript , GameObject gridShipCheck) { foreach(List item in listOfShipLists)//searches list of shiplists { foreach(GameObject item2 in item)//searches lists for gameobjects, uses strings from ship scripts to narrow search. { if(shipScript == "Ship1") { if(item2.GetComponent().shipType == gridShipCheck.GetComponent().shipType) { item.Add(gridShipCheck); } } if(shipScript == "Ship2") { if(item2.GetComponent().shipType == gridShipCheck.GetComponent().shipType) { item.Add(gridShipCheck); } } if(shipScript == "Ship3") { if(item2.GetComponent().shipType == gridShipCheck.GetComponent().shipType) { item.Add(gridShipCheck); } } if(shipScript == "Ship4") { if(item2.GetComponent().shipType == gridShipCheck.GetComponent().shipType) { item.Add(gridShipCheck); } } if(shipScript == "Ship5") { if(item2.GetComponent().shipType == gridShipCheck.GetComponent().shipType) { item.Add(gridShipCheck); } } else { return; } } } } public void fullSetCheck()//checks if any ship lists acn be launched { Debug.Log ("Checking Ship Lists"); foreach(List item in listOfShipLists) { Debug.Log ("Checking Ship Lists : A"); if(item.Count > 2)//number indicates how many ships must be in set to launch { Debug.Log ("Checking Ship Lists: B"); foreach(GameObject item2 in item) { Debug.Log("Checking Ship Lists: C"); item2.SendMessage("allowLaunch",SendMessageOptions.RequireReceiver); } } } } public void canLaunchCheck()//checks to see if ship groups are still large enough to launch { foreach(List item in listOfShipLists) { if (item.Count < 3) { foreach(GameObject item2 in item) { item2.SendMessage("disallowLaunch"); } } } } }

Viewing all articles
Browse latest Browse all 152

Trending Articles



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