- Home /
Is it possible to choose from preinitialized lists in the inspector?
I'm using lists to keep track of 'scenes' in various locations that the player can randomly encounter. Please forgive the amateur code, I'm sure there's tons of things I could be doing better. I initialize the lists with this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SetStartingLists : MonoBehaviour
{
public static List<string> townList = new List<string>();
public static List<string> forestList = new List<string>();
void Awake()
{
townList.Add("townScene1");
townList.Add("townScene2");
forestList.Add("forestScene1");
}
}
And choose which scene to set active with this, while also removing the chosen scene from the current list:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Fungus;
[CommandInfo("Pick",
"Pick Scene",
"Pick and remove scene from selected list")]
public class PickScene : Command
{
public List<string> currentList = SetStartingLists.townList;
public Flowchart flowchart;
public override void OnEnter()
{
int rNumber = (Random.Range(0, currentList.Count));
flowchart.ExecuteBlock(currentList[rNumber]);
SetStartingLists.history.Add(currentList[rNumber]);
currentList.RemoveAt(rNumber);
Continue();
}
}
This works fine so far, but it'd be great if I could select which list to edit from the inspector. In the above, only townList would be available. I've tried things like:
public string targetList;
currentList = targetList;
But this clearly doesn't work. Is what I'm asking for possible? Apologies if there's an obvious solution I've missed.
Answer by georgelewising · Aug 07, 2019 at 11:30 AM
References are fixed at compile time so you won't be able to do that. You could potentially use reflection to recover the list flexibly by name / index - but that's not the best of ideas - it doesn't promote a particularly healthy architecture.
You should instead consider a different solution. One option might be to combine an Enum & Dictionary Data Structure.
For example:
public enum Location
{
Town,
Forest,
Undefined
}
Store the data in SetStartingLists.cs:
public static Dictionary< Location, List< string > > LocationDictionary;
Define the key in the "PickScene.cs" inspector:
public Location currentSceneLocation;
And then access it during "PickScene.cs" runtime:
List< string > town = SetStartingLists.LocationDictionary[ currentSceneLocation ];
Thank you for the assist. $$anonymous$$y code still isn't working exactly the way I would like it to, but this definitely steers me into the right direction. Again, thank you.