- Home /
Instantiate scriptable objects with optional amount of specified parameters
I have a fruit object:
public class Fruit {
public string fruitName;
public string family;
public string species;
public List<string> tastes = new List<string>();
public List<string> behaviour = new List<string>();
public Fruit (string newName, string newFamily, string newSpecies) {
fruitName = newName;
family = newFamily;
species = newSpecies;
}
}
I have a fruit controller to instantiate fruits:
public class FruitController : MonoBehaviour {
public List<Fruit> fruits = new List<Fruits>();
void Awake (){
fruits.Add (new Fruit ("Apple", "Rosaceae", "Malus Domestica"));
fruits[0].tastes.Add ("Sweet", "Sour", "Bitter", "Earthy");
fruits[0].behaviour.Add ("Happy", "Passive-Aggressive", "Cromulence");
}
}
Is there any way of instantiating fruit within a single line and not having to continually add to the 'tastes' and 'behaviours' lists of each fruit following instantiation? Is there a better way than using lists entirely? Ideally, I would want to be able to write as little or as many of these attributes when instantiating fruit, but only in a single line. For example:
//Parameters: fruitName, family, species, taste1, taste2, taste3, taste4, behaviour1, behaviour2, behaviour3
fruits.Add (new Fruit ("Apple", "Rosaceae", "Malus Domestica", "Sweet", "Sour", "Bitter", "Earthy", "Happy", "Passive-Aggressive", "Cromulence"));
//Parameters: fruitName, family, species, taste1, behaviour1, behaviour2
fruits.Add (new Fruit ("Pomegranate", "Lythraceae", "Punica Granatum", "Bloody", "Flatulent", "Destructive"));
As you can see, the amount of entries differ between the two fruits in the two parameters of tastes and behaviours. Obviously, this doesn't work as is, since there's no differentiation between tastes and behaviours in the original Fruit class -unless I specifically overload the constructor, but this would again limit how many of each taste or behaviour I can add via instantiation- and the compiler has no idea what I'm doing. I'm just wondering if there's some implementation that allows for this sort of thing. I'd also be open to suggestions for not using Lists for these parameters, but they seem to work as is, albeit with the annoyance and potentially gargantuan task of having to write thousands of lines of code for insertion of said unknown amount of attributes, and is the only thing I can really think of. I would like many fruits by the end of it.
Thanks.
You can have Lists(or better HashSets for performance of Contains) of all possible variants and then check each string received from constructor public Fruit(string newName, string newFamily, string newSpecies, params string[] values)
if they are tastes or behaviours or anything else using something like allTastes.Contains(values[a])
. params keyword allows any amount of parameters to be received as an array ins$$anonymous$$d.
Answer by PseudoDev · Mar 08, 2016 at 06:47 PM
Thanks for the answer and apologies for the lateness of my reply.
I found these topics with similar functionality:
optional parameter string , Adding Multiple Elements to a List on One Line
It looks like params is what I need.
Edit: I have no idea as to why I can't pick Teravisor's reply as the answer. I can 'convert' my reply to his as the answer. Very strange.
Your answer
Follow this Question
Related Questions
GUI Placement Question. 1 Answer
accessing custom url parameters on android 1 Answer
Unityscript how to change parameters value 1 Answer
Reflection - passing parameters to an invoked function 2 Answers
space wrap 3D 1 Answer