- Home /
Scriptable Object Enum
Im trying to use Scriptable objects for dynmic enum ex : (Fire, Ice, Wind)
[CreateAssetMenu(fileName = "New Attack Item", menuName = "Menu/Enum/Enum Attack Item")]
[System.Serializable]
public class EnumAttackItem : ScriptableObject { }
I got an other scriptable object that list those objects :
public List<EnumAttackItem> EnumList;
How can i compare the scriptable object enum in code ???
Ex: I got a weapon with the attack type fire
How can i do :
if (MyWeaponAttack == FIRE)
// Code
Just like a real enum :
public Enum(Fire, Ice, Wind) AttackType
if (MyWeaponAttack == AttackType.Fire)
I dont want to hard code string ... i want the real type !
plz Help !!!
what exactly is fire ice and wind? i mean, your scriptableobject is emtpy, fire FIRE is he name of the scriptableobject? simply add an enum inside the scriptableobject and compare that enum
Fire ice and wind are the name of the scriptable object. If i add an enum inside my scriptable object it loses its ability to be dynamic from the designer if some one create a new scriptable object type... some one has to add it in code... Not shure whats the benefit to declare a enum inside scriptable object ??? You need to pass it to each gameobject after, just declare it in a class is better no ?
https://www.youtube.com/watch?v=6vmRwLYWNRo
Watch video at : 23:48
thx for help !
Answer by Bunny83 · Jun 29, 2020 at 11:32 PM
Well, you don't seem to get the point of using ScriptableObjects as enums. They are just empty objects and you can compare them. In order to compare them you have to have two references which you are going to compare. If you specifically know in code that you're going to have a "FIRE" value, you have to declare that in the script where you want to do the comparison and assign the FIRE asset to that variable. Something like that:
// assign your FIRE asset here
public EnumAttackItem FIRE;
// now you can do that comparison you have in your question:
if (MyWeaponAttack == FIRE)
However this does of course defies the point of having a "dynamic enum" value since your code demands that there is an instance FIRE. The main point of using ScriptableObjects as dynamic enum values is to completely decouple the code from the actual meaning of the value. The code shouldn't even know what values you may create in your project. Of course this depends on what you're actually trying to accomplish.
I highly recommend you watch the enum part of this brilliant Unite Talk (though if you have the time I can also recommend watching the whole talk).
The great thing about using ScriptableObjects as enum values is that you can add meta data or even functionality into the "enum value" itself. So you can tie in either behaviour or configuration data into the value itself so the code that is actually using your enum doesn't need to know what value we're talking about.
Your original code where you said if (MyWeaponAttack == FIRE)
is just plain procedural thinking. If you want to use SOs like shown in the video you have to think more in OOP terms and take a more abstract approach to whatever you want to actually solve.
Answer by williamfalvo · Jun 29, 2020 at 02:33 PM
If you want only to do
if(MyWeaponAttack == FIRE)
//Code
you can set a name of object and do an if statement like this
if((MyWeaponAttack.name == "Fire")
//Code
I don't like much this solution but for your challenge it works
Yea its already what im doing and i dont like it too.. i don't want to hard code string to compare them so like they say in the video im not shure its more dynamic than just code them in code im not understanding this approach... its simplify the designer job to add enum but for coder how we use those scriptable object in code. $$anonymous$$aking a dictionary of those enum and then make a foreach to parse all of them and compare sound like an idea but whats the gain ?
Refer to video :
https://www.youtube.com/watch?v=6vmRwLYWNRo Watch video at : 23:48
Your answer
Follow this Question
Related Questions
Allowing multiple selections on drop list via script inside inspector. 0 Answers
Character development with abilities 1 Answer
Using an enum inside of a scriptable Object - why don't I see it in the inspector? 1 Answer
Nested enum shows as int in Inspector 2 Answers
Changing Enum var order messes up Enums saved in Scriptable Objects. 2 Answers