[Solved] Newbie question: How to not derive from Entity?

Hi,

first of all congratulations for releasing your outstanding work to the public. I have been playing around for a few days and I am truly amazed with what you have created there. However since I have problems grapsing the whole basic concept, maybe someone would be kind enough to explain a principle approach to me?

Coming from XNA my first impulse was to derive from Entity to add some properties to my game objects, for a simple example let’s just stick with HitPoints, like so:

public class GameCharacter : Entity
{
public int HitPoints;
}

Now if I do that, I do not find my derived class in the Studio so I can not instanciate it there. I understand this is not the intended way to do things? Or would you suggest to just create instances programmatically instead?

As an alternative, I could put my hit points and whatnots in a script class and just attach that to an Entity instance in the Studio. Is that the intended approach? If so, I would have to do that over and over again, for every instance I add? (I guess I could copy/paste or somehow have templates though, in any case no biggy)

Also, if I build methods or classes to do some math that - in the derived class scenario - would “normally” take two GameCharacters as input, how would i do that with the attached scripts then? I.e. using two Entity instances as an input, how would I get my attached scripts holding the properties from them? E.g:

public Entity CalculateWinner(Entity character1, Entity character2)
{
// Compare HitPoints…how do i read them?
}

I.e. how do i get to the attached script class instances of the Entities? And would I then have to iterate those to find my required type instance? That seems like a huge performance no-no?

Sorry for sounding stupid, I just try to figure the right approach since all I am familiar with so far is just deriving from base classes (which doesnt seem to be the intended way here?)

Regards,
Sven

You would want to make HitPoints a Component, like so:

using System;
using SiliconStudio.Core;
using SiliconStudio.Paradox.Engine;

namespace CustomComponent
{
    [DataContract("Hitpoints")]
    [Display(42, "Hitpoints", Expand = ExpandRule.Once)]
    public class Hitpoints : EntityComponent
    {

        public readonly static PropertyKey<Hitpoints> Key = new PropertyKey<Hitpoints>("Key", typeof(Hitpoints));

        
        public override PropertyKey GetDefaultKey()
        {
            return Key; 
        }

        [DataMember(10)]
        public int HP;
    }
}

EDIT: Note… there has to be a more elegant way of setting the component index. In this case I just hard coded it as 42. Not the cleanest way to do things, but easy.

Then it can be attached to any entity, and is configurable in the editor:

1 Like

Hi there,

thanks a lot for all the input (and even providing screenshots!).
This looks very interesting. Now I know where to dig into the docs, there seems to be quite some content about components.

Thank you.