sschmid / Entitas

Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
MIT License
7.18k stars 1.11k forks source link

[Feature] Entity Blueprints #48

Closed npruehs closed 8 years ago

npruehs commented 8 years ago

Following up our previous discussion about allowing Entitas to be more data-driven, here's what I'd love to do, similar to what we are doing in our CBES:

  1. Create a Blueprint class with two properties:
    1. List of component types to add to the entity
    2. Dictionary mapping component property names to component property values.
  2. Adapt the code generator to provide an overload Pool.CreateEntity(Blueprint) that basically does three things:
    1. Create a new entity.
    2. Iterate over the component types of the blueprint.
    3. Add all components with their respective initial values taken from the dictionary.

This allows for easy serialization of entity data while preserving the flexibility and performance of the generated code.

What do you think? Would this be the correct way to go with Entitas?

Also, am I right that you are currently building a new code generator? Should I wait until you're finished?

npruehs commented 8 years ago

Alright, there you go:

https://github.com/npruehs/Match-One-Blueprints

Works like a charm! :) Pieces are read by a new BlueprintsController from the file Assets/Data/blueprints.xml.

Unfortunately, prefixing the generated source file name with a folder name does not work:

DirectoryNotFoundException: Could not find a part of the path "D:\Dev\SCM\Match-One\Assets\Sources\Generated\Blueprints\PositionComponentGeneratedBlueprintsExtension.cs".
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:292)
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool,System.Text.Encoding)
System.IO.File.WriteAllText (System.String path, System.String contents, System.Text.Encoding encoding) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:626)
System.IO.File.WriteAllText (System.String path, System.String contents) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/File.cs:621)
Entitas.CodeGenerator.CodeGenerator.writeFiles (System.String directory, Entitas.CodeGenerator.CodeGenFile[] files) (at Assets/Libraries/Entitas/Entitas.CodeGenerator/Editor/CodeGenerator.cs:56)
Entitas.CodeGenerator.CodeGenerator.Generate (ICodeGeneratorDataProvider provider, System.String directory, Entitas.CodeGenerator.ICodeGenerator[] codeGenerators) (at Assets/Libraries/Entitas/Entitas.CodeGenerator/Editor/CodeGenerator.cs:19)
Entitas.CodeGenerator.TypeReflection.TypeReflectionCodeGenerator.Generate (System.Reflection.Assembly assembly, System.String[] poolNames, System.String directory, Entitas.CodeGenerator.ICodeGenerator[] codeGenerators) (at Assets/Libraries/Entitas/Entitas.CodeGenerator.TypeReflection/Editor/TypeReflectionCodeGenerator.cs:7)
Entitas.Unity.CodeGenerator.UnityCodeGenerator.Generate () (at Assets/Libraries/Entitas/Entitas.Unity.CodeGenerator/Editor/UnityCodeGenerator.cs:27)

Also, Match-One asset serialization is still set to "Mixed", you might want to change it to "Force Text" for future updates :)

I've found one or two other things as well, but I think I'm gonna create a few new issues, so you can finally close this one.

sschmid commented 8 years ago

@npruehs how did you actually create the blueprints.xml? Does it support custom classes?

npruehs commented 8 years ago

@sschmid The XML file has been created with the Entitas Blueprints serializer:

https://github.com/npruehs/Entitas-Blueprints-CSharp/blob/develop/Source/Entitas.Blueprints/Entitas.Blueprints.Xml/BlueprintXmlSerializer.cs

That serializer essentially wraps the .NET XmlSerializer and thus supports all CLR classes, with the same restrictions applied (i.e. serializes public fields and properties only, requires getter and setter, ...)

Of course, as soon as one or more official Entitas formats have been developed, I'd love to add them to the Tome editor for easier data editing :)

sschmid commented 8 years ago

I started playing around with SerializedObject and using the EntityDrawer known from VisualDebugging which basically already provides most of the features to create a Blueprint out of the box. The first steps look promising and I try to also support GameObjects and other Unity Objects.

entitas unity blueprints
sschmid commented 8 years ago

Maybe we can even use Unity’s serialization system with its yaml backend

npruehs commented 8 years ago

Nice! One thing though: Many games I've been working at recently required the data (or parts of it) to be present at the server as well, which obviously is unable to deal with ScriptableObjects. I'd suggest providing one non-Unity data format at least to keep Entitas interesting for online games, and/or other engines such as MonoGame.

EDIT: Haha, but I love the animation curve component! :)

sschmid commented 8 years ago

@npruehs Definitely agree! It was just so tempting to play around with it :)

sschmid commented 8 years ago

Updated Wiki with a link to your repo https://github.com/sschmid/Entitas-CSharp/wiki/Tools-and-Extensions