void _OnHostPressed() { //Create peer this.peer = new NetworkedMultiplayerENet(); this.peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.RangeCoder; Error error = this.peer.CreateServer(PORT, 8); if (error != Error.Ok) { this.ShowError("Error: Unable to host server\nPerhaps there is alreay a server open?"); return; } this.p1Id = 1; this.p2Id = 0; GetTree().NetworkPeer = this.peer; Lobby.state = GameState.CHAR_SELECTION; //Hosts should start out as a spectator Lobby.role = MultiplayerRole.P1; Lobby.highLatency = false; this.Visible = false; //Goto CS as spectator CharSelection cs = (ResourceLoader.Load("res://scenes/ui/char_selection/char_selection.tscn") as PackedScene) .Instance() as CharSelection; GetTree().Root.AddChild(cs); //Set the character selection up for multiplayer, with this as a spectator cs.SetMp(); cs.SetCallback(this._MpCSCallback); cs.PlayersUpdated(true, false); //Get the player connected sprites set correctly }
/* * Which of these is called when? * If a player has changed -> Call SetPlayerId(...) on all clients * If a player hasn't changed -> Call SetupClient on just the new spectator */ //Called whenever the game has to be setup //This is called internally when the players have changed //Or by the server if you join late as a spectator private void SetupClient(int p1Id, int p2Id, bool highLatency) { //Set the role correctly if (p1Id == GetTree().GetNetworkUniqueId()) { Lobby.role = MultiplayerRole.P1; } else if (p2Id == GetTree().GetNetworkUniqueId()) { Lobby.role = MultiplayerRole.P2; } else { Lobby.role = MultiplayerRole.SPECTATOR; } Lobby.highLatency = highLatency; //Set the player ids this.p1Id = p1Id; this.p2Id = p2Id; //It doesn't matter what state you were in, once this is called you're in char selections Lobby.state = GameState.CHAR_SELECTION; //Case reseting lobby CharSelection cs = (ResourceLoader.Load("res://scenes/ui/char_selection/char_selection.tscn") as PackedScene) .Instance() as CharSelection; GetTree().Root.AddChild(cs); //Set the character selection up for multiplayer, with this as a spectator cs.SetMp(); cs.SetCallback(this._MpCSCallback); cs.PlayersUpdated(p1Id != 0, p2Id != 0); this.ResetSpectators(); }