Hello!
I’ve been wrestling with this issue for quite some time now. Firstly here’s the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Room : MonoBehaviour {
private bool __enable_start = false;
void OnLevelWasLoaded() {
if (Network.isClient) this.networkView.RPC("RegisterPlayer", RPCMode.Server, ToolBox.SerializeBase64(Player.instance.player_data));
}
void OnGUI() {
Game game = Player.instance.game;
if (game != null) {
GUILayout.BeginArea(new Rect(10, 10, Screen.width - 20, Screen.height - 20));
GUILayout.BeginHorizontal();
GUILayout.BeginVertical(GUILayout.Width(300), GUILayout.ExpandWidth(false));
this.__enable_start = true;
//teams
int team_count = game.teams.Count;
GUILayout.Label("Teams");
GUILayout.Space(10);
for (int i = 0; i < team_count; i++) {
GUI.enabled = true;
GUILayout.Space(10);
GUILayout.Label(game.teams*.team_name);*
_ if (game.teams*.players.Count > 0) {_
_ for (int j = 0; j < game.teams.players.Count; j++) {
GUILayout.BeginHorizontal();
GUILayout.Box(game.teams.players[j].name());_
if (game.teams.players[j].id() == Player.instance.player_data.id()) {
_ if (GUILayout.Button(“Leave team”, GUILayout.Width(100))) {_
game.LeaveTeam(Player.instance.player_data);
_ }
}
GUILayout.EndHorizontal();
}
} _
else this.__enable_start = false;*
int open_slots = (game.max_players / team_count) - game.teams*.players.Count;
GUI.enabled = !game.HasTeam(Player.instance.player_data);
if (open_slots > 0) {
for (int k = 0; k < open_slots; k++) {
_ if (GUILayout.Button(“Open slot”)) {_
game.JoinTeam(game.teams, Player.instance.player_data);
_ }
}
}
}*_
* GUILayout.EndVertical();*
* GUILayout.Space(20);*
* GUILayout.BeginVertical(GUILayout.Width(100), GUILayout.ExpandWidth(false));*
* //players*
* GUI.enabled = true;*
* GUILayout.Label(“Players”);*
* GUILayout.Space(10);*
* foreach (PlayerData player in game.players) {*
* GUILayout.Space(10);*
* GUILayout.Box(player.name());*
* }*
* GUILayout.EndVertical();*
* GUILayout.Space(20);*
* GUILayout.BeginVertical(GUILayout.Width(100), GUILayout.ExpandWidth(false));*
* //game options*
* GUI.enabled = Network.isServer;*
* GUILayout.Label(“Game options”);*
* GUILayout.Space(20);*
* game.type = (Game.Type) GUILayout.SelectionGrid((int) game.type, new string[]{“Deatchmatch”, “CTF”}, 1);*
* GUILayout.Space(20);*
* if (!(game.players.Count > 0)) this.__enable_start = false;
GUI.enabled = this.__enable_start && Network.isServer;
_ if (GUILayout.Button(“Start game!”)) {
Debug.Log(“Starting game”);
}*_
* GUI.enabled = true;*
* if (GUILayout.Button(“Disconnect”)) {*
* Player.instance.network_connector.Disconnect(200);
_ }*_
* if (GUILayout.Button(“Force”)) {*
* this.networkView.RPC(“RegisterGame”, RPCMode.Others, ToolBox.SerializeBase64(Player.instance.game));*
* }*
* GUILayout.EndVertical();*
* GUILayout.EndHorizontal();*
* GUILayout.EndArea();*
* }*
* } *
* void OnApplicationQuit() {*
if (Network.isServer) ToolRequest.WWWRequestXML(this, “cancelgame”, “http://hidden_for_security?action=cancel&id=” + Player.instance.game.id);
* }*
* void OnDisconnectedFromServer() {*
if (Network.isServer) ToolRequest.WWWRequestXML(this, “cancelgame”, “http://hidden_for_security?action=cancel&id=” + Player.instance.game.id);
* Player.instance.game = null;*
* SceneManager.instance.LoadLevel(“lobby”);*
* }*
* void OnWWWResponseReceived(Response response) {*
* if (response.identifier == “cancelgame”) SceneManager.instance.LoadLevel(“lobby”);*
* }*
* [RPC]*
* void RegisterPlayer(string serialized_player) {
PlayerData player = (PlayerData) ToolBox.DeserializeBase64(serialized_player);
_ Player.instance.game.JoinGame(player);
this.networkView.RPC(“RegisterGame”, RPCMode.Others, ToolBox.SerializeBase64(Player.instance.game));
}*_
* [RPC]*
* void RegisterGame(string serialized_game) {
Game game = (Game) ToolBox.DeserializeBase64(serialized_game);
_ Player.instance.game = game;
}*_
}
So the issue here can be described as the following: I have a host starting a server which has a variable name accessible through Player.instance.game (Player is an extended MonoBehaviour with a NetworkView component). Now when another player connects its sends its data to the player’s game instance with a RPC [Player.instance.game.JoinGame(player);]. This works for the host(!), in response to this the host sends the player a copy of his game object, also with a RPC, but somehow, this RPC does not get triggered. Also when I force the RPC mid game (so when both levels are the same and loaded, and both the client and host have the same scripts active and running) it doesn’t work either.
Does anybody have any idea what is going wrong?
Thnx!