Generate array based on attributes at compile time

Hey, I’m creating an in-game console for my game. My goal is to have an attribute that I can put on top of functions, and then the console system will gather those functions to create the commands that users can input at runtime.

Now, a solution to this is to gather these attributes at runtime at startup, and then build a list there. However, reflection is slow and it won’t scale too well with the amount of commands in the game, and just getting quicker load times, even by a bit can easily be worth this effort, if it’s possible.

So. Is there a way to generate the different commands at compile time? Writing them to a file is also a bit slow to read, so it would be great if I could somehow have that array in memory. Maybe there’s a pre-compile process where I can create a .cs file with the hard-coded array? Although I’d prefer to just not have any assets at all in regards to this, so maybe there could also be some code generation, which I have no idea how to do.

Are these static functions? I’m not clear on exactly what you’re hoping to achieve here. A few concrete examples illustrating the kind of thing you want to enable would really help.

1 Like

That’s how my datasacks package converts ScriptableObjects into static accessible classes.

See the GenerateCode() method here:

https://github.com/kurtdekker/datasacks/blob/master/datasacks/Assets/Datasack/Core/DatasackInspector.cs

It spoots out a DSMCodegen.cs file.