Open liaujianjie opened 4 years ago
I don't think we should throw when a component is not returned. A return type of Component | undefined
is valid and likely what we want. You should guard against null components if you aren't sure if the entity has a component. If you are sure that component exists on an entity, you can use the null assertion operator !
.
Aside from API semantics, I think including a branch that throws an exception in a method that is used a lot may have a negative impact on performance. I'm not certain about this, but I would think this is harder to optimize out and likely has a non-negligible impact on perf.
This PR adds
getComponentOrThrow
andgetMutableComponentOrThrow
methods onEntity
so that we can throw a developer-friendly error message when we're trying to access a component that doesn't exist on an entity.Typescript
Before
Typescript users currently need to either:
getComponent
/getMutableComponent
call.After
Notes for reviewer
I'm not sure if creating separate methods is a good idea. Alternatively, we can add/modify the params for the existing component getter methods on
Entity
.For example, adding a new param:
I'll write the tests if this gets the green light.