- Home /
Getting an "Enum" value from a Scriptable Object
Hi! I'm trying to "get" the value of an enum variable using a property getter named "VisumBlueprintChallengeMode" inside a scriptable object so I can "set" it to an "Enum" variable named "challenge".
Here's my code where the error exists (Look for "TODO: Fix Error")
using System;
using BaesickEntertainment.Scriptables;
using UnityEngine;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine.UIElements;
namespace BaesickEntertainment.Editor.Consilio.Panels
{
/// <summary>
/// Visum Creator is a level-creator editor where users can create their own "visum" (or stage).
/// </summary>
public class VisumCreator
{
// Cached References
private VisumBlueprint _visumBlueprint;
private VisumBlueprintEventSequence _visumBlueprintEventSequence;
private EventSequenceEditor _eventSequenceEditor;
#region Visum Blueprint Creator
[PropertyOrder(1)]
[BoxGroup("VISUM BLUEPRINT CREATOR")]
public string visumBlueprintName = "visum blueprint 101";
[PropertyOrder(1)]
[BoxGroup("VISUM BLUEPRINT CREATOR")]
[PropertyTooltip("unique visum blueprint identifier.")]
[ReadOnly]
public int visumBlueprintId = 0;
[PropertyOrder(1)]
[BoxGroup("VISUM BLUEPRINT CREATOR")]
[Button(ButtonSizes.Large)]
private void CreateNewVisumBlueprint()
{
_visumBlueprint = ScriptableObject.CreateInstance<VisumBlueprint>();
// Create a new visum blueprint inside the project folder (temporarily).
// TODO: Store the visum blueprint inside the Firebase to support user-generated levels
AssetDatabase.CreateAsset(_visumBlueprint, "Assets/Commons/Scriptables/Visum Blueprints/" + visumBlueprintName + ".asset");
AssetDatabase.SaveAssets();
_visumBlueprint.VisumBlueprintName = visumBlueprintName;
_visumBlueprint.VisumBlueprintId = visumBlueprintId;
visumBlueprintId += 1;
}
#endregion
[Space]
#region Visum Blueprint Editor
[PropertyOrder(2)]
[Title("VISUM BLUEPRINT EDITOR")]
[InfoBox("SELECT A VISUM BLUEPRINT TO EDIT", InfoMessageType.Warning, nameof(OnSetVisumBlueprint))]
[AssetSelector]
[OnValueChanged(nameof(CheckIfVisumBlueprintIsSet))]
public VisumBlueprint visumBlueprint;
// Invoke when a visum blueprint is set on the editpr
private bool OnSetVisumBlueprint()
{
bool isVisumBlueprintNotSet;
isVisumBlueprintNotSet = visumBlueprint == null;
return isVisumBlueprintNotSet;
}
private bool IsVisumBlueprintSet()
{
bool isVisumBlueprintSet;
isVisumBlueprintSet = visumBlueprint != null;
return isVisumBlueprintSet;
}
#endregion
#region Stage Details
[EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
[PropertyOrder(3)]
[Title("BASIC INFORMATION")]
[LabelText("Blueprint Name")]
public string blueprintName;
[EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
[PropertyOrder(3)]
[ReadOnly]
[LabelText("Unique ID")]
public int blueprintId;
private void CheckIfVisumBlueprintIsSet()
{
// Load visual blueprint's editor values HERE
if (visumBlueprint != null)
{
blueprintName = visumBlueprint.VisumBlueprintName;
blueprintId = visumBlueprint.VisumBlueprintId;
blueprintDescription = visumBlueprint.VisumBlueprintDescription;
blueprintLoadingSplashImage = visumBlueprint.VisumBlueprintLoadingSplashImage;
// TODO: FIX THIS --->>> challenge = visumBlueprint.VisumBlueprintChallengeMode;
visumBlueprintEventSequence = visumBlueprint.VisumBlueprintEventSequence;
}
else
{
blueprintName = String.Empty;
blueprintId = 0;
}
}
[EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
[PropertyOrder(3)]
[LabelText("Description")]
[PropertyTooltip("A short overview of what the blueprint is all about")]
public string blueprintDescription;
[Space(10)]
[EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
[PropertyOrder(4)]
[Title("MISCELLANEOUS")]
[LabelText("Loading Splash Image")]
[PropertyTooltip("The image to be shown in the loading screen before the stage start. (E.g: Trivia, Tip, Stage Detail)")]
public Sprite blueprintLoadingSplashImage;
#endregion
[Space]
#region Game Mode
[EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
[PropertyOrder(5)]
[Title("LEVEL CONFIGURATION")]
[InfoBox("CHOOSE A CHALLENGE", InfoMessageType.Error, "OnChallengeReferenceUpdate")]
[InlineProperty, EnumPaging]
[LabelText("Challenge Mode")]
public Challenges challenge;
public enum Challenges
{
None, Freestyle, TimeAttack, Survival
}
private bool OnChallengeReferenceUpdate()
{
bool isChallengeSetToNone;
isChallengeSetToNone = challenge == Challenges.None;
return isChallengeSetToNone;
}
#endregion
#region Visum Blueprint Event Sequence
[EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
[PropertyOrder(6)]
[InlineButton(nameof(CreateNewVisumBlueprintEventSequence), "Create New Event Sequence")]
[InlineButton(nameof(OpenVisumBlueprintEventSequenceEditor), "Edit")]
public VisumBlueprintEventSequence visumBlueprintEventSequence;
private void CreateNewVisumBlueprintEventSequence()
{
_visumBlueprintEventSequence = ScriptableObject.CreateInstance<VisumBlueprintEventSequence>();
// Create a new visum blueprint inside the project folder (temporarily).
// TODO: Store the visum blueprint inside the Firebase to support user-generated levels
AssetDatabase.CreateAsset(_visumBlueprintEventSequence, "Assets/Commons/Scriptables/Events Sequence/" + blueprintName + "(Event Sequence).asset");
AssetDatabase.SaveAssets();
visumBlueprintEventSequence = _visumBlueprintEventSequence;
}
private void OpenVisumBlueprintEventSequenceEditor()
{
EventSequenceEditor.OpenWindow();
_eventSequenceEditor.visumBlueprintEventSequence = visumBlueprintEventSequence;
}
#endregion
#region Visum Blueprint Processors
[EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
[PropertyOrder(6)]
[HorizontalGroup("Visum Blueprint Processors")]
[Button(ButtonSizes.Large)]
[PropertySpace(SpaceAfter = 5)]
private void SaveVisumBlueprint()
{
visumBlueprint.VisumBlueprintName = blueprintName;
visumBlueprint.VisumBlueprintDescription = blueprintDescription;
visumBlueprint.VisumBlueprintLoadingSplashImage = blueprintLoadingSplashImage;
visumBlueprint.VisumBlueprintChallengeMode = challenge;
visumBlueprint.VisumBlueprintEventSequence = visumBlueprintEventSequence;
AssetDatabase.SaveAssets();
}
[EnableIf(nameof(IsVisumBlueprintSet), Value = true)]
[PropertyOrder(6)]
[HorizontalGroup("Visum Blueprint Processors")]
[Button(ButtonSizes.Large)]
[PropertySpace(SpaceAfter = 5)]
private void ClearVisumBlueprintToDefault()
{
visumBlueprint.VisumBlueprintName = String.Empty;
blueprintDescription = String.Empty;
blueprintLoadingSplashImage = null;
challenge = Challenges.None;
}
#endregion
}
}
Here's the script for my Scriptable Object:
using System;
using Sirenix.OdinInspector;
using UnityEngine;
namespace BaesickEntertainment.Scriptables
{
public class VisumBlueprint : ScriptableObject
{
#region Configuration Parameters
private string _visumBlueprintName;
private int _visumBlueprintId;
private string _visumBlueprintDescription;
private Sprite _visumBlueprintLoadingSplashImage;
private Enum _visumBlueprintChallengeMode;
private VisumBlueprintEventSequence _visumBlueprintEventSequence;
#endregion
#region Properties
[Title("BASIC INFORMATION"), LabelWidth(150), LabelText("Name")]
[ShowInInspector, ReadOnly]
public string VisumBlueprintName
{
get => _visumBlueprintName;
set => _visumBlueprintName = value;
}
[ShowInInspector, ReadOnly, LabelWidth(150), LabelText("Blueprint ID")]
public int VisumBlueprintId
{
get => _visumBlueprintId;
set => _visumBlueprintId = value;
}
[ShowInInspector, ReadOnly, LabelWidth(150), LabelText("Description")]
public string VisumBlueprintDescription
{
get => _visumBlueprintDescription;
set => _visumBlueprintDescription = value;
}
[Title("MISCELLANEOUS"), LabelWidth(150), LabelText("Loading Splash Image")]
[ShowInInspector, ReadOnly]
public Sprite VisumBlueprintLoadingSplashImage
{
get => _visumBlueprintLoadingSplashImage;
set => _visumBlueprintLoadingSplashImage = value;
}
[Title("LEVEL CONFIGURATION")]
[ShowInInspector, ReadOnly, LabelWidth(150), LabelText("Challenge Mode")]
public Enum VisumBlueprintChallengeMode
{
get => _visumBlueprintChallengeMode;
set => _visumBlueprintChallengeMode = value;
}
[ShowInInspector, ReadOnly, LabelWidth(150), LabelText("Event Sequence")]
public VisumBlueprintEventSequence VisumBlueprintEventSequence
{
get => _visumBlueprintEventSequence;
set => _visumBlueprintEventSequence = value;
}
#endregion
}
}
Here's the error I'm getting: Assets\Scripts\Editor\Consilio\Panels\VisumCreator.cs(111,29): error CS0266: Cannot implicitly convert type 'System.Enum' to 'BaesickEntertainment.Editor.Consilio.Panels.VisumCreator.Challenges'. An explicit conversion exists (are you missing a cast?)
Answer by ChronoWalker · Jul 08, 2020 at 06:19 AM
You made VisumBlueprintChallengeMode an Enum, so variable "_visumBlueprintChallengeMode" should be declared as
private VisumBlueprintChallengeMode _visumBlueprintChallengeMode;
Answer by Lyrcaxis · Jul 08, 2020 at 05:07 AM
The image isn't playing, but I imagined you want something in this context:
public enum Challenges { None, Freestyle, TimeAttack, Survival }
// The base class object
public abstract class EnumWrapper<T> : ScriptableObject where T : System.Enum {
public T value;
}
[CreateAssetMenu(menuName = "Blueprints/Challenge")]
public class ChallengeVisumBlueprint : EnumWrapper<Challenges> {
// Potential additional settings here
}
public class MyChallenge : MonoBehaviour {
public ChallengeVisumBlueprint challenges;
void Awake() {
switch (challenges.value) {
case Challenges.None:
break;
case Challenges.Freestyle:
break;
case Challenges.TimeAttack:
break;
case Challenges.Survival:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
I taught getting enum values on SO are similar from getting other values on types like string or in (like this: scriptableObjectName.variableYouWantToGet) but it won't let me: Assets\Scripts\Editor\Consilio\Panels\VisumCreator.cs(111,29): error CS0266: Cannot implicitly convert type 'System.Enum' to 'BaesickEntertainment.Editor.Consilio.Panels.VisumCreator.Challenges'. An explicit conversion exists (are you missing a cast?)
Your answer
Follow this Question
Related Questions
Get data from Server 0 Answers
inheritance from a ScriptableObject 2 Answers
Use Enum in an if statement from a different script 1 Answer
How to get data from Scriptable Objects without copy-pasting code 1 Answer
[C#] How to make this code toggleable 3 Answers