Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative to Dictionary<T, int> #67

Closed
Donaut opened this issue Mar 6, 2023 · 5 comments
Closed

Alternative to Dictionary<T, int> #67

Donaut opened this issue Mar 6, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@Donaut
Copy link
Contributor

Donaut commented Mar 6, 2023

In dotnext they created a generic class with a static read-only int field each time somebody accesses the generic class with a new type the static field is incremented. And thus creating a compile-time dictionary. TypeMap There was a discussion about it too somewhere in the c# runtime repository but sadly I lost that.

@genaray genaray added the enhancement New feature or request label Mar 6, 2023
@genaray
Copy link
Owner

genaray commented Mar 6, 2023

Thanks! :) Haven't seen that one. I will take a look at it.

@Donaut
Copy link
Contributor Author

Donaut commented Mar 15, 2023

Hello, I have good news :). The idea works like a charm. Below is a sample code of how it looks.

var world = new World();

// Approach 1: Builders
var builder = new ArcheTypeInfo.Builder();
builder.AddComponent<int>();
builder.AddComponent<ulong>();
var archeTypeInfo = builder.Build(); // We want to avoid creating new ArcheTypes every time so we need a way to locate already existing ArcheTypes. ArcheTypeInfo is a struct that is used as a dictionary key for locating already existing ArcheTypes.
var entity = world.CreateEntity(archeTypeInfo);

// Approach 2: Generic methods that use the builder internally.
// world.CreateEntity<T0..T9>(in T0..T9) If we dont want to use the builder approach. It's a little bit slower than the builder approach.

// Set method with multiple generics so we can quickly assign data for each component type.
entity.Set<T0..T9>(in T0..T9);

@genaray
Copy link
Owner

genaray commented Mar 15, 2023

Thats great! :)

Just some questions:

  • do you have a working version somewhere?
  • Can the builder be made a struct?
  • Is it still compatible with 'Type's? E.g. getting a ComponentType by Type

However i think the .create call with generics should stay aswell since thats a nice api for quick prototyping, it also sets the values in the same step which is also quite nice :)

@Donaut
Copy link
Contributor Author

Donaut commented Mar 15, 2023

Currently, I don't have a fully working sample. There is no concept of ComponenTypes. A Type is just converted into an index, which is then used to place them inside an array. The more components there is the bigger the array.

@genaray
Copy link
Owner

genaray commented Mar 15, 2023

There is no concept of ComponenTypes

Well this is a problem, i think such an approach does not exclude the concept of componenttypes however. ComponentTypes are pretty important for the underlying chunks and store meta data such as byte size and in the future if the component is managed or not ^^ But if a type can be converted to an index, it can also be converted to the componenttype with that approach i guess.

@genaray genaray closed this as completed Apr 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants