From e27a4387829cb65bf943d96fb31870eae50449cd Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sun, 26 Dec 2021 14:57:34 +0100 Subject: [PATCH 1/4] feat: Turn off `strictMode` for children --- doc/components.md | 13 +++++++++---- .../flame/lib/src/components/component_set.dart | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/doc/components.md b/doc/components.md index 951bfdcb1d1..8d192b6dbd4 100644 --- a/doc/components.md +++ b/doc/components.md @@ -91,10 +91,15 @@ class GameOverPanel extends PositionComponent with HasGameRef { ### Querying child components -The children that have been added to a component live in the `QueryableOrderedSet` called -`components`. To query for a specific type of components in the set, a query first has to be -registered on the set, and then the `query` function can be run efficiently at any later point. The -register call is usually done in `onLoad`. +The children that have been added to a component live in a `QueryableOrderedSet` called +`children`. To query for a specific type of components in the set, the `query()` function can be +used. By default `strictMode` is `false` in the children set, but if you set it to true, then the +queries will have to be registered with `children.register` before a query can be used. + +If you know in compile time that you will later will run a query of a specific type it is +recommended to register the query no matter if the `strictMode` is set to `true` or `false`, since +there are some performance benefits to gain from it. The `register` call is usually done in +`onLoad`. Example: diff --git a/packages/flame/lib/src/components/component_set.dart b/packages/flame/lib/src/components/component_set.dart index 20ea049d993..f95b9f467ca 100644 --- a/packages/flame/lib/src/components/component_set.dart +++ b/packages/flame/lib/src/components/component_set.dart @@ -44,7 +44,7 @@ class ComponentSet extends QueryableOrderedSet { ComponentSet( int Function(Component e1, Component e2)? comparator, this.parent, { - bool strictMode = true, + bool strictMode = false, }) : super(comparator: comparator, strictMode: strictMode); /// Prepares and registers one component to be added on the next game tick. From f25b50fe45eb7877d45d24c00640989a9180d571 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sun, 26 Dec 2021 15:02:23 +0100 Subject: [PATCH 2/4] fix docs --- doc/components.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/doc/components.md b/doc/components.md index 8d192b6dbd4..e136ce62b12 100644 --- a/doc/components.md +++ b/doc/components.md @@ -96,10 +96,9 @@ The children that have been added to a component live in a `QueryableOrderedSet` used. By default `strictMode` is `false` in the children set, but if you set it to true, then the queries will have to be registered with `children.register` before a query can be used. -If you know in compile time that you will later will run a query of a specific type it is -recommended to register the query no matter if the `strictMode` is set to `true` or `false`, since -there are some performance benefits to gain from it. The `register` call is usually done in -`onLoad`. +If you know in compile time that you later will run a query of a specific type it is recommended to +register the query, no matter if the `strictMode` is set to `true` or `false`, since there are some +performance benefits to gain from it. The `register` call is usually done in `onLoad`. Example: From 198577e3f0db9352faa7ae976523939795b13d18 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sun, 26 Dec 2021 15:39:29 +0100 Subject: [PATCH 3/4] fix: Both places should have `strictMode = false` --- packages/flame/lib/src/components/component_set.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/lib/src/components/component_set.dart b/packages/flame/lib/src/components/component_set.dart index f95b9f467ca..1d95c296b14 100644 --- a/packages/flame/lib/src/components/component_set.dart +++ b/packages/flame/lib/src/components/component_set.dart @@ -245,7 +245,7 @@ class ComponentSet extends QueryableOrderedSet { /// will be added. static ComponentSet createDefault( Component parent, { - bool strictMode = true, + bool strictMode = false, }) { return ComponentSet( Comparing.on((c) => c.priority), From 792af1c160eb422ae4a6154c9d24a024b8189e57 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sun, 26 Dec 2021 15:54:11 +0100 Subject: [PATCH 4/4] fix: Use defaultStrictMode --- .../flame/lib/src/components/component_set.dart | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/flame/lib/src/components/component_set.dart b/packages/flame/lib/src/components/component_set.dart index 1d95c296b14..7151925bf3d 100644 --- a/packages/flame/lib/src/components/component_set.dart +++ b/packages/flame/lib/src/components/component_set.dart @@ -41,11 +41,16 @@ class ComponentSet extends QueryableOrderedSet { /// It is also called when the component changes parent. final Component parent; + static bool defaultStrictMode = false; + ComponentSet( int Function(Component e1, Component e2)? comparator, this.parent, { - bool strictMode = false, - }) : super(comparator: comparator, strictMode: strictMode); + bool? strictMode, + }) : super( + comparator: comparator, + strictMode: strictMode ?? defaultStrictMode, + ); /// Prepares and registers one component to be added on the next game tick. /// @@ -245,12 +250,12 @@ class ComponentSet extends QueryableOrderedSet { /// will be added. static ComponentSet createDefault( Component parent, { - bool strictMode = false, + bool? strictMode, }) { return ComponentSet( Comparing.on((c) => c.priority), parent, - strictMode: strictMode, + strictMode: strictMode ?? defaultStrictMode, ); } }