Skip to content

Cannot use field with name 'id' when using C++ codegen #205

@zaucy

Description

@zaucy

Component/system structs get generated with a static constexpr field called id which means that we cannot use id as an ecsact field. This is a fairly common field name. It's completely reasonable for a user to write something like this:

component Player { i32 id; }

To workaround this users must give a more unique name such as this:

component Player { i32 player_id; }

This is an annoying workaround and may become more of an issue if we introduce more static constexpr fields. I think we can solve this in 2 different ways.

1) all static constexpr have prefix or suffix _

In ecsact you're not allowed to have prefix or suffix _ for your fields so this may work. However, we may relax this rule in the future.

2) use a traits struct

Instead of having a static constexpr fields we could use template specialized structs (AKA C++ traits)

template<>
struct ecsact::component_traits<ExampleComponent> {
  static constexpr ecsact_component_id id = /* codegen value */;
};

This has the advantage of not conflicting with any user code, but makes access to the components ID in C++ code look like this:

-C::id
+ecsact::component_traits<C>::id

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions