Interest Groups
You can imagine Photon's "Interest Groups" as sub-channels for conversations in a room:
Clients only get the messages of Interest Groups they subscribed to (and group 0).
They can send any event to any group they want to.
This simple feature can be used for basic Interest Management or any way you see fit.
See the example use cases below.
Available Groups
Clients do not need to explicitly create interest groups.
Interest groups are created on demand; when an actor subscribes to a new group number, the server will create it.
Photon offers up to 256 interest groups.
The group number 0 is reserved and meant for broadcast: All actors (clients) inside a room are subscribed to group 0 and cannot unsubscribe from it.
The other 255 groups are available to the developer to use freely.
Any event assigned to a group > 0 will only be transmitted to clients that are interested in that group and in the room when the server relays the event.
Global Setup
The list of subscribed groups can be updated at any time inside the room by adding or removing their numbers using:
SetInterestGroups
SetInterestGroups
tells the server, which groups your client is interested in.
By default, this is group 0 but no others, so you have to subscribe to some groups, if you want to use this.
To globally filter which interest groups the client receives events from:
C#
void PhotonNetwork.SetInterestGroups(byte[] disableGroups, byte[] enableGroups)
You should prefer using the bulk updates but you can also toggle reception for individual Interest Groups:
C#
void PhotonNetwork.SetInterestGroups(byte group, bool enabled)
SetSendingEnabled
When using SetSendingEnabled
, the client may locally discard updates going to specific groups - they simply never reach the server (there is no server-side logic for this).
To globally define which interest groups the client should send events to, use:
C#
void PhotonNetwork.SetSendingEnabled(byte[] disableGroups, byte[] enableGroups)
To toggle a single sending interest group:
C#
void PhotonNetwork.SetSendingEnabled(byte group, bool enabled)
By default, the client sends updates to all groups, which is most likely what you want.
Notes:
- Priority is always to group addition: if the same group number is added to both
enableGroups
anddisableGroups
arrays then the group will be added. - A
null
array acts as "no group" and an empty array (new byte[0]
) acts as "all groups". PhotonNetwork.SetInterestGroups(new byte[0], enableGroups)
will remove all groups except the ones inenableGroups
.PhotonNetwork.SetInterestGroups(disableGroups, new byte[0])
will add all currently existing (used) groups, no matter what the value ofdisableGroups
is.
Usage Guide
Instantiate
PhotonNetwork.Instantiate
methods accept a group
parameter that will set the Interest Group of the PhotonView attached to the prefab.
group
other than the default 0 to PhotonNetwork.Instantiate
, make sure to enable receiving events from that group before.
Otherwise the prefab will not be instantiated locally.
PhotonView
PhotonViews attached to prefabs will automatically get the Interest Group passed to the PhotonNetwork.Instantiate
call.
By default, it is 0.
If it's not 0, joining clients will likely not execute Instantiate for groups != 0.
You can set or update a PhotonView's Interest Group via code as follows:
C#
photonView.Group = interestGroupCode;
Setting a view's group is done locally only - remote instances keep the group which was set when instantiating the GameObject.
PunRPCs and PhotonView serialization/synchronization events will be sent only to the Interest Group configured for the respective PhotonView unless it was disabled using SetSendingEnabled
.
So usually, the updates can be filtered server-side with the SetInterestGroup settings of the receiving clients.
RaiseEvent
To set the target Interest Group when calling PhotonNetwork.RaiseEvent
use RaiseEventOptions.InterestGroup
.
Example Use Cases
Interest Groups to subscribe to, per client, can be defined dynamically at runtime or statically at compile time.
Interest Groups setup can be the same on all clients or different per client.
Interest Groups are useful to lower the number of messages per second inside rooms.
By lowering traffic, you stay below the message/second limit, cut costs and sometimes it can help you increase the number of maximum players per room.
But you can come up with other clever ways of using Interest Groups in your game.
Network Culling
Most common use case of Interest Groups is Network Culling.
Interest Groups could be mapped to areas of interest in your game.
For instance if you have a "big fat world" you can separate it into smaller chunks, let's call them "areas" and assign an interest group per "area".
Team Events
If you have teams in your game and want to implement team exclusive events, you can assign an interest group per team.
All team members should subscribe to the team's interest group.
Inter team events should be sent using the team's own group number.
Intra team events should be sent using an opponent team's group number.