- Home /
The question is answered, right answer was accepted
How would I get data out of a list of lists? (Made from a "List Wrapper")
Currently I am trying to basically create a "tag system" in which one object could take two objects which both have the script "Object Tag" and send their names to a script that would decide whether the first object could be built on top of the second object. (Think like "Indoors vs Outdoors" from Prison Architect for example)
Here's the code for a "String List Wrapper": using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable]
public class StringListWrapper
{
public string tagName;
public List<string> tagList;
}
The "tagName" refers to the tag of the structure the player is trying to build, wheras the "tagList" refers to the list of other objects that the player is allowed to build that structure on top of.
Here's the code for the "Tag Checker": using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Tags : MonoBehaviour {
public GameObject object1;
public GameObject object2;
public List<StringListWrapper> tags = new List<StringListWrapper>();
private void Start()
{
TagCheck(object1, object2);
}
public void TagCheck(GameObject gO1, GameObject gO2)
{
List<string> TagSet1 = gO1.GetComponent<ObjectTag>().tags;
List<string> TagSet2 = gO2.GetComponent<ObjectTag>().tags;
bool check = false;
for (int i = 0; i < TagSet1.Count; i++)
{
Debug.Log(tags[i]);
}
}
}
What I am hoping to do is find an item in the "tags" list from the "Tags" script using a string name. But what I have found is that each item in the "tags" list is simply a "StringListWrapper". (As you can see I did a debug of the "tags" index.
If you need anymore clarification just ask! Also, if you have a suggestion for a better way of reaching my eventual goal please, tell me.
Thank you, --- expat1999
Answer by Azrapse · Jan 17, 2017 at 06:41 AM
If I understood right, you want this:
Every structure describes itself with a "tag".
Every structure describes on top of which other structures it can be built with a collection of tags.
If you want this functionality in a Tags script, you could make it like this:
using System.Collections.Generic;
public class Tags: MonoBehaviour
{
public string tag;
public HashSet<string> canBeBuiltOnTopOf = new HashSet<string>();
public static bool CanBeBuiltOnTopOf(GameObject g1, GameObject g2)
{
var tags1 = g1.GetComponent<Tags>();
var tags2 = g2.GetComponent<Tags>();
if(tags1 == null || tags2 == null)
{
return false;
}
return tags1.canBeBuiltOnTopOf.Contains(tags2.tag);
}
}
Then you could use it like this:
bool allowed = Tags.CanBeBuiltOnTopOf(gameobject1, gameobject2);
Thank you for you're answer! :D
10/10 would stop being lazy and actually learn about HashSets/Tables again...
--- expat1999
No, you can use only List from namespace System.Collections.Generics. HashSet, Dictionary, Queue are not shown.
Figured out how to make use of the code sample above. It's definitely the winner.
Answer by bburtson09 · Jan 17, 2017 at 08:53 AM
In your loop you are iterating through your current class member tags List of type StringListWrapper
In your Loop if you change
Debug.Log (tags [i])
To:
Debug.Log (TagSet1[i])
It should return a string...
If you want to return the String field of your type StringListWrapper foreach item in your tag set You'll need to change debug log to:
Debug.Log (tags[i].tagName)
In hindsight I should have tried that already... Lol...
I'll test it out as soon as I can.
UPDATE: It's seems to work... still tinkering...
--- expat1999
O$$anonymous$$ messed around with this a bit. It got rather messy, so I'm going with the Hashsets above, but it was good to learn about the idea of "wrapping".
Follow this Question
Related Questions
How to create a list from Children with different variables 1 Answer
Convert tags to list, then back to array 1 Answer
Using similar to GameObject.FindObjectsWithTag but for tag.contains. 0 Answers
How to not have a function affect a prefab? 2 Answers
Find GrandChildren of Grandparent Gameobject by tag. 1 Answer