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

How can I create a TypedReference?

Discussion in 'Scripting' started by Rick-Gamez, Aug 25, 2017.

  1. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    Hey guys, I'm back to working on my serialization engine and I'm having trouble with creating a typed reference.

    Let me start with a NOTE: Normally my engine will build pre defined serialization instructions in the editor when the AssetDatabase refreshes for types that are marked with my custom serializable attribute. This means that the engine can serialize values even if they are not marked as System.Serializable. I'm not going to go into the background of why this function and attribute exist because it would take too long to explain. This is just a single feature of the engine.

    However, in order to get the feature to work it needs to be able to set field values in structures. I was going to do this using GetField().SetValueDirect() but I am not sure how to express the struct as a TypedReference as required by the SetValueDirect method's obj parameter.

    Other Additional Info :
    1. The struct I'm testing on is a Vector4 but comes back to the engine as object upon deserialize.
    2. The engine extends the option to allow for building of serialization instructions at run time (so they only have to build once per play session) which is how Vector4 is being tested without my attribute. (This feature is only available if the user (programmer) selects it in the engine settings).

    Conclusion :
    How can I make a TypedReference for a struct? Thanks!
     
    CPlusSharp22 likes this.
  2. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    Okay here's where I'm trying to create the TypedReference :
    Code (csharp):
    1.  
    2. var tr = System.TypedReference.MakeTypedReference(val, val.GetType().GetFields(_flgs));
    3.  
    val currently represents a vector4.
    I keep getting the NotImplemmented exception.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Is it MakeTypedReference that's throwing the exception?

    It might be that this is due to the old Mono version, You could try the experimental new one in 2017.1 and see if it' supported there.
     
    Rick-Gamez likes this.
  4. Rick-Gamez

    Rick-Gamez

    Joined:
    Mar 23, 2015
    Posts:
    218
    Yes, MakeTypedReference is throwing it because if I disable that line of code above the exception goes away.

    maybe there's another way to A. get a typed reference or B. to set field value in a struct? (This is a non issue in a class so I know the instruction progression is correct) And remember the struct is coming back upon deserialize as System.Object.

    Judging by the fact that if I input a new FieldInfo[0] as the second parameter then the exception will instead say "the parameter "flds" is empty" (or something to that effect), I am assuming that the method is supported. Just a thought.
     
    Last edited: Aug 25, 2017
  5. MrDropC

    MrDropC

    Joined:
    Aug 26, 2017
    Posts:
    1
    Hi, in case you (or anyone else) were still wondering how to do this -- I've had to do this once before, and succeeded through use of the __makeref keyword. See below for example on how to employ the keyword for this purpose:

    Code (CSharp):
    1. // Given:
    2. Vector4 yourStruct;
    3. FieldInfo yourStructField; // Just pretend this refers to the vector's x-component field.
    4. float valueToAssign; // Let's say we want to assign this to the field mentioned above.
    5.  
    6. // How to do it:
    7. TypedReference yourStructRef;
    8.  
    9. yourStructRef = __makeref(yourStruct); // Looks funky, but "__makeref" is actually a built-in C# keyword.
    10. yourStructField.SetValueDirect(yourStructRef, valueToAssign); // You can now assign values in such a way.
    11.  
    Hope this does it for you.
     
  6. snoopyuj

    snoopyuj

    Joined:
    May 22, 2017
    Posts:
    13
    SetValueDirect also get the NotImplemmented exception :(
     
  7. LoxodonStudio

    LoxodonStudio

    Joined:
    Nov 1, 2016
    Posts:
    38

    Hi, you can create a TypedReference with the "__makeref" keyword, but IL2CPP does not support TypedReference .
     
    CPlusSharp22 and jilleJr like this.