I'm trying to come up with a scripting solution for my entity component framework written in C#. This would be for specialized scripts attached to entities. Right now I have a Script component to which you can attach script ids. These ids map to C# classes that implement the scripts. There is only one instance of each script. When it runs it is presented with "property bag" state associated with the entity, and it can read and write values to that.
It's functional, but the way I get/set script properties in code (accessing them by id from a property bag) is ugly. I'd rather just be able to treat them as regular C# properties on the script object. The reason I can't do that is because I need to quickly serialize the state of the script.
I have the following requirements:
- I need to be able to reasonably quickly serialize the state of the script and equally quickly re-hydrate it. The state should be platform portable, and generate no allocations during serialization (this I have now)
- Be able to store variables of simple data types (float, int, etc...) as part of per-entity per-script state. (this I have now)
- Access those properties with clear syntax (E.g. "DeathTimer = 1.5", instead of "propertyBag.SetFloatValue(DeathTimerId, 1.5f)" (this I do not have now)
- Be able to access an entity's components through simple syntax (e.g. "entity.Transform.Position = new Vector3(0, 0, 0)"). (this I do not have now, but I could probably hack something together to give me this nice syntax).
1) I've though about using Lua, but I'm not sure how flexible it is. Can I customize how C# properties/methods are exposed in Lua? i.e. make my ugly PropertyBag object appear as a regular object to which I can get/set properties.
2) I could invent my own scripting language to accomplish everything I want. I started down this path for a while, but it is a lot of work.
3) Keep using C# and find some solution to make my syntax nice (basically this means finding a good way to handle quick serialization of script state). This is the most flexible coding-wise, and has the least dependencies (i.e. I don't need to depend on some 3rd party Lua library), and I'd like to go down this path if possible. If C# had macros, I could easily "auto-generate" object properties that have their data stored in my PropertyBag (ala DependencyProperty). I think this would solve most of my syntactic concerns. But alas...
Any thoughts? What are other people using?