Skip to content

Proposed new SimObject class system. #18

@lukaspj

Description

@lukaspj

Hey, I've been learning a bit more about C# and I have a new proposal for the SimObject system, to replace this syntax:

SimObject sim = 42;
sim["Fish"] = 2;
Console.WriteLine(sim["Fish"] + sim.mID);
SimObject sim = (dynamic)new TorqueID(42);
sim.Dynamics.Fish = 2;
Console.WriteLine(sim.Dynamics.Fish + sim.mID);

It's more verbose but I think it's clearer what happens. Also, this has the advantage of not having to create implicit converters for each of the inherited classes.

Using:

   class TorqueID : DynamicObject
   {
      private int mID;

      public TorqueID(int ID)
      {
         mID = ID;
      }

      public override bool TryConvert(ConvertBinder binder, out object result)
      {
         if (binder.Type.GetConstructor(new Type[] { typeof(int) }) != null)
         {
            result = Activator.CreateInstance(binder.Type, mID);
            return true;
         }
         result = null;
         return false;
      }
   }

Makes the TorqueID class implicitly convert to any class with a constructor of format: "public C(int id)" which it's fair to assume that each SimObject will have. Which means the boiler plate code for SimObject is nothing more than:

   class SimObject
   {
      public int mID;
      public dynamic Dynamics;

      public SimObject(int ID)
      {
         mID = ID;
         Dynamics = new DynamicVariables(mID);
      }
   }
   class DerivedSimObject : SimObject
   {
      public DerivedSimObject(int ID) 
         : base(ID)
      {
      }
   }

Ofc, this introduces dynamics which means types will be evaluated at run-time. But this makes sense when you consider that it's modelled to match TorqueScript. You can find a Gist of a minimal running example here.

I'm not completely done with rethinking the SimObjects, I'm gonna look at object instancing next.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions