Hello,
I have multiple public Lists in a scriptable object and I want to call them dynamicly with their name.
Something like this :
string nameOfTheList;
var variable=myScriptableObject.nameOfTheList[index];
How can I do that ?
Thanks a lot.
Hello,
I have multiple public Lists in a scriptable object and I want to call them dynamicly with their name.
Something like this :
string nameOfTheList;
var variable=myScriptableObject.nameOfTheList[index];
How can I do that ?
Thanks a lot.
Make them static public and then you can call them directly. This means there only is one copy.
For scriptable objects I don’t really see the use of this though. What do you want to do?
I want to make footstep sound for my avatar : detection of the type of ground (script on the mesh ground) + detection of the collision trigger on each foot + scriptable object with audio clip database.
I wanted to have the scriptable object with lists of Audioclips named by type of ground (“Stone”…), the script on the ground with the string name of the type and the script of the foot which plays a random sound of the list with the name on the ground.
I have found a very much simplier solution : I create a scriptable object with a list of sounds and I create one version for each type of ground. My script on the ground has a variable of a scriptable object and my foot script just put this scriptable object to have the good list.
But I would really have wanted to know how to call a list with the string of its name.
I think you’re looking for the nameof system?
It’s better to make a dictionary with the name being the key and then you get an array as value
Convert the strings to numbers and use an array. In other words, instead of using Sounds."grass".getmoreData, use Sounds[1].getMoreData, where you know 1 is the number for grass. Possibly have the numbers be an enumerated type, which lets you actually write GRASS (with no quotes) in the code.
Suppose you made your ScriptableObjects from your class named FootStep_t. Each of your ScriptableObjects is for a certain type of terrain, right? The trick is to group them together somehow, and an array is the easiest way. Use public FootStep_t[ ] AllFootSteps; to get an array of FootStep_'s (the SO’s you made). Then size it to 4 in the Inspector, and drag in your 4 ScriptableObjects. Now you’ve got them grouped, and easy to look-up using 0,1,2 or 3.
Then make is so your grass terrain doesn’t say “grass” anywhere. Instead it knows that it’s terrainType:1, and so on for stone terrains being terrainType:2. Let’s say you have the current terrainType, which we made into a number from 0 to 3, in terrainNum. Get the correct ScriptableObject using AllFootSteps[terrainNum]. As a double-check, if terrainNum is 1, that’s AllFootSteps[1], which is where you dragged in the Grass one.
The only drawback is remembering “1” is grass, “2” is stone … . An old trick is to use an enumerated type – it can make DIRT=0, GRASS=1, STONE=2, and so on. Now you can set your terrain #'s with a drop-down that actually says GRASS or DIRT. It’s still really just 0 to 3, so the array look-up works the same, but it’s easier for you to read as a human.
You’re probably looking for a Dictionary
var soundsDictionary = new Dictionary<string, AudioClip[]>();
soundsDictionary["Grass"] = new[] { grassFoot1, grassFoot2, grassFoot3 };
soundsDictionary["Wood"] = new[] { woodFoot1, woodFoot2 };
soundsDictionary["Snow"] = new[] { snowFoot1, snowFoot2 };
var grassSounds = soundsDictionary["Grass"];
BUT:
The solution you chose (ScriptableObjects + Direct references) is the way to go. (no magic strings, faster, and editor friendly)
Thank you guys !