Improbable Icon

Why is component ID required in schema file?

schema

#1

Hi,

This might be a dumb question but I wasn’t able to find an answer to it.

I’ve been going through the Pirates Tutorial and there is this piece of code that describes the ship schema:

package improbable.ship;

component Health {
    // Component ID. Must be unique within the project.
    id = 1006;

    // Exclude this component from worker queries.
    option queryable = false;

    int32 current_health = 1;
}

Then there are some explanations below it, such as:
id = 1006 sets the ID for the Health component. Every component needs an ID that is unique within the project.

Why do we need to provide an ID to each component if it has to be unique within the project and every component needs it?
Couldn’t codegen assign an ID to each component and we (devs/users) won’t have to worry about it or keep track of the component IDs?
What happens if two components have the same ID (e.g. I have accidentally assigned the same ID to two different components)?

Thanks,
Nikola

Update: It probably has something to do with backwards compatibility when new snapshots are created, although I will have to have better understanding of the snapshots to fully understand this.


#3

Hello again @freezing,

The IDs are required for the management of data, component delegation to workers, etc. on a live deployment. They also help you in dealing with backwards-compatibility. There some things about it in the short section of the Schemalang documentation that is dedicated to IDs.

Without a developer specifying an explicit ID himself it would be hard to still achieve some degree of backwards-compatibility of snapshots. Codegen would not be able to make the correct identification through component renames and other modifications that do not break compatibility. Hence the fact that we do not assign them automatically. :slight_smile:

Internally we rely on Protocol buffers, which also explains the presence of an ID behind each field of your components. This is another element in the backwards-compatibility (where possible) of your Schema files.

Does this answer your question ? Don’t hesitate to ask for further details if some points are not clear enough yet.

Duco


#4

Hi Duco,

Thanks for explaining. It makes sense and I see why it’s very hard (impossible) to assign them automatically. I guess there has to be an identifier that remains the same and it’s much better if it’s not the name of the component.

Also, I was a bit confused at first when I saw option queryable = false, int32 id = 1 as I wasn’t sure what was the value assignment and what was the tag ID, but after reading the schema docs it makes more sense - sorry, I should have done that first, but I missed that part of the docs somehow.

It’s all clear for now, but I’m sure I’ll have more questions while going through tutorials :slight_smile:

Kind regards,
Nikola