Writing custom components

When writing custom components, can we have them show up in the editor?
Currently the only way I can imagine is:

  • Create empty entity
  • Attach script
  • The script attaches my custom component to the entity

From what I understand this is the wrong way around.
How can I attach my components to my entities without writing a custom script to do it?

Hi!

I assume you already created a custom component, based on a built-in one. It should look something like this:

namespace MyGame
{
    [Display(100, "Custom Component", Expand = ExpandRule.Once)]
    [DataContract("MyCustomConponent")]
    // [DefaultEntityComponentProcessor(typeof(MyCustomProcessor))] // A processor is optional
    public sealed class MyCustomConponent : EntityComponent
    {
        public static PropertyKey<MyCustomConponent> Key = new PropertyKey<MyCustomConponent>("Key", typeof(MyCustomConponent));

        public override PropertyKey GetDefaultKey()
        {
            return Key;
        }
}

If the component is defined in a game project (a project that is referenced by your .pdxpkg file), the component should get picked up by the editor automatically. It can be added just like the default ones.

Currently, you will have to reload your project every time you add (or change) a component, to have the changes take effect.

Also, make sure to save your project in Visual Studio, so the class gets compiled and found when opening the project in the editor.

Let me know if you have any trouble with that!

Aha, I tried deriving from ComponentBase or implement IComponent but not from EntityComponent.
Also the building / reloading cycle must have thrown my experimentation off.

Glad it’s actually a pretty easy process :slight_smile:
Thanks