text
stringlengths 1
372
|
|---|
broadly speaking, two types of multiplayer games exist:
|
high tick rate.
|
these games need to synchronize game states many times per second
|
with low latency.
|
these would include action games, sports games, fighting games.
|
low tick rate.
|
these games only need to synchronize game states occasionally
|
with latency having less impact.
|
these would include card games, strategy games, puzzle games.
|
this resembles the differentiation between real-time versus turn-based
|
games, though the analogy falls short.
|
for example, real-time strategy games run—as the name suggests—in
|
real-time, but that doesn’t correlate to a high tick rate.
|
these games can simulate much of what happens
|
in between player interactions on local machines.
|
therefore, they don’t need to synchronize game states that often.
|
if you can choose low tick rates as a developer, you should.
|
low tick lowers latency requirements and server costs.
|
sometimes, a game requires high tick rates of synchronization.
|
for those cases, solutions such as firestore don’t make a good fit.
|
pick a dedicated multiplayer server solution such as nakama.
|
nakama has a dart package.
|
if you expect that your game requires a low tick rate of synchronization,
|
continue reading.
|
this recipe demonstrates how to use the
|
cloud_firestore package
|
to implement multiplayer capabilities in your game.
|
this recipe doesn’t require a server.
|
it uses two or more clients sharing game state using cloud firestore.
|
<topic_end>
|
<topic_start>
|
1. prepare your game for multiplayer
|
write your game code to allow changing the game state
|
in response to both local events and remote events.
|
a local event could be a player action or some game logic.
|
a remote event could be a world update coming from the server.
|
to simplify this cookbook recipe, start with
|
the card template that you’ll find
|
in the flutter/games repository.
|
run the following command to clone that repository:
|
open the project in templates/card.
|
info note
|
you can ignore this step and follow the recipe with your own game
|
project. adapt the code at appropriate places.
|
<topic_end>
|
<topic_start>
|
2. install firestore
|
cloud firestore is a horizontally scaling,
|
NoSQL document database in the cloud.
|
it includes built-in live synchronization.
|
this is perfect for our needs.
|
it keeps the game state updated in the cloud database,
|
so every player sees the same state.
|
if you want a quick, 15-minute primer on cloud firestore,
|
check out the following video:
|
to add firestore to your flutter project,
|
follow the first two steps of the
|
get started with cloud firestore guide:
|
the desired outcomes include:
|
you don’t need to write any dart code in this step.
|
as soon as you understand the step of writing
|
dart code in that guide, return to this recipe.
|
<topic_end>
|
<topic_start>
|
3. initialize firestore
|
open lib/main.dart and import the plugins,
|
as well as the firebase_options.dart file
|
that was generated by flutterfire configure in the previous step.
|
<code_start>
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
import 'package:firebase_core/firebase_core.dart';
|
import 'firebase_options.dart';
|
<code_end>
|
add the following code just above the call to runApp()
|
in lib/main.dart:
|
<code_start>
|
WidgetsFlutterBinding.ensureInitialized();
|
await Firebase.initializeApp(
|
options: DefaultFirebaseOptions.currentPlatform,
|
);
|
<code_end>
|
this ensures that firebase is initialized on game startup.
|
add the firestore instance to the app.
|
that way, any widget can access this instance.
|
widgets can also react to the instance missing, if needed.
|
to do this with the card template, you can use
|
the provider package
|
(which is already installed as a dependency).
|
replace the boilerplate runApp(MyApp()) with the following:
|
<code_start>
|
runApp(
|
provider.value(
|
value: FirebaseFirestore.instance,
|
child: MyApp(),
|
),
|
);
|
<code_end>
|
put the provider above MyApp, not inside it.
|
this enables you to test the app without firebase.
|
info note
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.