Learn GONet

We believe learning through examples is powerful. Please take some time to review the examples and tutorials available in order to learn how to get the most out of GONet to make your multi-player Unity game all your players deserve.


Subscribe to the GONet youtube channel to keep up to date with the latest video content available. https://www.youtube.com/channel/UClkvaQmqFgu2DdXBj9c1fMA

A Simple and Complete Start-to-Finish Tutorial


Here are the most important GONet C# classes to know about when starting out:

  • GONetParticipant (i.e., GNP) – add this to any GameObject you want networked
    • Just adding this will allow you to auto-magically sync Transform data
    • GameObject must be in the scene hierarchy or in /Resources sub-folder in the Project
    • If on a prefab in a /Resources sub-folder, ensure only the single “owner” machine calls the standard Unity Instantiate(prefab) method, because GONet will automatically network the instantiation on other non-owner machines
    • In the Inspector, this class will show you ALL things GONet currently knows about to sync for you on this GameObject. To add/remove things to sync, see [GONetAutoMagicalSync] below; then, look into adding Animator Controller and see the parameters show up as options to sync.
  • GONetAutoMagicalSyncAttribute – add this C# attribute to any public field or property on any class that extends MonoBehaviour that is also on the GameObject that also has GONetParticipant added. This will cause an automatic sync of changes to this value to all other players.
    • For anyone familiar with Unity’s [SyncVar] will understand how to use [GONetAutoMagicalSync], but will see there is additional capability here
  • GONet convenience classes that extend MonoBehaviour:
    • GONetBehaviour – Provides a base class with commonly used hooks into the GONet API that might be easier to use for beginners before they are familiar with GONet’s event API (i.e., GONetMain.EventBus).
      • The most common example is when you want to execute some code as new GONetParticipants are added into the scene (e.g., player/enemy/projectile spawned). To do that, just override the OnGONetParticipantStarted method.
    • GONetParticipantCompanionBehaviour – For GameObjects that have a GONetParticipant “installed” on them, the other MonoBehaviours also “installed” can optionally extend this class to automatically have a reference to the GONetParticipant instance to reference it when making decisions on what to execute.
      • The most common example is to use GONetParticipant.IsMine to know whether or not to execute some game logic or not so that the logic is only executed on the owner’s machine and the networking will handle the rest so the other machines will see the results of the game logic being executed by “the owner.”
  • GONetMain – use this static C# class to access all sorts of interesting GONet capabilities
    • IsServer/IsClient – check if code is running on the server and/or a client
    • Time – network sync’d elapsed seconds since start of “session” (clients and server will have the ~same value for this)
    • EventBus – this is one of the most powerful things GONet offers. Publish and subscribe to network events

Supported Auto-Magical Sync Types

The following data types are the only ones that can be used with [GONetAutoMagicalSync] C# attribute on a MonoBehaviour field/property:

  • bool
  • byte, sbyte, short, ushort, int, uint, long, ulong
  • float, double
  • Vector2/3/4
  • Quaternion

Notice these are all value types (see the GONetSyncableValueTypes enum in code). However, GONet can (de)serialize all sorts of reference types (e.g., List<blah>) and send them across the network through the GONet event API (see GONetMain.EventBus).

Deeper Dive on Events (and Replay)

A common operation in video game networking is sending commands across the network to remote machines for processing (i.e., Remote Procedure Call, RPC) so all sides are doing the same thing in unison. Another seemingly unrelated common feature people want in multi-player video games is the ability to replay previous game play. GONet does not have a direct RPC implementation, but rather uses events and the reason is how it all fits into our Record+Replay feature (upcoming in PRO version). Also, video games are highly event-drive by nature, so picking up the GONet event syntax/semantics is worthwhile and hopefully not too time consuming.

For RPC-like behavior, you would create a new event struct/class that contains fields/properties that need to get networked. That new event struct/class has to do two things:

(1) implement IGONetEvent or one of its child interfaces like ITransientEvent or IPersistentEvent

(2) be decorated with C# attributes [MessagePackObject] (at the struct/class level) and [Key] (at the fields/properties level) for proper serialization

Then in order to use it, create an instance of the event and pass it into
GONetMain.EventBus.Publish(yourEvent). That will network the event to all machines connected. To process on the other side, you have to subscribe for that event: GONetMain.EventBus.Subscribe<YourEventType>(YourEventHandlerMethod).

With events, you can initiate an action on one machine and through publishing it, all machines in the network will receive the event and handle it accordingly. An example of an event that GONet uses internally when GameObject.Instantiate() is called for a GONetParticipant is InstantiateGONetParticipantEvent. Search the codebase for its definition to review how an event struct/class of your own would be structured.

NOTE: GONet event API requires knowledge of C# generics.

%d bloggers like this: