# Documentation

{% hint style="success" %}
**MADNET** uses the singleton design pattern. To access the current instance of NetworkManager, use `NetworkManager.Instance`
{% endhint %}

## Writing Networked Scripts:

Create a new C# script in Unity and inherit `NetworkBehavior`&#x20;

#### Flow logic:

Movement code should be client-authoritative, while certain codes, such as validation of a task's completion, are best done on the server. Client and server communication is established with `NetworkInvoke()`

To invoke a function on all connected clients, call `NetworkInvoke("FUNCTION_NAME", object[] parameters, bool includeOwner)`

If `includeOwner = true`, the function will be invoked again on the calling client

`NetworkIdentity.isLocalPlayer` returns true for the current local client that has authority, useful for player control scripts where only the object with authority should respond

## Adding Movement:

Attach [`NetworkTransform`](https://github.com/MadkevOP7/MADNetworking/blob/main/MAD%20Networking/Core/Components/NetworkTransform.cs) to any `GameObject` you wish to sync transform with.&#x20;

Sync Options:&#x20;

* Position
* Rotation
* Scale

{% hint style="info" %}
**NetworkTransform** can be adjusted based on the type of game you are making. For slower pace games, increase the `Sync Interval`; for faster pace games, ie. FPS Shooter, decrease the `Sync Interval` for more accuracy. Lower `Sync Interval` increases network usage.&#x20;
{% endhint %}

## Spawning GameObjects:

Register a prefab by adding it to `NetworkManager`'s `registeredPrefabs`. You can get a GameObject's registered `NetId` using `GetRegisteredSpawnPrefabID(GameObject gameObject)`

{% hint style="success" %}
Player prefab is automatically registered when added to `NetworkManager`'s `playerPrefab` field.
{% endhint %}
