Multiplayer FPS Problems after Episode 26, please help

Updated on April 14, 2018 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on September 13, 2017

Hi guys,

 

i know the MP FPS Tutorial is now quiet “old” many changes have happened since releasing. (unity API Change, Database updates)

 

some Problems could be solved but now i come to a point where i cant find help to solve this.

 

i got 3 errors in console:

    1.  NullReferenceException: Object reference not set to an instance of an object
      LoginMenu.Start () (at Assets/scripts/LoginMenu.cs:52) [in the code i posted it is in line 41-42 … beginning with “string [] splitdatafile …”

 

    1. [Kills] not found in Hello World!
      UnityEngine.Debug:LogError(Object)
      DataTranslator:DataToValue(String, String) (at Assets/scripts/DataTranslator.cs:30)
      DataTranslator:DataToKills(String) (at Assets/scripts/DataTranslator.cs:11)
      PlayerStats:OnReceivedData(String) (at Assets/scripts/PlayerStats.cs:18)
      c__Iterator1:MoveNext() (at Assets/scripts/UserAccountManager.cs:97)
      UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

 

    1.  FormatException: Input string was not in the correct format
      System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)
      DataTranslator.DataToKills (System.String data) (at Assets/scripts/DataTranslator.cs:11)
      PlayerStats.OnReceivedData (System.String data) (at Assets/scripts/PlayerStats.cs:18)
      UserAccountManager+c__Iterator1.MoveNext () (at Assets/scripts/UserAccountManager.cs:97)
      UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

im not sure which code i should show u, because i only get a hint in the first error. so i post u guys my loginmenu and the useraccountmanager, i thik these are the two scripts where the “magic” happens.

 

LoginMenu

 

//This script controls the UI in the Database Control (Free) demo scene
//It uses database control to login, register and send and recieve data
using UnityEngine;
using System; //allows string.Split to be used with SplitStringOptions.none
using System.Collections;
using DatabaseControl;//This line is always needed for any C# script using the database control requests. See PDF documentation for more information
//use 'import DatabaseControl;' if you are using JS
public class LoginMenu : MonoBehaviour
{
    ////These variables are set in the Inspector:
    //they are enabled and disabled to show and hide the different parts of the UI
    public GameObject login_object;
    public GameObject register_object;
    public GameObject loading_object;
    //these are the login input fields:
    public UnityEngine.UI.InputField input_login_username;
    public UnityEngine.UI.InputField input_login_password;
    //these are the register input fields:
    public UnityEngine.UI.InputField input_register_username;
    public UnityEngine.UI.InputField input_register_password;
    public UnityEngine.UI.InputField input_register_confirmPassword;
    
    //red error UI Texts:
    public UnityEngine.UI.Text login_error;
    public UnityEngine.UI.Text register_error;
  
    ////These variables cannot be set in the Inspector:
    //the part of UI currently being shown
    // 0 = login, 1 = register, 2 = logged in, 3 = loading
    int part = 0;
    //scene starts showing login
   
