Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Dynamic enums

Discussion in 'Scripting' started by ashenvale, Jun 28, 2015.

  1. ashenvale

    ashenvale

    Joined:
    Aug 28, 2012
    Posts:
    7
    Hi,

    I have successfully built an enum dynamically into a dll, but for some reason when I try to use it I get the following error on the console,

    error CS0656: The compiler required member `someEnum.value__' could not be found or is inaccessible.

    I have searched the internet and can only find the following article

    http://stackoverflow.com/questions/...ld-value-of-dynamic-enum-from-c-sharp-net-2-0

    Unfortunately I don't want to use Mono-Cecil to solve it.

    The problem seems that the enum created have a field called value__ which is made private by default but it should really be made public to work correctly.
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,614
    Can you explain more about your use-case, your target platform, and the technique you're using to compile the enum?
     
  3. ashenvale

    ashenvale

    Joined:
    Aug 28, 2012
    Posts:
    7
    I am developing a dialogue tree editor. The user builds the tree then enums are generated for dialogues with more than 1 branch in them. These enums are used to select which dialogue to progress to next. I want to call a function where user can evaluate which branch to progress to next. This function is selectable to call through the editor.

    I am developing for pc at the moment but may develop it for other platforms.

    The following code is used to generate an enum.

    Code (CSharp):
    1. EnumBuilder eb = mb.DefineEnum(assemblyName + "." + preDialogueID + node.ID, TypeAttributes.Public, typeof(int));
    2.            
    3.             int enumIndex = 0;
    4.  
    5.             for (int i = 0; i < linkToNode.Count; ++i)
    6.             {
    7.                 DialogueTree.DialogueNode linkNode = linkToNode[i];
    8.                 eb.DefineLiteral(preDialogueID + linkNode.ID.ToString(), enumIndex++);
    9.             }
    10.        
    11.             // Create the type and save the assembly.
    12.             Type createdType = eb.CreateType();
    I can see on the dll created that there are enums defined in them with correct values. But unfortuntately I can't seem to use the enums.
     
  4. ashenvale

    ashenvale

    Joined:
    Aug 28, 2012
    Posts:
    7
    Ok I sorted it out. Instead of generating the dll I generate the source code. This is ideal as it will be cross platform.
     
    superpig likes this.