Skip to main content

One post tagged with "mlapi"

View All Tags

Hands-on with Netcode and RPCs Webinar

Lori Krell

Lori Krell

Unity, Senior Tech Writer

First of all, thank you to everyone who registered and attended our first Netcode webinar! Please remember to fill out the survey you received in your inbox so we can continue making our webinars more informative, educational, and engaging for you folks.

If you missed the webinar, don’t worry! We’ve included the recording below for you to view at your own convenience.

Now, let’s review the questions we had submitted before and during the webinar.

About MLAPI#

Learn more about MLAPI, goals, plans, and more.

What's the future of Multiplayer and Unity#

The future of Multiplayer at Unity is very exciting and we are inviting the entire world to join us on the journey. We are still working on DOTS-Netcode for Entity Component System users; though, as you can see, we're also very invested in supporting GameObject/MonoBehaviour developers by way of our acquisition of MLAPI. Our plan is to contribute to its enhancement and broaden the scale of what MLAPI offers.

Unity users love choice and customizability, and our new Multiplayer stack allows for the netcode layer to be transport-agnostic and the transport layer to be netcode-agnostic. MLAPI is open-source meaning that, for whatever functionality isn't present, users can choose to contribute back changes. This open-source community can create custom extensions to unlock others making different kinds of multiplayer games. For those who don't want to go down that route, we have a long, multi-year plan for offering turn-key solutions to create many of the most popular types of multiplayer games. Alongside releasing a stable netcode foundation, we're focusing first on providing solutions for small-scale, client-hosted cooperative games. Then, we'll be releasing sophisticated solutions tailored to dedicated game server scenarios advantageous for competitive type games.

Please stay posted for more information and, in the meantime, please continue to give us feedback about your preferences and project needs! We're happy to listen and help wherever possible.

In Unity’s perspective, what value to developers are MLAPI offering compared to the underlying netcode library?#

MLAPI will offer Unity developers the ability to more easily create networked, multiplayer experiences. We acquired the technology after conducting a landscape research where developers rated different existing solutions based on what was important to them. Across the board, MLAPI had high marks when it came to ease of use, performance, feature breadth, support access, and cost. Scalability was the one place users felt it could improve.

So in joining the Unity family of solutions, we've invested our team of experts to expand what is possible with MLAPI out-of-the-box. You'll see us providing a range of solutions for different kinds of networked experiences, starting first with small-scale, client-hosted co-ooperative game types. No other netcode out there is made within Unity, meaning MLAPI will be supported first and foremost by us here. Expect smoother integrations across the ecosystem and strong focus on widening compatibility with the platforms that Unity supports. With MLAPI being open-source, and developed in the open, you can see our velocity, direction, and contribute to the effort.

When will the new MLAPI and netcode be released to market?#

MLAPI and the new Transport Package are available in experimental phases now. Production ready releases are on their way. Please stay posted and watch our public roadmap for more info. We recommend joining our Discord and Forums (Multiplayer, MLAPI, and Transport) to watch for announcements, releases, and updates. We post news, answer questions, and share feature plans in RFCs with the community for everyone.

What are the benefits to choosing MLAPI over the original Unity networking Solution or Mirror? I currently teach Unity Networking using Mirror on the high school level.#

MLAPI is Unity's only non-deprecated, GameObject/MonoBehaviour based netcode going forward. If you appreciate your solutions all being developed together, then going with MLAPI means you'll see MLAPI sit well in the greater ecosystem of Unity's home-grown, engine technology. No other netcode out there can offer that level of integration. Nevertheless, there are some excellent options out there for you to consider from third-parties. Many are production-ready today, or currently offering support for different networking models that we're not focusing on first. So the choice is really up to you with what you stay to!

For projects that will be published in 6 months from now, what networking solution do you suggest (peer-to-peer or relay server - not simulation server)? Is MLAPI part of the new DOTS NetCode?#

Projects publishing within 6 months from now will want to go with production-ready UNet if you are looking for something that's part of 2019.4 LTS or 2020.3 LTS. UNet has its lower-level transport API used many of the world's most successful Made with Unity titles. UNet is deprecated, meaning it has no more feature development and will be removed in the near future. However, if you use it from the LTS releases of 2019 or 2020, you'll be able to count on it and our support behind it for the period of support given to each LTS.