    bool isDatabaseSetup = true;
    void Start()
    {
        //this checks whether the database is setup. It is used to prevent errors for users who try to use the demos
        //without having setup a database.
        //You don't need to use this bool as it will work without it as long as the database has been setup
        TextAsset datafile = Resources.Load("data") as TextAsset;
        string[] splitdatafile = datafile.text.Split(new string[] { "-" }, StringSplitOptions.None);
        if (splitdatafile[0] == "0")
        {
            isDatabaseSetup = false;
            Debug.Log("These demos will not work out of the box. You need to setup a database first for it to work. Please read the Setup section of the PDF for more information");
        }
        else
        {
            isDatabaseSetup = true;
        }
        //sets error Texts string to blank
        blankErrors();
     
    }
    void Update()
    {
        if (isDatabaseSetup == true)
        {
            //enables and disables the defferent objects to show correct part
            if (part == 0)
            {
                login_object.gameObject.SetActive(true);
                register_object.gameObject.SetActive(false);
                loading_object.gameObject.SetActive(false);
            }
            if (part == 1)
            {
                login_object.gameObject.SetActive(false);
                register_object.gameObject.SetActive(true);
                loading_object.gameObject.SetActive(false);
            }
            if (part == 2)
            {
              //We are logged in - we´ve already transitioned to a new scene .. Hopefully 🙂
            }
            if (part == 3)
            {
                login_object.gameObject.SetActive(false);
                register_object.gameObject.SetActive(false);
                loading_object.gameObject.SetActive(true);
            }
          
        }
    }
    void blankErrors()
    {
        //blanks all error texts when part is changed e.g. login > Register
        login_error.text = "";
        register_error.text = "";
        
    }
    public void login_Register_Button()
    { //called when the 'Register' button on the login part is pressed
        part = 1; //show register UI
        blankErrors();
    }
    public void register_Back_Button()
    { //called when the 'Back' button on the register part is pressed
        part = 0; //goes back to showing login UI
        blankErrors();
    }
    public void data_LogOut_Button()
    { //called when the 'Log Out' button on the data part is pressed
        part = 0; //goes back to showing login UI
        UserAccountManager.instance.LogOut();
        blankErrors();
    }
    public void login_login_Button()
    { //called when the 'Login' button on the login part is pressed
        if (isDatabaseSetup == true)
        {
            //check fields aren't blank
            if ((input_login_username.text != "") && (input_login_password.text != ""))
            {
                //check fields don't contain '-' (if they do, login request will return with error and take longer)
                if ((input_login_username.text.Contains("-")) || (input_login_password.text.Contains("-")))
                {
                    //string contains "-" so return error
                    login_error.text = "Unsupported Symbol '-'";
                    input_login_password.text = ""; //blank password field
                }
                else
                {
                    //ready to send request
                    StartCoroutine(sendLoginRequest(input_login_username.text, input_login_password.text)); //calls function to send login request
                    part = 3; //show 'loading...'
                }
            }
            else
            {
                //one of the fields is blank so return error
                login_error.text = "Field Blank!";
                input_login_password.text = ""; //blank password field
            }
        }
    }
    IEnumerator sendLoginRequest(string username, string password)
    {
        if (isDatabaseSetup == true)
        {
            IEnumerator e = DatabaseControl.DCF.Login(username, password);
            while (e.MoveNext())
            {
                yield return e.Current;
            }
            string returned = e.Current as string;
            if (returned == "Success")
            {
                //Password was correct
                blankErrors();
                part = 2; //show logged in UI
                //blank username field
                input_login_username.text = ""; //password field is blanked at the end of this function, even when error is returned
                //set logged in username and password to variables
                UserAccountManager.instance.LogIn(username, password);
            }
            if (returned == "incorrectUser")
            {
                //Account with username not found in database
                login_error.text = "Username not found";
                part = 0; //back to login UI
            }
            if (returned == "incorrectPass")
            {
                //Account with username found, but password incorrect
                part = 0; //back to login UI
                login_error.text = "Incorrect Password";
            }
            if (returned == "ContainsUnsupportedSymbol")
            {
                //One of the parameters contained a - symbol
                part = 0; //back to login UI
                login_error.text = "Unsupported Symbol '-'";
            }
            if (returned == "Error")
            {
                //Account Not Created, another error occurred
                part = 0; //back to login UI
                login_error.text = "Database Error. Try again later.";
            }
            //blank password field
            input_login_password.text = "";
        }
    }
    public void register_register_Button()
    { //called when the 'Register' button on the register part is pressed
        if (isDatabaseSetup == true)
        {
            //check fields aren't blank
            if ((input_register_username.text != "") && (input_register_password.text != "") && (input_register_confirmPassword.text != ""))
            {
                //check username is longer than 4 characters
                if (input_register_username.text.Length > 4)
                {
                    //check password is longer than 6 characters
                    if (input_register_password.text.Length > 6)
                    {
                        //check passwords are the same
                        if (input_register_password.text == input_register_confirmPassword.text)
                        {
                            if ((input_register_username.text.Contains("-")) || (input_register_password.text.Contains("-")) || (input_register_confirmPassword.text.Contains("-")))
                            {
                                //string contains "-" so return error
                                register_error.text = "Unsupported Symbol '-'";
                                input_login_password.text = ""; //blank password field
                                input_register_confirmPassword.text = "";
                            }
                            else
                            {
                                //ready to send request
                                StartCoroutine(sendRegisterRequest(input_register_username.text, input_register_password.text, "[Kills]0/[Deaths]0")); //calls function to send register request
                                part = 3; //show 'loading...'
                            }
                        }
                        else
                        {
                            //return passwords don't match error
                            register_error.text = "Passwords don't match!";
                            input_register_password.text = ""; //blank password fields
                            input_register_confirmPassword.text = "";
                        }
                    }
                    else
                    {
                        //return password too short error
                        register_error.text = "Password too Short";
                        input_register_password.text = ""; //blank password fields
                        input_register_confirmPassword.text = "";
                    }
                }
                else
                {
                    //return username too short error
                    register_error.text = "Username too Short";
                    input_register_password.text = ""; //blank password fields
                    input_register_confirmPassword.text = "";
                }
            }
            else
            {
                //one of the fields is blank so return error
                register_error.text = "Field Blank!";
                input_register_password.text = ""; //blank password fields
                input_register_confirmPassword.text = "";
            }
        }
    }
    IEnumerator sendRegisterRequest(string username, string password, string data)
    {
        if (isDatabaseSetup == true)
        {
            IEnumerator ee = DatabaseControl.DCF.RegisterUser(username, password, data);
            while (ee.MoveNext())
            {
                yield return ee.Current;
            }
            string returned = ee.Current as string;
            if (returned == "Success")
            {
                //Account created successfully
                blankErrors();
                part = 2; //show logged in UI
                //blank username field
                input_register_username.text = ""; //password field is blanked at the end of this function, even when error is returned
                UserAccountManager.instance.LogIn(username, password);
            }
            if (returned == "usernameInUse")
            {
                //Account Not Created due to username being used on another Account
                part = 1;
                register_error.text = "Username Unavailable. Try another.";
            }
            if (returned == "ContainsUnsupportedSymbol")
            {
                //Account Not Created as one of the parameters contained a - symbol
                part = 1;
                register_error.text = "Unsupported Symbol '-'";
            }
            if (returned == "Error")
            {
                //Account Not Created, another error occurred
                part = 1;
                login_error.text = "Database Error. Try again later.";
            }
            input_register_password.text = "";
            input_register_confirmPassword.text = "";
        }
    }
}

 

