MMORPG style login and registration system?

I have taken classes on how to work with code, but I still need help with this game project. I know how to make the buttons and text fields for the Login GUI and Account registration GUI,but the problem is: I don’t know how to make them functional. I have heard something about using a server side database, but I have almost no idea how to use them. Can someone help me figure out how to create a functional login and account creation GUI that communicates with a server side database? If I can figure that out, then more than half the problems I face in creating this game would go away. I also have heard about PHP but am not sure how to use it. Basicly here is what I need help with:

  1. creating a server side database to store player data (such as username and password),
  2. creating a Login and account creation GUI that is functional with the database and
  3. (may be a later function but is not a priorty as of now) create a way for the database to also store the player’s stats, such as how much in-game money he has and his combat level.

also the database ABSOLUTELY CAN’T be client side, as if it were, it could be tampered with (or in other words, hacked) much more easily.

Can anyone help me accomplish creating this idea? And by the way, I am working in C#.

OK, I wasn’t going to reply because I don’t have much helpful to say, but I can’t stand to see you get NO reply, so here we go.

There’s a good possibility that what you’re attempting to do is beyond your current capabilities. You certainly WILL have to use PHP, or some other server-side programming solution (any of which are going to be at least as hard as PHP, probably harder). If you barely know what PHP is, then you have a few months of learning ahead of you, at the very least, getting up to speed on that. And yes, from your description, the PHP will need to speak to some server-side database, so you also have to learn how to set up and manage a database (MySQL is common, but there are many other choices, including cloud services these days).

Once you know how to make something like a db-backed REST service in PHP, then communicating with it in Unity is comparably easy, thanks to the WWW and WWWForm classes. But here too, you will probably need more than a beginner’s level of Unity proficiency to see how to make the GUI, grab the inputs and build the request, handle the response from the server when it eventually comes back, etc.