MLAPI is in an experimental and is GameObject and MonoBehaviour based; whereas DOTS-Netcode is a separate offering targeting Unity developers working with the Entity Component System. DOTS-Netcode is also in an experimental state, so you'll have to wait for either to be ready for production scenarios when we promote the packages out of Experimental.

If none of these are ideal for your needs or timeline, we suggest looking on the Unity Asset store for well-rated, well-supported 3rd party solutions for Multiplayer.

Is MLAPI going to be a part of the new netcode?#

MLAPI is a networking solution focused on gameobject/monobehaviour based Unity projects. "Netcode" is how Unity has previously spoken about the DOTS-specific networking solution. They are two separate solutions for separate tech stacks. We know it's confusing, we're working on better naming!

MLAPI Features#

Learn more about MLAPI features and capabilities.

What is your current focus in development (feature/bugs)?#

Our focus is first on offering a supported foundation for small-scale, client-hosted co-operative type games. This is why our first release was paired with the Boss Room Co-op RPG sample. In the future, we'll focus on supporting dedicated server scenarios, with focus on lower-latency, competitive style gameplay. Just like for small-scale, client-hosted, we'll have educational content and learning materials provided for that release as well as all future ones.

Since today's version of MLAPI (0.1.0 experimental) does allow for dedicated servers for those wanting to implement those types of scenarios, we're open to taking in bugs and feedback for whatever issues you have with the MLAPI foundation along the way. Since we're open-source and developing with the community, you can also go through the RFC and Pull Request process to see if your additions are brought into the product, too! How neat is that?

Will the MLAPI Relay and ServerList be getting updates or will they be replaced by something else? What is the timeline for that?#

Thank you for your question. We are working on new Relay and Room services for what was found with UNet! We’ll have more details to share this summer.

Will you offer server rollback?#

We've not yet committed to supporting server rollback, but it is under consideration. The good news is that MLAPI is a MIT licensed open-source product. So if you have a desire to use what's there as a foundation, you can extend the system via your own custom solutions. We invite you to make custom additions for your own purposes if that suits your needs or schedule.

Will you offer server hosting?#

Server hosting is also available! We have a managed account program that you can ask to speak with someone about today at https://unity.com/products/multiplay.

Will you offer matchmaking?#

Matchmaking is currently in the works for MultiPlay customers (our dedicated game server hosting solution). Please reach out if you are interested in learning more.

Will you offer a firewall punch through (super important)?#

The Unity Transport Package won't have inherent firewall punch-through upon release. However, we will have some announcements soon about other ways you can make sure your players can be connected, such as access to a Relay service. More information will be coming this Summer.

Does it support client side prediction or similar techniques?#

Client-side prediction is on our roadmap and on its way! Please check our public roadmap and vote on your priorities and preferences!

Do you support object relevancy based on the distance away from the player those objects are not updated?#

Network relevancy and area of interest solutions are in the works for MLAPI!

Will voice chat solutions like Vivox work with netcode, or will they have to update to support netcode?#

Vivox and other voice chat solutions are compatible with MLAPI, but they are not currently integrated with the solution.

Can MLAPI support MMORPG scale?#

Right now our target for MLAPI is to support small scale games. MMORPGs are usually on the other end of that spectrum. MLAPI might be able to support some MMORPGs which are instance-based, but there might be a lot of work involved to write custom systems to improve the scalability of MLAPI. We recommend that anyone who tries to build a large scale game like an MMORPG first benchmark the solution with a proof of concept application to ensure that MLAPI meets your requirements.

Is MLAPI suited to MMORPG or would transport layer be a better option?#

With MLAPI, you might run into some scaling issues when creating an MMORPG. The Unity Transport package is a low level transport solution which will allow you to ensure that every part of the system scales well enough to support a massive player count. Using the Unity Transport Package can also allow you to build your game using a complete DOTS stack which can boost server performance a lot.

If you decide to go the MLAPI route we would recommend to start with a small proof of concept to ensure that MLAPI can hit your performance and scaling needs.

When will we see a sample for dedicated server support?#

Our focus is first on offering a supported foundation for small-scale, client-hosted co-operative type games. This is why our first release was paired with the Boss Room Co-op RPG sample. In the future, we'll focus on supporting dedicated server scenarios, with focus on lower-latency, competitive style gameplay. Just like for small-scale, client-hosted, we'll have educational content and learning materials provided for that release as well as all future ones.

Is this technology fully compatible with WebGL?#

MLAPI is platform agnostic and should work with all platforms compatible with the transport you choose to go with. Unity's Transport Package will not have WebGL on its first release, but this is something we are considering for its future. Please check our public roadmap and vote on the criticality of this feature for your needs.

MLAPI Technical Questions#

The following are more technical questions for using and extending MLAPI.

I'm a newbie. How can I start to make multiplayer games?#

Our Bitesize samples are a good starting point if you want something smaller. And then we have Boss Room, which is a larger sample. Right now we don't have a pong tutorial, but that is a good idea for the future.

Keep an eye on our Golden Path, Hello World series, and other upcoming docs and resources to help you out!

How to handle authoritative server concepts using MLAPI?#

MLAPI is built on a client-server network model utilizing a virtual star network topology. Our first release will focus on supporting client-host scenarios for small-scale cooperative type games. Fully authoritative dedicated server concepts will be our next focus which you'll see updates to MLAPI entailing within the near future. In the meantime, all the building blocks come with foundation, meaning that if you want to get started implementing your own fully authoritative server concepts, the code base should be extensible to your own custom additions.

If you want to wait for us for better prediction systems, area of interest, snapshot and delta change support, and more sophisticated authority management, you'll have to wait just a bit longer for our turn-key solutions. In the meantime, please stay in communication with us about your needs as this helps influence our prioritization and focus.

MLAPI and the Unity Engine have a set of tools to help you build authoritative dedicated servers.

Server Mode#

MLAPI can be run in server mode by starting the NetworkManager with: NetworkManager.

Singleton.StartServer();

For an authoritative server you want to run code only on the server. The IsServer property can be used in code inside NetworkBehaviours.

public void Update()
{
if(IsServer)
{
Debug.Log("This will only be called on the server");
}
}

IsServer also is true when running in host mode because a host is both a server and a client. To run code only when in dedicated server mode, use IsServer && !IsClient.

Code Stripping#

If you want to develop your server and client in the same Unity project but make sure that you do not distribute your server code to prevent hacking and exploits, use compile directives to strip server code.

Debug.Log("This is just a regular debug log");
#if UNITY_SERVER
Debug.Log("This code will not be present at all in a client build");
#endif

If you want to use compile directives with MLAPI RPCs make sure to not strip the entire RPC method but just the content.

[ServerRpc]
public void MySecretServerRpc()
{
#if UNITY_SERVER
var verySecretCalculation = 42 + 0;
// and more code...
#endif
}
Server Build#

To create a server build check the Server Build checkbox in the build settings. Server builds support many platforms including Windows, Mac OS X, Linux on x86 / x86-x64. Both Mono and IL2CPP scripting backend are supported.

A server build builds the player for server use. Enables #UNITY_SERVER define directive. Server builds don't display visual elements.

Art assets such as textures, models and sound files are still included in the build if they are referenced in one of your scenes. Stripping them can reduce server build size and memory usage of the server.

How to use in standalone .net core server?#

MLAPI can only be used with a Unity based server. We do not support standalone .net core servers. You can create a headless standalone Unity application using our Server Build feature. When using MLAPI in a listen-server mode a .net core based relay server could be used to transfer data between clients.

What’s the strategy for code migration from legacy 3rd parties libraries like PUN?#

There is not a single strategy which can be applied to any third party library. Libraries which use a similar star topology design like MLAPI, for instance UNet or Mirror can have quite straight forward migration paths. Others like PUN which are based on a cloud relay service and allow all clients to communicate with each other could be much harder to migrate.

For UNet we have a migration guide which can be a good starting point.

Start a project with the network in mind VS creating, testing, and polishing a local game and THEN convert to network?#

In most cases we would recommend starting with networking in mind if networking is a core feature of your game and you are sure that it will make it into a release. With MLAPI a multiplayer game also always just works as a single player game by running in host mode and just not letting anyone else connect.

Going from single player to multiplayer on the other hand can be tricky. Some code or game designs might not be easily portable to multiplayer. A Reason why one would decide to start with single player would be that it is much easier to create a single player game.

