Start a new topic
Answered

Struct support in game engine

I have this scenario:


public struct Quaternion : IEquatable<Quaternion>

 {

  /// <summary>

  /// Specifies the X-value of the vector component of the Quaternion.

  /// </summary>

  public float X;

  /// <summary>

  /// Specifies the Y-value of the vector component of the Quaternion.

  /// </summary>

  public float Y;

  /// <summary>

  /// Specifies the Z-value of the vector component of the Quaternion.

  /// </summary>

  public float Z;

  /// <summary>

  /// Specifies the rotation component of the Quaternion.

  /// </summary>

  public float W;

  public bool Equals(Quaternion other)

  {

   return (X == other.X &&

     Y == other.Y &&

     Z == other.Z &&

     W == other.W);

  }

 }


What i get is:

Structure must be immutable, field must be read-only


How would I solve this?

Taking look at System.Numerics.Vectors from corefx they have same approach, any thought?


Best Answer

   

Use [JsAttribute(StructIsImmutable = false)]

/// <summary>
/// If <c>true</c> specifies that this structure is immutable by design, no copy/clone will be done by the compiler.
/// If <c>false</c> specifies that it can be mutated and therefore needs to be copied/cloned whenever .NET would create a copy of a value type.
/// Targets: structures only.
/// </summary>
public bool StructIsImmutable { get; set; } 

   


Since structs are not present in JS world only classes, all structs are passed by reference, thus we require "immutable" structs.


You have 3 solutions:
1) the safiest one and recommended by many patterns will be made all fields readonly, public readonly float X, and etc. of course add .ctor(x, y, z, w) that assign given values to fields.


2) convert this struct to class, we also suggest introduce Clone() method to clone this class before passing to others


3) only if you sure that nobody else can change values of struct instance in JS context you can add this attribute on the struct [Js(StructIsImmutable = true)], DuoCode compiler will compile such struct.

This is good, I understand about readonly pattern but strange why Microsoft did not adopt this pattern too, would be

otherwise possible to compile struct like C# does?

As I graphics/game developer, I have to say that it is a convention to implement vector/matrix/quaternion/bounds with struct.


There have been quite a lot existing implementations out there using struct, it is impossible to either "introduce Clone() method to clone this class before passing to others" or "mark them as immutable explicitly"(since they are NOT immutable).


Therefore I think it is a must to have the compiler/translator take care of this, especially considering there is already some other c#-to-js compiler got this feature implemented.



This feature is implemented and will be available in DuoCode 1.4.

Answer

   

Use [JsAttribute(StructIsImmutable = false)]

/// <summary>
/// If <c>true</c> specifies that this structure is immutable by design, no copy/clone will be done by the compiler.
/// If <c>false</c> specifies that it can be mutated and therefore needs to be copied/cloned whenever .NET would create a copy of a value type.
/// Targets: structures only.
/// </summary>
public bool StructIsImmutable { get; set; } 

   

Login or Signup to post a comment