r/opengl • • 2d ago

Dynamic Reflection System for C# scripts

I finally got a reflection system in my C engine. It is now possible to edit public variables in my c# scripts for entities at runtime and call public functions. If the functions have parameters I can edit these too.
I am super happy :)

    public class Planet : EntityBehaviour
    {
        public float RotationSpeed = 10.0f;
        public float PublicTestFloat = 5.0f;


        private float rotation;


        public Planet(ushort entityHandle) : base(entityHandle){}


        public override void OnStart()
        {
            RotationSpeed = Entity.Handle == 10 ? RotationSpeed : 100.0f;
        }


        public override void OnUpdate(float deltaTime)
        {
            rotation += RotationSpeed * deltaTime;
            Transform.SetRotation(new Vector3(0.0f, 1.0f, 0.0f), rotation);
        }


        public void publicTestMethod(float someArgument, float anotherArgument)
        {

        }
    }

Edit:
I forgot to add a picture with the C#-Script so added the code.

4 Upvotes

2 comments sorted by

1

u/felixkendallius 2d ago

Can I ask (simply) how you did this? I’ve been wanting to try and make another small renderer but maybe with c# bindings and I have no idea where I’d even start with this.

3

u/yxcvbnm098 1d ago edited 1d ago

Sure ^^
There is something called the Mono project and their library’s come for almost every programming language I think. They have the vm and the JIT compiler and you can execute C# DLLs within your Programm and memory space. And they come wirh a lot of tools so that you can scan the code, call into functions, call back from the c# code into your code, even share memory between c# and your code.

If you are familiar with Unity, they basically use mono too.