Skip to main content

Creating Networked Items

Creating a networked item is crucial for most multiplayer games like collectible coins or shooting bullets.

Network Item Manager

The NetworkedItemManager is a component in the NetworkManager prefab that manages all the items in a game. This component needs to be referenced in a script to spawn or delete the synchronized item.

This component requires a networked item factory reference in order for a script to spawn and manage the factory items.

Item Manager

Network Custom Item Factory

This component needs the Game Object prefab of the networked item containing the necessary components. The name property of the model needs to be referenced in a script to spawn the prefab.

Item Factory

Networked Item Components

In order for a Game Object prefab to be synchronized to all clients it requires these three components:

  1. WorldItemNetworkTransform - the core class that is synchronizing the data between the local and remote controller classes.
  2. LocalWorldItemTransformController - the source of truth that sends the transform data up to the server to be synchronized.
  3. RemoteTransformController - replicator of the data from the server to the client.

Networked Components

info

In the WorldItemNetworkTransform component, make sure the Sync Data Types property list includes the necessary transform properties that should be synced.

Networked Item Prefab

A networked item prefab needs the three components listed above for it to be synchronized across all clients. Then the prefab needs to be added to the Network Custom Item Factory.

Networked Item API

Network Item Manager Class

The NetworkItemManager class manages all the creation and deletion of networked items. It has listener methods for local and remote items, as well as methods for spawning and deleting local items.

import { NetworkItemManager } from "Genies.Components.Sdk.External.Multiplayer";
import { ItemControlType } from "Genies.Components.Sdk.External.Multiplayer.Item";

let itemManager: NetworkItemManager;

//Item Manager Events
itemManager.OnLocalItemCreated;
itemManager.OnRemoteItemAdded;
itemManager.OnRemoteItemCreated;
itemManager.OnRemoteItemRemoved;
itemManager.OnRemoteItemUpdated;

//Item Manager Methods
itemManager.SpawnLocalItem();
itemManager.DeleteLocalItem();

Network Item Class

The NetworkItem class manages the networked item entity. It has a reference to the server MMOItem property. It also has a subclass NetworkWorldItem that has a reference to the local GameObject.

import { NetworkWorldItem, NetworkItem } from "Genies.Components.Sdk.External.Multiplayer.Item";
import { MMOItem } from "Sfs2X.Entities";

//Network Item Class References
let item: NetworkItem;
let mmoItem: MMOItem = item.MmoItem;
let worldItem: NetworkWorldItem = item as NetworkWorldItem;

//Network Item Properties (Networked)
item.Id;
item.MmoItem;

//MMO Item Properties (Server)
mmoItem.Id;
mmoItem.AOIEntryPoint;

//Network World Item Properties (Client)
worldItem.Id;
worldItem.GameObject;
worldItem.DoNotDestroyGameObjectOnDestroy;
note

The Network World Item class references each have an Id property that cab be used to indicate which item this is on the server and across clients.

TypeScript Example

Here is a full script that spawns, updates, and deletes a networked item:

import {GameObject, MonoBehaviour, Random, Vector3} from "UnityEngine";
import {NetworkItemManager, NetworkManager} from "Genies.Components.Sdk.External.Multiplayer";
import {ItemControlType, NetworkWorldItem, NetworkItem} from "Genies.Components.Sdk.External.Multiplayer.Item";
import { MMOItem } from "Sfs2X.Entities";

export default class ItemCreator extends MonoBehaviour {

private item: NetworkWorldItem;

private Start(): void {
this.CreateItem();
}

private async CreateItem(): Promise<void> {
//Get reference to NetworkItemManager
const networkItemManager = NetworkManager.Instance.Get<NetworkItemManager>();
//Spawn a NetworkItem prefab
let item: NetworkItem = await networkItemManager.SpawnLocalItem("follower", "box", null, ItemControlType.Mixed);
//print the MMOItem Id
let mmoItem: MMOItem = item.MmoItem;
console.log("Item Spawned: ", mmoItem.Id);
//Store the NetworkWorldItem subclass
this.item = item as NetworkWorldItem;
}

private async DeleteItem(): Promise<void> {
var networkItemManager = NetworkManager.Instance.Get<NetworkItemManager>();
//Change the option to not destroy the Game Object (dying animation for example)
this.item.DoNotDestroyGameObjectOnDestroy = true;
//Delete the local item from Item Manager
await networkItemManager.DeleteLocalItem(this.item);
//Destroy the local GameObject
GameObject.Destroy(this.item.GameObject);
}

private UpdateItem(item: NetworkWorldItem) {
var randomXSpawn = Random.Range(-5, 5);
var randomZSpawn = Random.Range(-10, 10);
//Change the item position
item.GameObject.transform.position = new Vector3(randomXSpawn, 0, randomZSpawn);
}
}