Choice of character

Hello!

Sorry for the trouble, but could you tell me the right way.
I create a game on AdvancedTutorial and I want to organize a simple selection of characters,
and I did it simply, I added the if else construct to the InstantiateEntity(); method.

This all works for the one who creates the game,
but does not work for the one who joins!

Player who joined automatically get a creator character, but the creator can choose any character!

Why this method does not work for those who joined?
You could not tell me in which direction I need to work to fix this?

Comments

  • Can we see the script?
  • stanchion said:

    Can we see the script?


    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System;
    using System.Linq;
    using UE = UnityEngine;

    namespace Bolt.AdvancedTutorial
    {

    public partial class Player : IDisposable
    {
    public const byte TEAM_RED = 1;
    public const byte TEAM_BLUE = 2;


    public string name;
    public BoltEntity entity;
    public BoltConnection connection;

    public IPlayerState state {
    get { return entity.GetState (); }
    }

    public bool isServer {
    get { return connection == null; }
    }

    public Player ()
    {
    players.Add (this);
    }

    public void Kill ()
    {
    if (entity) {
    state.Dead = true;
    state.respawnFrame = BoltNetwork.serverFrame + (15 * BoltNetwork.framesPerSecond);
    }
    }

    internal void Spawn ()
    {
    if (entity) {
    state.Dead = false;
    state.health = 100;

    // teleport
    entity.transform.position = RandomSpawn ();
    }
    }

    public void Dispose ()
    {
    players.Remove (this);

    // destroy
    if (entity) {
    BoltNetwork.Destroy (entity.gameObject);
    }

    // while we have a team difference of more then 1 player
    while (Mathf.Abs (redPlayers.Count () - bluePlayers.Count ()) > 1) {
    if (redPlayers.Count () < bluePlayers.Count ()) {
    var player = bluePlayers.First ();
    player.Kill ();
    player.state.team = TEAM_RED;
    } else {
    var player = redPlayers.First ();
    player.Kill ();
    player.state.team = TEAM_BLUE;
    }
    }
    }




    public void InstantiateEntity ()
    {



    //Choose prefab depending on the number
    if (Storage.PlayerChoose == 0) {
    entity = BoltNetwork.Instantiate (BoltPrefabs.Player, new TestToken (), RandomSpawn (), Quaternion.identity);

    }

    else if(Storage.PlayerChoose == 1){
    entity = BoltNetwork.Instantiate (BoltPrefabs.Player1, new TestToken (), RandomSpawn (), Quaternion.identity);
    }

    else if(Storage.PlayerChoose == 2){
    entity = BoltNetwork.Instantiate (BoltPrefabs.Player2, new TestToken (), RandomSpawn (), Quaternion.identity);
    }

    else if(Storage.PlayerChoose == 3){
    entity = BoltNetwork.Instantiate (BoltPrefabs.Player3, new TestToken (), RandomSpawn (), Quaternion.identity);
    }

    else if(Storage.PlayerChoose == 4){
    entity = BoltNetwork.Instantiate (BoltPrefabs.Player4, new TestToken (), RandomSpawn (), Quaternion.identity);
    }

    else if(Storage.PlayerChoose == 5){
    entity = BoltNetwork.Instantiate (BoltPrefabs.Player5, new TestToken (), RandomSpawn (), Quaternion.identity);
    }

    else if(Storage.PlayerChoose == 6){
    entity = BoltNetwork.Instantiate (BoltPrefabs.Player6, new TestToken (), RandomSpawn (), Quaternion.identity);
    }

    else {
    entity = BoltNetwork.Instantiate (BoltPrefabs.Player, new TestToken (), RandomSpawn (), Quaternion.identity);
    }
    //Choose prefab depending on the number

    state.name = name;
    state.team =
    redPlayers.Count () >= bluePlayers.Count ()
    ? TEAM_BLUE
    : TEAM_RED;

    if (isServer) {

    entity.TakeControl (new TestToken ());
    } else {

    entity.AssignControl (connection, new TestToken ());
    }

    Spawn ();


    }

    }

    partial class Player
    {
    static List players = new List ();

    public static IEnumerable redPlayers {
    get { return players.Where (x => x.entity && x.state.team == TEAM_RED); }
    }

    public static IEnumerable bluePlayers {
    get { return players.Where (x => x.entity && x.state.team == TEAM_BLUE); }
    }

    public static IEnumerable allPlayers {
    get { return players; }
    }

    public static bool serverIsPlaying {
    get { return serverPlayer != null; }
    }

    public static Player serverPlayer {
    get;
    private set;
    }

    public static void CreateServerPlayer ()
    {
    serverPlayer = new Player ();
    }

    static Vector3 RandomSpawn ()
    {
    float x = UE.Random.Range (-32f, +32f);
    float z = UE.Random.Range (-32f, +32f);
    return new Vector3 (x, 32f, z);
    }

    }

    }
  • What is "Storage.PlayerChoose" ? You probably aren't sending this from the client, it should be sent either in the connect token or an event.
  • stanchion said:

    What is "Storage.PlayerChoose" ? You probably aren't sending this from the client, it should be sent either in the connect token or an event.

    //No, this works, it is a variable with the value int.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Storage : MonoBehaviour {

    public static int PlayerChoose;
    }

    //With the help of a singleton I give players the ability to set a unique value that affects the choice

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Singltone : MonoBehaviour {

    private static Singltone _instance;
    public static Singltone Instance { get { return _instance; } }



    // Use this for initialization
    void Start()
    {

    _instance = this;
    DontDestroyOnLoad(gameObject);
    }


    public void upPl(int pl){
    Storage.PlayerChoose = pl;
    }
    }


    //The selection button calls the value assignment function
    //And passes a unique parameter that is bound to a button

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ButtonChoose : MonoBehaviour {

    public int person;


    public void Choose(){

    Singltone.Instance.upPl(person);
    }
    }

    //And in this script I do a check whether the value has changed or not
    //And everything works, the values ​​change, but there are no characters established by these values

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Test : MonoBehaviour {

    void OnGUI()
    {
    GUI.Box(new Rect(Screen.width - 200, Screen.height - 33, 200, 33), "SinglNumber : " + Storage.PlayerChoose);


    }
    }
  • //I think the problem is in the ServerCallbacks script
    //It seems to me, according to this logic, the one who creates, indicates all the others how to do it
    //I do not know how to change this

    using System.Linq;
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;

    namespace Bolt.AdvancedTutorial {

    [BoltGlobalBehaviour(BoltNetworkModes.Host, "Level1")]
    public class ServerCallbacks : Bolt.GlobalEventListener {
    public static bool ListenServer = true;

    void Awake() {
    if (ListenServer) {
    Player.CreateServerPlayer();
    Player.serverPlayer.name = "SERVER";
    }
    }

    void FixedUpdate() {
    foreach (Player p in Player.allPlayers) {
    // if we have an entity, it's dead but our spawn frame has passed
    if (p.entity && p.state.Dead && p.state.respawnFrame <= BoltNetwork.serverFrame) {
    p.Spawn();
    }
    }
    }

    public override void ConnectRequest(UdpKit.UdpEndPoint endpoint, Bolt.IProtocolToken token) {
    BoltNetwork.Accept(endpoint);
    }

    public override void Connected(BoltConnection c) {
    c.UserData = new Player();
    c.GetPlayer().connection = c;
    c.GetPlayer().name = "CLIENT:" + c.RemoteEndPoint.Port;

    c.SetStreamBandwidth(1024 * 1024);
    }

    public override void SceneLoadRemoteDone(BoltConnection connection) {
    connection.GetPlayer().InstantiateEntity();

    }

    public override void SceneLoadLocalDone(string map) {
    if (Player.serverIsPlaying) {
    Player.serverPlayer.InstantiateEntity ();
    } else {
    Player pl = new Player ();
    pl.InstantiateEntity ();
    }

    }

    public override void SceneLoadLocalBegin(string map) {
    foreach (Player p in Player.allPlayers) {
    p.entity = null;
    }
    }
    }
    }
  • The advanced tutorial is fully server authoritative, clients cannot do anything without asking the server.
  • stanchion said:

    The advanced tutorial is fully server authoritative, clients cannot do anything without asking the server.

    Excuse me, could you tell me how to organize this? I mean how to organize requests to change the character from the client
  • it should be sent either in the connect token or an event.
  • stanchion said:

    it should be sent either in the connect token or an event.

    I can not figure out how to make an event!!!
    I do as in this lesson https://doc.photonengine.com/en-us/bolt/current/getting-started/bolt-103-events

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Singltone : Bolt.EntityEventListener {

    private static Singltone _instance;
    public static Singltone Instance { get { return _instance; } }

    void Start()
    {

    _instance = this;
    DontDestroyOnLoad(gameObject);
    }

    // Auxiliary method that changes the value
    public void upPl(int pl){
    Storage.PlayerChoose = pl;

    //Created the event and wrote the code as in the lesson
    //Logically, there should be an event about changing the value

    var player = ChoosePlayer.Create (entity);
    player.ChoosePlayerInt = Storage.PlayerChoose;
    player.Send ();
    }
    }


    But this all does not work I get an error :

    ArgumentNullException: Argument cannot be null.
    Parameter name: entity
    ChoosePlayer.Create (.BoltEntity entity, EntityTargets targets)
    ChoosePlayer.Create (.BoltEntity entity)
    Singltone.upPl (Int32 pl) (at Assets/samples/AdvancedTutorial/scripts/MyScripts/Singltone.cs:28)
  • Did u fix your issue?
Sign In or Register to comment.