Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/bridge_packages/flame_behaviors/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ For instance a `TimerComponent` can implement a time-based behavioral activity:
class MyBehavior extends Behavior {
@override
Future<void> onLoad() async {
await add(TimerComponent(period: 5, repeat: true, onTick: _onTick));
add(TimerComponent(period: 5, repeat: true, onTick: _onTick));
}

void _onTick() {
Expand Down
6 changes: 3 additions & 3 deletions doc/bridge_packages/flame_bloc/bloc.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ We can do that by using `FlameBlocProvider` component:
class MyGame extends FlameGame {
@override
Future<void> onLoad() async {
await add(
add(
FlameBlocProvider<PlayerInventoryBloc, PlayerInventoryState>(
create: () => PlayerInventoryBloc(),
children: [
Expand All @@ -44,7 +44,7 @@ fashion:
class MyGame extends FlameGame {
@override
Future<void> onLoad() async {
await add(
add(
FlameMultiBlocProvider(
providers: [
FlameBlocProvider<PlayerInventoryBloc, PlayerInventoryState>(
Expand Down Expand Up @@ -72,7 +72,7 @@ By using `FlameBlocListener` component:
class Player extends PositionComponent {
@override
Future<void> onLoad() async {
await add(
add(
FlameBlocListener<PlayerInventoryBloc, PlayerInventoryState>(
listener: (state) {
updateGear(state);
Expand Down
2 changes: 1 addition & 1 deletion doc/bridge_packages/flame_spine/flame_spine.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FlameSpineExample extends FlameGame {

// Set the "walk" animation on track 0 in looping mode
spineboy.animationState.setAnimationByName(0, 'walk', true);
await add(spineboy);
add(spineboy);
}

@override
Expand Down
13 changes: 13 additions & 0 deletions doc/flame/components/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ class MyGame extends FlameGame {
The two approaches can be combined freely: the children specified within the constructor will be
added first, and then any additional child components after.

The `add()`, `addAll()`, and `addToParent()` methods are synchronous: they return immediately
without waiting for the child to load or mount. This makes them safe to call from anywhere,
including inside `update()` or a loop that spawns many components, without having to `await` them
or wrap them in `unawaited`. If you need to wait until a child has reached a given lifecycle stage,
await its `loaded`, `mounted`, or `removed` future instead (see the lifecycle getters under
[Component lifecycle](#component-lifecycle)):

```dart
world.add(coin);
await coin.mounted;
Comment on lines +223 to +224

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One footgun with this is that if they do this in onLoad of the component they are adding to it will be a deadlock

// The coin is now guaranteed to be mounted.
```

Note that the children added via either method are only guaranteed to be available eventually:
after they are loaded and mounted. We can only assure that they will appear in the children list
in the same order as they were scheduled for addition.
Expand Down
4 changes: 2 additions & 2 deletions doc/flame/examples/lib/anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class AnchorGame extends FlameGame {
paint: BasicPalette.blue.paint(),
);

await _redComponent.addAll([
_redComponent.addAll([
_blueComponent,
CircleComponent(radius: 2, anchor: Anchor.center),
]);

await addAll([
addAll([
_redComponent,
_parentAnchorText,
_childAnchorText,
Expand Down
2 changes: 1 addition & 1 deletion doc/flame/examples/lib/time_scale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TimeScaleGame extends FlameGame with HasTimeScale {

@override
Future<void> onLoad() async {
await add(
add(
EmberPlayer(
position: size / 2,
size: size / 4,
Expand Down
8 changes: 4 additions & 4 deletions doc/flame/game.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class MyCrate extends SpriteComponent {

class MyWorld extends World {
@override
Future<void> onLoad() async {
await add(MyCrate());
void onLoad() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature of onLoad shouldn't change right? I think super.onLoad might be needed here too?

add(MyCrate());
}
}

Expand Down Expand Up @@ -236,8 +236,8 @@ application. This is a common scenario when building games: there is a single fu

Adding this mixin provides performance advantages in certain scenarios. In particular, a component's
`onLoad` method is guaranteed to start when that component is added to its parent, even if the
parent is not yet mounted itself. Consequently, `await`-ing on `parent.add(component)` is guaranteed
to always finish loading the component.
parent is not yet mounted itself. Consequently, awaiting `component.loaded` after
`parent.add(component)` is guaranteed to finish loading the component.

Using this mixin is simple:

Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/platformer/app/lib/overlays/hud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Hud extends PositionComponent with HasGameReference<EmberQuestGame> {

for (var i = 1; i <= game.health; i++) {
final positionX = 40 * i;
await add(
add(
HeartHealthComponent(
heartNumber: i,
position: Vector2(positionX.toDouble(), 20),
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/platformer/step_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Hud extends PositionComponent with HasGameReference<EmberQuestGame> {

for (var i = 1; i <= game.health; i++) {
final positionX = 40 * i;
await add(
add(
HeartHealthComponent(
heartNumber: i,
position: Vector2(positionX.toDouble(), 20),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CameraTarget extends PositionComponent

@override
Future<void> onLoad() async {
await add(moveEffect);
add(moveEffect);
}

void go({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class InputHandler extends PositionComponent

@override
Future<void> onLoad() async {
await add(
add(
KeyboardListenerComponent(
keyDown: {
LogicalKeyboardKey.arrowLeft: (_) => onLeftStart(),
Expand Down
2 changes: 1 addition & 1 deletion examples/lib/stories/animations/benchmark_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ starts to drop in FPS, this is without any sprite batching and such.

@override
Future<void> onLoad() async {
await camera.viewport.addAll([
camera.viewport.addAll([
FpsTextComponent(
position: size - Vector2(10, 50),
anchor: Anchor.bottomRight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class BlobWorld extends Forge2DWorld
..dampingRatio = 1.0
..collideConnected = false;

await addAll([
final blobParts = [
for (var i = 0; i < 20; i++)
BlobPart(i, jointDef, blobRadius, blobCenter),
]);
];
addAll(blobParts);
await Future.wait(blobParts.map((part) => part.loaded));
createJoint(ConstantVolumeJoint(physicsWorld, jointDef));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class RopeJointWorld extends Forge2DWorld
width: handleWidth,
height: 3,
);
await add(box);
add(box);
await box.loaded;

createPrismaticJoint(box.body, anchor);
return box.body;
Expand All @@ -49,7 +50,8 @@ class RopeJointWorld extends Forge2DWorld
for (var i = 0; i < length; i++) {
final newPosition = prevBody.worldCenter + Vector2(0, 1);
final ball = Ball(newPosition, radius: 0.5, color: Colors.white);
await add(ball);
add(ball);
await ball.loaded;

createRopeJoint(ball.body, prevBody);
prevBody = ball.body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class WeldJointWorld extends Forge2DWorld
color: Colors.white,
);

await addAll([leftPillar, rightPillar]);
addAll([leftPillar, rightPillar]);
await Future.wait([leftPillar.loaded, rightPillar.loaded]);

createBridge(leftPillar, rightPillar);
}
Expand Down Expand Up @@ -71,7 +72,8 @@ class WeldJointWorld extends Forge2DWorld
width: sectionWidth,
height: 1,
);
await add(section);
add(section);
await section.loaded;

if (prevSection != null) {
createWeldJoint(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DialogueBoxComponent extends SpriteComponent with HasGameReference {
'dialogue_box.png',
srcSize: spriteSize,
);
await addAll([buttonRow, textBox]);
addAll([buttonRow, textBox]);
return super.onLoad();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FlameSpineExample extends FlameGame with TapCallbacks {

// Set the "walk" animation on track 0 in looping mode
spineboy.animationState.setAnimation(0, 'walk', true);
await add(spineboy);
add(spineboy);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SharedDataSpineExample extends FlameGame with TapCallbacks {
spineboy.animationState.setAnimation(0, 'walk', true);
spineboys.add(spineboy);
}
await addAll(spineboys);
addAll(spineboys);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ class AntWorld extends World {
Future<void> onLoad() async {
final random = Random();
curve = DragonCurve();
await add(curve);
add(curve);
await curve.loaded;
bgRect = curve.boundingRect().inflate(100);

const baseColor = HSVColor.fromAHSV(1, 38.5, 0.63, 0.68);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CameraComponentPropertiesExample extends FlameGame {
..strokeWidth = 0.25
..color = const Color(0xaaffff00),
);
await world.add(_cullRect);
world.add(_cullRect);
camera.mounted.then((_) {
updateSize(canvasSize);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MultipleWorldsExample extends FlameGame {
final world2 = CollisionDetectionWorld();
final camera1 = CameraComponent(world: world1);
final camera2 = CameraComponent(world: world2);
await addAll([world1, world2, camera1, camera2]);
addAll([world1, world2, camera1, camera2]);
final ember1 = CollidableEmber(position: Vector2(75, 75));
final ember2 = CollidableEmber(position: Vector2(-75, 75));
final ember3 = CollidableEmber(position: Vector2(75, -75));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class _BallWorld extends World with TapCallbacks {

// Add a stats display to show pool information
statsDisplay = _StatsDisplay(pool: ballPool);
await add(statsDisplay);
add(statsDisplay);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion examples/lib/stories/components/keys_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class KeysExampleGame extends FlameGame {
final mage = await loadSprite('mage.png');
final ranger = await loadSprite('ranger.png');

await addAll([
addAll([
SelectableClass(
key: ComponentKey.named('knight'),
sprite: knight,
Expand Down
6 changes: 3 additions & 3 deletions examples/lib/stories/components/time_scale_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TimeScaleExample extends FlameGame
);
gameSpeedText.position = Vector2(size.x * 0.5, size.y * 0.8);

await world.addAll([
world.addAll([
_Chopper(
position: Vector2(-100, -10),
size: Vector2.all(64),
Expand Down Expand Up @@ -96,8 +96,8 @@ class _Chopper extends SpriteAnimationComponent

@override
Future<void> onLoad() async {
await add(CircleHitbox());
await add(_timer);
add(CircleHitbox());
add(_timer);
return super.onLoad();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lib/stories/system/step_engine_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class StepEngineExample extends FlameGame
hudComponents: [_controlsText],
);

await addAll([world, cameraComponent]);
addAll([world, cameraComponent]);
}

@override
Expand Down
4 changes: 2 additions & 2 deletions packages/flame/benchmark/collision_detection_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FlatCollisionBenchmark extends AsyncBenchmarkBase {
),
);

await _game.addAll(_blocks);
_game.addAll(_blocks);
await _game.ready();
}

Expand Down Expand Up @@ -96,7 +96,7 @@ class NestedCollisionBenchmark extends AsyncBenchmarkBase {
components.add(parent);
}

await _game.addAll(components);
_game.addAll(components);
await _game.ready();
}

Expand Down
8 changes: 4 additions & 4 deletions packages/flame/benchmark/component_churn_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class ComponentChurnBenchmark extends AsyncBenchmarkBase {
Future<void> setup() async {
_game = FlameGame();
await mountGame(_game);
await _game.world.addAll(
_game.world.addAll(
List.generate(staticPopulation, (_) => Component()),
);
for (var i = 0; i < _liveBatches; i++) {
final batch = _newBatch();
_batches.addLast(batch);
await _game.world.addAll(batch);
_game.world.addAll(batch);
}
await _game.ready();
}
Expand All @@ -65,7 +65,7 @@ class ComponentChurnBenchmark extends AsyncBenchmarkBase {
_game.world.removeAll(_batches.removeFirst());
final batch = _newBatch();
_batches.addLast(batch);
await _game.world.addAll(batch);
_game.world.addAll(batch);
_game.update(_dt);
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ class MassAddRemoveBenchmark extends AsyncBenchmarkBase {
Future<void> run() async {
for (var i = 0; i < _amountCycles; i++) {
final components = List.generate(_amountComponents, (_) => Component());
await _game.world.addAll(components);
_game.world.addAll(components);
_game.update(_dt);
_game.world.removeAll(components);
_game.update(_dt);
Expand Down
4 changes: 2 additions & 2 deletions packages/flame/benchmark/priority_change_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SiblingPriorityChangeBenchmark extends AsyncBenchmarkBase {
),
),
);
await _game.world.addAll(parents);
_game.world.addAll(parents);
await _game.ready();
_children = [
for (final parent in parents) parent.children.toList(growable: false),
Expand Down Expand Up @@ -98,7 +98,7 @@ class YSortPriorityBenchmark extends AsyncBenchmarkBase {
final random = Random(_randomSeed);
_game = FlameGame();
await mountGame(_game);
await _game.world.addAll(
_game.world.addAll(
List.generate(_amountChildren, (i) => Component(priority: i)),
);
await _game.ready();
Expand Down
Loading
Loading