I agree with @JoeStrout . I have already done this (written both the C# code needed for Unity to communicate with the database, and the PHP code to handle Unity’s request and supply it with responses). I could just give you the code (and if you want me to I will). However, as @JoeStrout said, you have a LOT of learning ahead of you. Registering and logging in isn’t half the battle, hell it’s not even 10% of it. You have to handle saving player data to the database, and then requesting that data when the player successfully logs in. What if the player wants to make changes to their registration data? You’ll have to know how to push those changes to the database.

My suggestion: As @JoeStrout said, go learn PHP. Also learn MySQL (it’s not a coding language, it’s an interface). Learn more about the C# (.NET) library. Then try and tackle this.

Can you show me the script? maybe I can learn something form it. Then how will I make a database to connect to it? Thanks.

I use Award Space as a server to hold my database.

The following two scripts are the PHP scrips you need. The first is for logging in. The second is for registration.
LOGIN PHP SCRIPT

<?PHP $username = $_POST['username']; $password = $_POST['password']; $con = mysql_connect("fdb13.awardspace.net","INSERT YOUR MYSQL USERNAME HERE","INSERT YOUR MYSQL PASSWORD HERE") or ("Cannot connect!" . mysql_error()); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("1870311_admin" , $con) or die ("could not load the database" . mysql_error()); $check = mysql_query("SELECT * FROM accounts WHERE `username`='".$username."'"); $numrows = mysql_num_rows($check); if ($numrows == 0) { die ("Username does not exist."); } else { $password = md5($password); while($row = mysql_fetch_assoc($check)) { if ($password == $row['password']) die("Log in successful!"); else die("Incorrect password."); } } ?>

REGISTRATION PHP SCRIPT

<?PHP $name = $_POST['name']; $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; $con = mysql_connect("fdb13.awardspace.net","INSERT YOUR MYSQL USERNAME HERE","ENTER YOUR MYSQL PASSWORD HERE") or ("Cannot connect!" . mysql_error()); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("1870311_admin" , $con) or die ("could not load the database" . mysql_error()); $check = mysql_query("SELECT * FROM accounts WHERE `username`='".$username."'"); $numrows = mysql_num_rows($check); if ($numrows == 0) { $password = md5($password); $ins = mysql_query("INSERT INTO `accounts` (`id` , `name` , `username` , `password` , `email`) VALUES ('' , '".$name."' , '".$username."' , '".$password."' , '".$email."') ; "); if ($ins) die ("Succesfully Created User!"); else die ("Error: " . mysql_error()); } else { die("User already exists!"); } ?>

The following script is used in Unity to submit requests to the server to register and/or log in.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
/*
THE FOLLOWING SCRIPT WAS WRITTEN BY DAVID ROSEN. IT IS FREE TO USE WITH ACKNOWLEDGEMENT OF THE CREATOR.
IT IS INTENDED FOR USE WITH AWARD SPACE (https://www.awardspace.net/), PHP and MySQL
*/
public class Main_Menu : MonoBehaviour
{
    //THE FOLLOWING ARE REFERENCES TO THE UI INPUT FIELDS THAT THE PLAYER TYPES INTO
    public Text usernameText, passwordText, regNameText, regUsernameText, regPassText, regConfPassText, regEmailText;
    public Text messageText; //THIS IS THE TEXT TO BE DISPLAYED ON SCREEN TO THE PLAYER
   
    //THE FOLLOWING VARIBALES ARE POPULATED BY THE TEXT THE PLAYER ENTERS INTO THE CORRESPONDING INPUT FIELDS
    private string username, password, regName, regUsername, regPass, regConfPass, regEmail;
   
    //FUNCTION TO BE CALLED VIA THE UI BUTTON
    public void LogIn()
    {
        messageText.text = ""; //CLEAR ANY DISPLAYED MESSAGES TO THE PLAYER
       
        username = usernameText.text;    //POPULATE THE PRIVATE username VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE usernameText INPUT FIELD
        password = passwordText.text;    //POPULATE THE PRIVATE password VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE passwordText INPUT FIELD
       
        if (username == "" || password == "") //IF THE PLAYER HASN'T ENTERED THE REQUIRED INFORMATION...TELL THEM TO
            messageText.text = "Please complete all fields.";
        else    //IF ALL INFORMATION IS ENTERED, BUILD A WWWForm AND SEND IT TO THE SERVER
        {
            WWWForm form = new WWWForm();
            form.AddField("username", username);
            form.AddField("password", password);
            WWW w = new WWW("http://???????????????.dx.am/login.php", form);    //REPLACE ?????????? WITH YOUR AWARD SPACE DOMAIN
            StartCoroutine(LogIn(w));
        }
    }
    //FUNCTION TO BE CALLED VIA THE UI BUTTON
    public void Register()
    {
        messageText.text = ""; //CLEAR ANY DISPLAYED MESSAGES TO THE PLAYER
       
        regName = regNameText.text;    //POPULATE THE PRIVATE regName VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE regNameText INPUT FIELD
        regUsername = regUsernameText.text;    //POPULATE THE PRIVATE regUsername VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE regUsernameText INPUT FIELD
        regPass = regPassText.text;    //POPULATE THE PRIVATE USERNAME regPass WITH THE TEXT THE PLAYER ENTERED INTO THE pregPassText INPUT FIELD
        regConfPass = regConfPassText.text;    //POPULATE THE PRIVATE regConfPass VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE regConfPassText INPUT FIELD
        regEmail = regEmailText.text;    //POPULATE THE PRIVATE regEmail VARIABLE WITH THE TEXT THE PLAYER ENTERED INTO THE regEmailText INPUT FIELD
       
        if (regName == "" || regUsername == "" || regPass == "" || regConfPass == "" || regEmail == "") //IF THE PLAYER HASN'T ENTERED THE REQUIRED INFORMATION...TELL THEM TO
            messageText.text = "Please complete all fields.";
        else    //IF ALL INFORMATION IS ENTERED......
        {
            if(regPass == regConfPass)    //......AND THE PASSWORDS MATCH, BUILD A WWWForm AND SEND IT TO THE SERVER
            {
                WWWForm form = new WWWForm();
                form.AddField("name", regName);
                form.AddField("username", regUsername);
                form.AddField("password", regPass);
                form.AddField("email", regEmail);
                WWW w = new WWW("http://???????????.dx.am/register.php", form);    //REPLACE ?????????? WITH YOUR AWARD SPACE DOMAIN
                StartCoroutine(Register(w));
            }
            else    //......AND THE PASSWORDS DON'T MATCH, TELL THE PLAYER
                messageText.text = "Your passwords do not match.";
        }
    }
   
    //WE USE COROUTINES TO SEND INFORMATION TO THE SERVER, SO THAT WE CAN WAIT FOR A RESPONSE
    private IEnumerator LogIn(WWW _w)
    {
        yield return _w;    //WAIT FOR A RESPONSE FROM THE SERVER

        if (_w.error == null)    //IF THE SERVER DOESN'T SEND BACK AN ERROR
        {
            if (_w.text == "Log in successful!")    //THE PHP SCRIPT SUPPLIED WILL SEND THIS MESSAGE BACK IF THE LOGIN WAS SUCCESSFUL
            {
                // WHAT HAPPENS WHEN THE PLAYER LOGS IN
            }
            else
                messageText.text = _w.text;    //THE PHP SCRIPT SUPPLIED WILL TELL THE PLAYER IF THEIR PASSWORD IS INCORRECT, OR IF THEIR USERNAME DOESN'T EXIST
        }
        else
            messageText.text = "ERROR: " + _w.error;    //IF THERE IS AN ERROR (SUCH AS THE SERVER BEING DOWN) THE PHP SCRIPT SUPPLIED WILL TELL THE PLAYER
    }
    private IEnumerator Register(WWW _w)
    {
        yield return _w;    //WAIT FOR A RESPONSE FROM THE SERVER

        if (_w.error == null)    //IF THE SERVER DOESN"T SEND BACK AN ERROR
            messageText.text = _w.text;        //THE PHP SCRIPT SUPPLIED WILL SEND A MESSAGE BACK TO THE PLAYER SAYING REGISTRATION WAS COMPLETED
        else
            messageText.text = "ERROR: " + _w.error;    //IF THERE IS AN ERROR (SUCH AS THE SERVER BEING DOWN) THE PHP SCRIPT SUPPLIED WILL TELL THE PLAYER
    }
}

You’re going to have to figure out how to do the rest on your own buddy. I’d be handicapping you if I gave you anymore.

5 Likes

thanks, hope this works.

using UnityEngine;
using System.Collections;

public class Game_Menu_GUI : MonoBehaviour {
   
    public string Username = "";
    public string Password = "";
   
    private string CreateUsername = "";
    private string CreatePassword = "";
    private string ConfirmPassword = "";
    private string PlayerEmail = "";
   
    public string CurrentGameMenu = "Login";
   
    public string MenuText = "";
    public string SecondMenuText = "";
   
    void OnGUI () {
        if(CurrentGameMenu == "Login"){
            Login();
        }else if (CurrentGameMenu == "CreateAccount") {
            CreateAccount():
        }
    }
    void Login () {
        GUI.Label(new Rect(200, 250, 200, 25), "Enter your username:");
        Username = GUI.TextField(new Rect(200, 275, 200, 25), Username);
       
        GUI.Label(new Rect(200, 300, 200, 25), "Enter your password:");
        Password = GUI.TextField(new Rect(200, 325, 200, 25), Password);
       
        GUI.Label(new Rect(200, 350, 200, 25), MenuText);
       
        if(GUI.Button(new Rect(200, 375, 200, 25), "Login")){
            if(Username == "" || Password == ""){
                MenuText == "Please fill in all info";
            }else {
                WWWForm form = new WWWForm();
                form.AddField("Username", Username);
                form.AddField("Password", Password);
                WWW w = new WWW("http://???????????????.dx.am/login.php", form);
                StartCorountine(LogIn(w));
            }
        }
        if(GUI.Button(new Rect(200, 400, 200, 25), "Create an account")){
            CurrentGameMenu = "CreateAccount";
        }
    }
    void CreateAccount () {
        GUI.Label(new Rect(200, 250, 200, 25), "Enter a username:");
        CreateUsername = GUI.TextField(new Rect(200, 275, 200, 25), CreateUsername);
       
        GUI.Label(new Rect(200, 300, 200, 25), "Enter a password:");
        CreatePassword = GUI.TextField(new Rect(200, 325, 200, 25), CreatePassword);
       
        GUI.Label(new Rect(200, 350, 200, 25), "Confirm password:");
        ConfirmPassword = GUI.TextField(new Rect(200, 375, 200, 25), ConfirmPassword);
       
        GUI.Label(new Rect(200, 400, 200, 25), "Enter your email:");
        PlayerEmail = GUI.TextField(new Rect(200, 425, 200, 25), PlayerEmail);
       
        GUI.Label(new Rect(200, 450, 200, 25), SecondMenuText);
       
        if(GUI.Button(new Rect(200, 475, 200, 25), "Create Account")){
            if(CreateUsername != "" || PlayerEmail != ""){
                if(CreatePassword == ConfrirmPassword){
                    WWWForm form = new WWWForm();
                    form.AddField("CreateUsername", CreateUsername);
                    form.AddField("ConfirmPassword", ConfirmPassword);
                    WWW w = new WWW("http://???????????.dx.am/register.php", form);
                    StartCorountine(CreateAccount(w));
                }
            }   
        }
        if(GUI.Button(new Rect(200, 500, 200, 25), "Cancel Account Creation")){
            CurrentGameMenu = "Login";
        }
    }
    private IEnumerator Login(WWW _w) {
        yield return _w;
        if(_w.error == null){
            if(_w.text == "Log in successful!"){
                //What happens to the player when he logs in:
            }else {
                MenuText = _w.text;
            }
        }else {
            MenuText = "Error" + _w.error;
        }
    }
    private IEnumerator CreateAccount(WWW _w) {
        yield return _w;
        if(_w.error == null) {
            SecondMenuText = _w.text;
        }else {
            SecondMenuText = "Error" + _w.error;
        }
    }
}

I have taken the code you gave me and changed a few things for my game’s purpose. Will something like this code work?

where do I put the phps? in the client? if not where? does the domain have to have web hosting?

where and how do i create the php files?

If you have questions like this, I think it’s best if you start learning PHP asap. Start here:
PHP
MySQL

Yes, if you want to work with server-side and client-side, you need to have webhosting. A webhoster which offers PHP and MySQL at least.
Like other’s have already said, you can use WWW and WWWForm classes which you can check in Unity’s User manual, but I strongly recommend learning about PHP and MySQL first! And additionally some basic HTML as well so that you know how to build up the forms and such. If you don’t have any knowledge, at least basic knowledge, about these programming languages, it will be very hard to understand what’s needed in Unity to achieve what you want.

Good luck.

ok, I have set up the php’s but do I have to create a table in the database? Or will the scripts do that automatically?

You’re asking for somebody to take your hand and walk you through everything. No one here is going to do that. There are WAY too many tutorials out there if you just look. Start here ----->

2 Likes

The code I made has worked for a little, but as I contiued adding to It I got error CS0103,
start corountine does not exist in its current context. I have noticed something in the video, could this error be caused be the IEnumerators being private?

When I create the database it will not give the rows “Extra” and other parts to the structure as shown in your video, I think its outdated. How do I edit the types of rows i get in the database?

Please, like DRRosen3 already suggested, search for some tutorials on PHP and MySQL first! START with PHP and MySQL, like the ones I posted earlier. These will teach you how to set up databases, work with PHP to fill your databases and more things like that. A quick search for more tuturials about PHP and SQL on Google gave me this result. It really is best if you start with the two links I already gave (SQL and PHP). That gives you a BASIC UNDERSTANDING of both PHP and MySQL, how to work with database, how to fill them with data, how to read the data, etc. From there you can move forward with, for example a Register and login concept just in a website. Once you understand how that works, you can move on to learn how to do this in Unity.

Seen your, seemingly, lack of enough knowledge, I’d suggest to find a (free) webhost which offers hosting options with both PHP and MySQL to try out the tutorials before you move on to anything else.

I cannot stress this enough when you want to make a game/app that requires a login system (or any other system for which you need a database outside of your game client): LEARN THE BASICS of communicating with a database BEFORE you start building the game in unity. Learn to make your own registration and login system BEFORE you start building the game in unity. Do NOT simply copy & paste without understanding anything! Otherwise you’ll end up with bucketloads full of questions about basics that have nothing to do with Unity before you can really start in Unity.

Start with something very simple like making PHP on a webpage say “Hello world” and go from there. I know it takes some time to learn it all (I have done it while I was in a completely different field of work. I had to learn it all by myself, without any teachers at school or any collegues at work who knew what they were talking about), but it really is the best way. Otherwise you will not understand what to do if you get error message. You wouldn’t have a clue where to start looking for an answer, like you don’t now. In essence, you would have a game, but you wouldn’t have the skills to properly maintain the game and its userbase and offer a good support when people start to play your game and run into problems (register, login, things that need to be fixed in the database, etc).

I hope this was helpful for you.

1 Like

The problem currently is not lack of knowledge, but the fact that the tutorial is outdated, and the interface for editing the databases has changed in award space. The database editor looks totally different from the one in video. If I only knew how to use the new editor, I would probably be finished by now. I have successfully set up the phps, and the web host, and the script, but can’t figure out how to use the database editor to work with the codes and phps.
basically the database editor in award space looks different now than when it did in the video. But once the database is complete, the login screen should become operational.

Okay… Well, I don’t have my site hosted on Award Space. Which database system do they use there? PhP My Admin?

My Admin

What did you use to create the site and other stuff?

My portfolio site (which works with databases) is on NameCheap which is a paid hosting account.
I use PHP MyAdmin as well, but my host has a seperate Creation system for databases. I simply give the name of the database I want to create and the system automatically sets it up.

I use Mysql to create the tables in my database which means I run a a SQL query on the database to create the table.