Improbable Icon

Serializable AST from C#?



I wish to reverse the workflow on Schema -> AST -> Code and instead be able to serialize a class from C# which will turn into JSON (AST) and then through codegen, generate code for me.

The end result is that I want to be able to create an editor window in Unity in which I can “make and generate code from”. Bypassing the need to write schema layout files at all.

Any thoughts on this?


That is interesting, though I wonder if it might be even more interesting to have an Window in Unity where you can select which Component to change and then not write a C# class but use a panel to add fields, commands and events


Hey @AtomiC,

When you say:

What do you exactly mean by “generate code for me”? Or “serialize a class from C#”? For the latter do you only care about representing its data or do you else need to do something with the classes methods?

That point put aside it is theoretically possible already to do the first step that you suggest (C# -> AST) but you’ll have to write your own C# parser that is able to recognize patterns that are not compatible with Schema’s expressiveness and then decide what to do in such a case.

As it is Schema’s expressiveness is only a very small subset of C# so you cannot translate just any code backwards into Schema (or the Json AST which is only different clothing for the same thing.

But if you provide a more precise use-case we might be able to work out something that suits your needs. :slight_smile:



ImprobableCodeGen codeGen = new ImprobableCodeGen();

CodeComponent healthComponent = new CodeComponent();
healthComponent.Namespace = “Improbable.Health”;
healthComponent.AddField(“Health”, “int32”)
healthComponent.AddCommand(“Damage”, “DamageResponse”, “DamageRequest”);
healthComponent.AddEvent(“Damaged”, “DamageEvent”);
healthComponent.queryable = true;


Here is a rough outline of the workflow I want to achieve.


I’m still not sure what you mean here.

Do you want your workflow to be able to use the example you gave in order to generate all the backing-code for your workers & deployment?

One thing you could do along those lines is to write a generator from C# to Schema directly without going through the AST representation as the Schema would contain exactly the same information as what you wrote in your code-snippet. However I am still not sure how that should work as you would need to run that generation before you build your worker which means the code-snippet will actually never get (and should not get) executed at run-time… This looks like something overly-complicated as it would just be simpler to write the Schema and use it to generate the backing C#.

The core principle that lies behind the above is that you need the backing-code to be generated before you build and run your workers (i.e the Schema should be known at that point).


That’s pretty much it except it is meant to be used in Unity Editor code to add components one by one and not to be used as in a build step before building workers. It should be used when you just want to add to the schema files or edit.

I guess in essence I want a schema reader/writer but I hoped I could bypass the need to write one if you had something internal.

What I hope to achieve in the end is to get an overview over all component, types, commands and events in an editor window (which I can do by reading the generated code), and being able to quickly add and edit the schema files.


Hey! I’m guessing your goal is to be able to just write some arbitrary C# class, and have the SpatialOS Component backing it be created for you, rather than having to write the schema yourself?

As discussed in this thread, a good way to experiment with this would be to reflectively look at the class, and perhaps use a templating system to generate the SpatialOS schema files. You’d then potentially be able to generate a wrapper that ‘hooks up’ your C# class to the SpatialOS version of it.

This idea is something we’ve had discussions about before, but felt we needed to learn more from our community before pursuing. Hearing more about your reasons for wanting to do this would be really appreciated!



If the Unity integration were to ship with an editor GUI for searching for and modifying schema, do you think that would achieve what you’re looking to do?


It would :slight_smile:
I would also like to be able to create schema files and add new components, types and enums to them.