How many different ways can an online multiplayer game be hosted? Does it have to be through Unity's services?#

MLAPI can be used with any service / hosting solution. It does not have to go through Unity's services. We support external services for both hosting dedicated servers and for a listen-server based host approach over a relay service.

The transport layer of MLAPI allows you to easily run MLAPI with any networking service including platform specific networking solutions.

What are the precautions we should take with respect to security / hacking / misuse?#

Security / Misuse#

Generally speaking game servers are pretty secure because their protocol is fairly simple and does not allow for remote execution of code. In MLAPI for instance an incoming package can always only call an RPC but not run any other code.

To write secure code with MLAPI you want to make sure that your game play code does not expose any security risks for any given input by a client.

We do not recommend doing the following.

// A malicious client could send a tempered packet and include anything in this string not just values which the initial game code allowed.
[ServerRpc]
public void DoNotDoThisServerRPC(string command)
{
// This is highly insecure because <command> could be anything
Process.Start("CMD.exe", command);
}
Hacking#

In games hack clients often give the player an unfair advantage. This could be receiving more information such as looking through walls, aimbots or in some cases a hack client

  • Wallhacks: Use NetworkShow/NetworkHide to hide information from players which they can't see.
  • Aimbots: No easy way to prevent them. On the server side you can check for unrealistic input. For instance if a player has very low reaction time and always pin point accuracy they might be using an aimbot.
  • Value Manipulation: To prevent hackers from just manipulating variables like health to any value you can use NetworkVariables and only allow the server to write to the variable. In addition when a client makes an RPC to the server you can validate that the parameters passed are legal.

Hackable RPC:

[ServerRpc]
void DealDamage(int targetId, int damage)
{
enemies[targetId].DealDamage(damage);
}

Hack-proof RPC:

[ServerRPC]
void DealDamage(int targetId){
// check that the player can actually do this action. If you don't do this, a hacker could send an RPC multiple times.
if(canDealDamage)
{
// server decides how much damage is dealt
int dmg = 10;
enemies[targetId].DealDamage(damage);
}
}
Authentication / Login#

Make sure that when your players log into the game that their user information such as passwords are authenticated and encrypted when transmitting them. Many games use HTTPS based approaches for security reasons.

The same applies for important transactions such as buying items in a cash shop etc. How would one go about unifying local and online multiplayer through MLAPI? Allowing several players on one client to play with several from another client/host.

One way to achieve that would be to create a separate network object foreach local player on the own client. You could use an id to identify which object belongs to which input device on each client. Pseudo code:

class PlayerSpawner : NetworkObject
{
private GameObject m_PlayerPrefab;
public void SpawnAdditionalPlayerServerRpc(int localPlayerId, ServerRpcParams serverParams = default )
{
var sender = serverParams.Receive.SenderClientId;
var playerObject = Instantiate(m_PlayerPrefab);
playerObject.GetComponent<PlayerController>.LocalPlayerId.Value = localPlayerId;
playerObject.GetComponent<NetworkObject>().SpawnWithOwnership(sender);
}
}
class PlayerController: NetworkBehaviour
{
public NetworkVariable<int> LocalPlayerId = new NetworkVariable<int>();
public void FixedUpdate(){
if(IsOwner){
// Use LocalPlayerId.Value here to map your input devices to the right player object
float input = //....
// the just regularly move with that input
transform.Tranlate(new Vector3(input * Time.fixedDeltaTime, 0, 0));
}
}
}

Is it possible to raise events on specific entities?#

Entities in MLAPI are called NetworkObjects. RPCs are remote calls which always run on the same NetworkObject from which they have been called from. So to raise an event on a specific entity with MLAPI you can just call client or server RPCs on the right NetworkObject.

What's the best way to go about implementing a weapon with a high fire rate?#

There are many ways to approach a gameplay feature like that. One key idea in netcode is that you want to synchronize as little data as possible. One approach to achieve a high fire rate weapon would then be to just synchronize the inputs with the server and let the server shoot the bullets based on those inputs.

