- Home /
Get enum value out of function c#
Hey, I have created custom editor field function and I'm trying to get the get enum value out of the function with the "out" keyword. I seem to be all ok with all the different types but I can't seem to get enum out. The function is generic and enum type can be of anything. I'm currently able to get it out by simply changing the return type to Enum but I would like to learn the "out" way too to keep the code consistency all the way same and sometimes I need to return multiple values which "return" doesn't allow instantly.
Enum EnumTextField(string field, string text , out string textOut, Enum @enum, GUIStyle style)
{
GUILayout.BeginHorizontal(field, style);
textOut = EditorGUILayout.TextField(text);
@enum= EditorGUILayout.EnumPopup(@enum);
GUILayout.EndHorizontal();
return @enum;
}
Simplified the function for easier readability. When I want to use that function I just call:
NF.enterType = (NodeFollow.TriggerEnterType)EnumTextField("Trigger Name", NF.triggerName, out NF.triggerName, NF.enterType, style);
So, my question is how to get the enum value out with the C# "out" keyword ? ? ? In format like:
EnumTextField("Trigger Name", NF.triggerName, out NF.triggerName, NF.enterType, out NF.enterType, style);
I'm not familiar with structs other than that they work similarly to classes but I don't really see how it would help here as the Enum way seems to be pretty simple. Thanks for help thought.
the advantage of struct is if you only need them temporary (like in this case you only need to pass values between functions), then in most cases, struct won't generate garbage memory over time.
Answer by Bunny83 · Oct 09, 2019 at 10:33 AM
The function is generic
Uhm, no it's not a generic method. At least not in the sense of generics in C#. You need to actually make a generic method.
Out or ref parameters are kinda restricted when it comes to inheritance. While passing in a more derived variable to a less derived parameter is usually possible, it's not possible for ref or out parameters since the assignment goes the other way round. Inside the method you essentially have a less derived variable. So you could assign any derived type to that variable. However since out parameters are just an alias for the outside variable that would mean you could assign values to the outer variable that it can't hold.
The special Enum type is a bit tricky. Technically an enum is just an integral numeric type (int, byte, ....). Though it's also "derived" from the special struct Enum. There's no real inheritance going on here. The Enum type is just a way to fit the special behaviour of enums into the type system.
As you can read here on SO the latest C# version (7.3+) does now support Enum as a generic constraint. Though it's possible to simply use an unconstraint generic parameter instead. Unfortunately since we currently can not use Enum as generic constraint, we have to actually cast it to object in order to get it to properly cast between Enum and "T". Something like this should work:
public static void EnumTextField<T>(string field, string text, out string textOut, ref T @enum, GUIStyle style) where T:struct
{
GUILayout.BeginHorizontal(field, style);
textOut = EditorGUILayout.TextField(text);
@enum = (T)(object)EditorGUILayout.EnumPopup((System.Enum)(object)@enum);
GUILayout.EndHorizontal();
}
Note that we have to use a ref parameter instead of an out parameter since you have to read the incoming value for the EnumPopup. out parameters can not be read inside the method since they are only supposed to output a value.
This is exactly what I needed. Thank you. Also, I was about to write on main post as of not native english speaker, I might have got some of the word meanings and sayings wrong. A lot of computer language here. :)
Answer by cdr9042 · Oct 09, 2019 at 10:23 AM
the comment is a bit deep now so I'll write an example of using struct here:
public struct Damage
{
public float value;
public Type damageType;
public enum Type { blunt, slash };
}
public class Actor
{
public float hp = 500;
public void GetDamageData(out Damage damage)
{
damage.value = 10;
damage.damageType = Damage.Type.blunt;
}
}
then you can use them like class. if you manually set all the value, then struct doesn't need to use new () to make an instance
Actor attacker = new Actor();
Actor defender = new Actor();
Damage damageData;
attacker.GetDamageData(out damageData);
defender.TakeDamage(damageData);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Function that returns instantiated object 1 Answer
code path returning error 2 Answers
C# Return Type Error? 1 Answer