Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to SQL db

Discussion in 'Multiplayer' started by deadlycrow, Jun 15, 2014.

  1. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    159
    i want to create a construction based game, like minecraft or the incoming fortnite, i have the idea but i need a SERVER database, the point is i dont know how the hell communicate unity with some database, to makes querys for lets say, my house position, rotation, size, etc... my constructor skill, level, etc.. inventory... how can i achieve this?
     
  2. Keemo

    Keemo

    Joined:
    Apr 22, 2014
    Posts:
    31
    I never did it in unity before, but i think you could do a webservice which sends a POST to the Server and with the response you can work.
    http://docs.unity3d.com/ScriptReference/WWW.html

    So on your server for example is a php script named Player which connects to a database. In your database every player got a unique ID so you use this ID as a parameter in your POST and the php script returns the player name.

    Im currently not at home but if you want i can show you an example is used for an iphone app. The great thing is this webservice works almost in every program or scripting language.
     
    Last edited: Jun 16, 2014
  3. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    159
    yes please, im really lost in this matter :D
     
  4. Keemo

    Keemo

    Joined:
    Apr 22, 2014
    Posts:
    31
    Okay here is a example for a very simple login:
    In my database every user had a Unique ID, a login name and a md5 password.
    However i never did it in unity, but i think soon i will give it a try.

    Code (csharp):
    1.  
    2. <?php
    3.  
    4. $db = "app";
    5. $con = mysql_connect("localhost","dbusername","dbpasswd");
    6. if (!$con)
    7.   {
    8.   die('Could not connect: ' . mysql_error());
    9.   }
    10.   mysql_select_db($db);
    11.  
    12. $SELECT = mysql_fetch_array(mysql_query("SELECT * FROM player Where Login = '$_POST[login]'"));
    13.     $pw = $_POST['pw'];
    14.     $md5 = md5($pw);
    15. if ($md5 === $SELECT['password']):
    16.     return "success" // Now in Unity you could use a If function : if(response == "success"){Debug.Log("Your login works")}
    17.     mysql_close($con);
    18.  
    19.     else:
    20.     return "wrong password";
    21.     mysql_close($con);
    22.     endif;
    23. ?>
    24.  
    (Im not sure if this php script works, i just edited it a bit.)

    So you need to find out how to send a POST with the parameters "login" and "pw".

    To try if you php script works you can use a .htm file which should look like this:

    Code (csharp):
    1.  
    2. <html>
    3. <head>
    4. </head>
    5. <body>
    6. <form action="yourphpfile.php" method="post">
    7.   <table style="text-align: left; width: 100%;" border="0"
    8. cellpadding="2" cellspacing="2">
    9.     <tbody>
    10.       <tr>
    11.         <td>login</td>
    12.         <td><input name="login" type="text"></td>
    13.         <td></td>
    14.       </tr>
    15.     <tr>
    16.         <td>passwd</td>
    17.         <td><input name="pw" type="text"></td>
    18.         <td></td>
    19.       </tr>
    20.       <tr>
    21.         <td></td>
    22.         <td><input type="submit"></td>
    23.       </tr>
    24.     </tbody>
    25.   </table>
    26. </form>
    27. </body>
    28. </html>
    29.  
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    This link should explain how to go about connecting to your database & updating/requesting information.
     
  6. Keemo

    Keemo

    Joined:
    Apr 22, 2014
    Posts:
    31
    You can do that but its not recommended to let a client connect directly to a database and a webservice is still better because it works everywhere. This plugin for example wont work for the unity webplayer.
     
  7. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    It goes without saying that this should only be used with an authorative server.
    However, neither the php nor the mysql solution prevent you from sending in fake data.
    And you could prevent them from messing up your database by simply saving
    the information to the database using a profile with limited permissions.
     
  8. hayer

    hayer

    Joined:
    Mar 28, 2013
    Posts:
    1
    I would say that using a webservice is the wrong way to go. Webservices are great; but not for this use.

    How do you generate the "world" / terrain?
    I would suggest creating a way to generate the world from a seed value. This is like Minecraft does it.

    1. Player connects to the server.
    2. Server sends the seed value back.
    3. Player generate the world using the seed value.
    4. Server sends changes like, "block at 3, 6 is destroyed".

    All this using the Unity or some network-plugin (read: No webservice or direct connect to the database). You already have a stream for sending player updates like positions and angles. Why not use that same stream for sending data about changes in the terrain?
     
  9. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    If you read his post carefully, then you can see that he never mentioned saving the terrain generation in the mysql database.
    "my house position, rotation, size, etc... my constructor skill, level, etc.. inventory"
    The house position would be the additional information the server sends, as you mentioned.
     
  10. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    159
    exactly, because the realtime procedural "world generation" is beyond my knowledge, and that means, put procedural npcs around the world, trees and stuff, and even i dont want to know the thousands of lines to do that... i wish to be involved in a team, so in that situation i can take a look to that... but im so godamn alone lol, the terrain is quiet, there is 6 terrains in the map, 2 isles of 6000x6000 and a 4 underwater sand terrains of 8000 each one, so is a big world, there will be resources, so when the player cut a tree, that tree will be respawn or re-grown again like in a half of an hour or something