class ExampleWeapon : NetworkBehaviour
{
private NetworkVariable<bool> m_IsShooting = new NetworkVariable<bool>(new NetworkVariableSettings { WritePermission = NetworkVariablePermission.OwnerOnly });
private float m_FireRate = 100; // per second
public void FixedUpdate()
{
if(IsOwner)
{
m_IsShooting.Value = Input.GetKey(KeyCode.Mouse0);
}
if(IsServer){
var bulletsFired = Time.fixedDeltaTime * m_FireRate // this can be done more accurately
// foreach bullet do a raycast, deal damage etc. Or better one raycast for all bullets in the same frame.
}
}
}

How to use the MLAPI to build a deterministic rollback based networked game?#

MLAPI uses a state transfer model which is very different from a deterministic rollback input sync based model. This does not mean that you cannot create a deterministic rollback based game with MLAPI but you won't be able to use the high level components such as NetworkObject, NetworkBehaviour, RPCs or NetworkVariables for game play.

To pass the inputs between your clients you can use CustomNamedMessages. When using deterministic rollback you have to make sure that your simulation is fully deterministic even across different platforms and CPU architectures. This often means that existing libraries for gameplay such as physics or path finding can't be used or must be modified to be deterministic.

The Unity Transport Package could often be the better choice to create a deterministic rollback based solution as it still provides networking functionality for passing inputs but not any of the high level components.

How does MLAPI perform over the network compared to solutions such as Mirror and DarkRift2?#

At this time, we're not releasing any of our performance comparisons for MLAPI versus alternative technologies as performance outcomes are highly dependent on specific usage.

How performant are RPCs compared to classic serialized data/bitwise messages with methods on either end to process the data?#

RPCs are designed to be very lightweight. They almost directly go into serialization and don't have much overhead. Each RPC call includes a NetworkId, BehaviourIndex and rpc hash overhead though so that the RPC can be run on the correct behaviour on the other side. With custom messages if you could implement your own system to redirect calls to the right objects which might be more lightweight.

Another key difference is that RPCs get batched by default by our RPC queue while custom messages might not get batched based on the transport used. This can also affect performance and bandwidth used.

What is the best way to synchronize physics between players?#

There are many ways in which physics can be done in multiplayer games. MLAPI currently does not interact with the physics system. Instead you can build the physics in your game however you want. Here are some ideas for how you can do physics:

Server Authoritative Physics#

You can run the physics simulation fully on the server. The physics objects on the client would then just replicate the positions and rotations they receive from the server. This is a very easy way to get consistent physics into a multiplayer game but there will be noticeable latency for players with slower connections.

To achieve this with MLAPI you would simply set the rigidbodies of your network objects to kinematic on everyone but the server.

Client Authoritative Physics#

Another way to do physics is to give each client control over a set of objects and have them run the physics for those objects and replicate it to everyone else. This reduces latency but can lead to some strange interactions if two physics objects collide which are owned by different players.

In MLAPI you can get client authoritative physics by setting a rigidbody to kinematic on everyone but the owner client.

Predicted Physics#

Some games which have gameplay on fast realtime physics might need to predict the physics in their game. MLAPI does not currently provide any built in way for prediction so a system like that would have to be custom written.

To be able to run multiple instances of MLAPI in the same environment, singletons need to be removed from the code base. Is this a feature you are considering (and if not, why)?#

We just recently made some changes on our develop branch to remove dependencies on the singleton patterns from MLAPI. On our develop version you are now technically capable of running multiple instances of MLAPI in the same environment.

While it is possible, what we have so far is more of a proof of concept and will mainly be used for internal use such as testing. Advanced users might be able to use this feature with the next version but we are not yet at a point where we have a streamlined API which is easy to use.

Documentation#

Learn more about our open source documentation.

When will the netcode package get proper documentation?#

You can find documentation for MLAPI and the low-level transport at https://docs-multiplayer.unity3d.com/. The documentation will continue to improve and evolve as we continue to develop the solution. DOTS' netcode solution will have more complete documentation as it moves closer to release.

What’s the best way to contribute to the documentation?#

We accept feedback, edits, and new content through a few different ways:

  • On each webpage, you can select a link to edit the page. This link will open the correct page to edit in GitHub.
  • On each webpage, you can select a link to log an issue. This opens a GitHub issue to provide feedback on the page you are viewing.
  • Provide feedback in the documentation channel on Discord.
  • Visit the Contributing Guide to learn more about adding pages, submitting pull requests, and more. Our documentation is open source just like our MLAPI code.