UserAccountManager

 

 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DatabaseControl;
using UnityEngine.SceneManagement;
public class UserAccountManager : MonoBehaviour {
    public static UserAccountManager instance;
    void Awake()
    { if (instance != null)
        {
            Destroy(gameObject); return;
        }
        instance = this;
        DontDestroyOnLoad(this);
    }
    public static string playerUsername { get; protected set; }
    private static string playerPassword = "";
    public static bool isLoggedIn { get; protected set; }
    public string loggedInSceneName = "Lobby";
    public string loggedOutSceneName = "LoginMenu";
    public delegate void OnDataReceivedCallBack(string data);
    public void LogOut()
    {
        playerUsername = "";
        playerPassword = "";
        isLoggedIn = false;
        SceneManager.LoadScene(loggedOutSceneName);
    }
    public void LogIn(string _Username, string _Password)
    {
        playerUsername = _Username;
        playerPassword = _Password;
        isLoggedIn = true;
        SceneManager.LoadScene(loggedInSceneName);
    }
    public void SendData(string data)
    {
        if (isLoggedIn) StartCoroutine(SetData(data));
    }
    IEnumerator SetData(string data)
    {
        IEnumerator e = DCF.SetUserData(playerUsername, playerPassword, data);
        // << Send request to set the player's data string. Provides the username, password and new data string 
        while (e.MoveNext())
        {
            yield return e.Current;
        }
        string response = e.Current as string; // << The returned string from the request
        if (response == "Success")
        {
            //The data string was set correctly. Goes back to LoggedIn UI
            Debug.Log("Succes sending data");
            // loggedInParent.gameObject.SetActive(true);
        }
        else
        {
            Debug.Log("Error: Unknown Error. Please try again later. Send data problem");
        }
    }
    public void GetData(OnDataReceivedCallBack onDataReceived)
    {
        //Called when the player hits 'Get Data' to retrieve the data string on their account. Switches UI to 'Loading...' and starts coroutine to get the players data string from the server
        if (isLoggedIn)
            StartCoroutine(GetData_numerator(onDataReceived));
    }
    IEnumerator GetData_numerator(OnDataReceivedCallBack onDataReceived)
    {
        string data = "ERROR"; IEnumerator e = DCF.GetUserData(playerUsername, playerPassword); // << Send request to get the player's data string. Provides the username and password 
        while (e.MoveNext())
        {
            yield return e.Current;
        }
        string response = e.Current as string;        // << The returned string from the request
        if (response == "Error")
        {
            Debug.Log("Error: Unknown Error. Please try again later. GetDataProblem");
        }
        else
        {
            //The player's data was retrieved. Goes back to loggedIn UI and displays the retrieved data in the InputField
            data = response;
        }
        if (onDataReceived != null)
            onDataReceived.Invoke(data);
    }
}
 

im very thankful to the person who realy reads this, because i think no one wants to help at such an “old” BrackeyCourse since so much was changing since the release. but i hope someone could help me.

greetings with love

Brxn

 

  • Liked by
Reply
0 on April 14, 2018

What about it?

 

  • Liked by
Reply
Cancel