Miniplex 2.0 Beta 1! #258
Replies: 4 comments 17 replies
-
Just upgraded our project to use 2.0 and everything looks great, nice work! Only one thing I would mention:
Unfortunately this does not work if you want to check for the first element in an if statement: if (archetype[0]) {
// do something
} So you now have to do this: const [first] = archetype;
if (first) {
// do something
} Which is annoying. The alternative is: IMO the archetype should have a |
Beta Was this translation helpful? Give feedback.
-
In the docs for 2.0 you talk about this being safe but unfortunately it isnt always. For example this throws an error: import { World } from "miniplex";
interface Entity {
health: { value: number };
age: {};
}
test(`it works`, () => {
const world = new World<Entity>();
const archetype = world.archetype("health", "age");
const entityA = world.add({
health: { value: 10 },
age: {},
});
const entityB = world.add({
health: { value: 20 },
age: {},
});
for (const entity of archetype) {
console.log(entity.health.value);
world.remove(entityA);
world.remove(entityB);
}
});
|
Beta Was this translation helpful? Give feedback.
-
Heya! Upgrading today for fun, I'll post some thoughts in this comment as I come across them.
const ECS = createReactApi(new World<...>());
const entities = ECS.useEntities(world => world.with('active'));
This line of code is causing a new entity reference every time the Entity component renders https://github.com/hmans/miniplex/blob/main/packages/miniplex-react/src/createReactAPI.tsx#L32, which might explain all of my whacky behaviour I'm seeing.
To fix it we'll probably want to conditionally create it in the render function as each Entity element should have its own unique reference.
(Yep that fixes things for me)
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the thorough feedback, @itsdouges! Let me address these one by one:
Yes, I've thought about it, but so far decided against it because I've been intending to guide users towards declaring their archetypes outside of components. All in all, though, I'm not against offering this as an alternative API, I just wouldn't want to make it the default.
Hm, it should, but maybe something is off. I will check.
I will check. It should throw when not run inside an entity context.
When an entity is added, removed, or has components added/removed, the world will emit its Can you describe a scenario (or maybe even provide some code, in Codesandbox or not) that demonstrates the unexpected behavior that you're seeing?
The The reason I'm doing this is that without it, the removed component would already be gone by the time
Good catch, that's a pretty nasty bug! Thanks for all the good feedback, and keep it coming! |
Beta Was this translation helpful? Give feedback.
-
Hooray, it's the first beta release of Miniplex 2.0! While the documentation is being updated (including an all-new website that is still deeply work in progress!), I'd like to provide you with a summary of the changes so you can start giving this thing a go in your projects.
Focus:
Miniplex 2.0 is a complete rewrite of the library, but while it does bring some breaking changes, it will still allow you to do everything that you've been doing with 1.0. When upgrading a 1.0 project to 2.0, most changes you will need to do are related to things having been renamed.
The headline changes in 2.0:
Installing:
While Miniplex 2.0 is in beta, you will need to install it using the
beta
tag:Core:
world.createEntity
has been renamed and simplified to justworld.add
(which now returns the correct type for the entity, too), andworld.destroyEntity
toworld.remove
.addComponent
andremoveComponent
have not been changed.There is a new
world.update
function that you may use to update an entity and make sure it is reindexed across the various archetypes. It provides a number of different overloads to provide some flexibility in how you update the entity.Please keep in mind that in Miniplex, you'll typically mutate your entities anyway, so going through this function is not strictly necessary. The upcoming documentation will go into more detail as to when using this function is useful (or even necessary), and when you should avoid it.
The
Tag
type and constant have been removed. For tag-like components, simply usetrue
(whichTag
was just an alias for.)Entities added to a world no longer receive a
__miniplex
component. This component has always been an internal implementation detail, but you might have used it in the past to get a unique identifier for an entity. This can now be done throughworld.id(entity)
, with ID lookups being available throughworld.entity(id)
.Archetypes can now be iterated over directly. Example:
You can use this to neatly fetch the first entity from an archetype that you only expect to have a single entity in it:
The queuing functionality that was built into the
World
class has been removed. If you've relied on this in the past,miniplex
now exports aqueue
object that you can use instead. Example:Please note that this is being provided to make upgrading to 2.0 a little easier, and will likely be removed in a future version.
world.archetype
can now take a predicate! You can use this as an escape hatch for creating any kind of archetype based on the conditions you specify. Example:Please note that his requires entities with the
health
component to be updated through theworld.update
function in order to keep the archetype up to date.You can use
with
andwithout
as an alternative API for creating archetypes. Example:You can use
where
to create a predicate-based iterator. This allows you to quickly filter a set of entities without creating new archetypes or other objects. Example:All of these can be nested!
Entities fetched from an archetype will have much improved types, but you can also specify a type to narrow to via these functions' generics:
Miniplex provides the new
Strictly
andWith
types which you can use to compose types from your entity main type:React:
The
miniplex-react
package has been retired, you can now import the React-specific bits fromminiplex/react
. The main export of that package has been renamed tocreateReactAPI
and expects to be passed an instance of a world, so the setup now looks like this:The
<Archetype>
component now supports thewith
andwithout
properties:If you already have a reference to an archetype, you can pass it to the newly improved
<Entities>
component to automatically render all entities contained within it, and have them automatically update when the archetype changes:If you ever want to list a simple array of entities, you can use the same component (but it will not automatically update if the array contents change):
<ManagedEntities>
has been removed. You were probably not using it. If you were, you can replicate the same behavior using a combination of the<Entities>
or<Archetype>
components and auseEffect
hook.The
useEntity
hook has been renamed touseCurrentEntity
.The world-scoped
useArchetype
hook has been removed, and superceded by the new globaluseEntities
hook:Feedback and Questions?
This is the first beta of a big new release for this library, and since it is a complete rewrite, there are bound to be some bugs and rough edges.
Beta Was this translation helpful? Give feedback.
All reactions