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

MySQL database for RPG stats; collecting large volume of details from database

Discussion in 'Scripting' started by KrisM, Feb 14, 2015.

  1. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    I still consider myself a beginner in terms of game development, but I've got over a decades worth of experience with web design (PHP). So try to keep this in mind when giving answers. My question here could have a simple answer, so I'll try to keep this brief with this hope in mind. I'll expand more if you guys need additional details.

    I've got a series of ... close to 100 database entries for player stats alone; Health, Mana, Stamina, to name the basics. I've got a simple MySQL database, php script, C# script, and scene going which passes one value (player name) from the database to the game. It works well, but the process to collect one entry from the database seems a bit long, and to repeat it for every entry feels like a long way to go about something where there should be an easier/better/quicker method.

    So, is there a way to collect, via an array, all the details in a table in the database where 'PlayerID = x', and then break it down via C# in unity? I know I can use PHP to simply list all those details, but what C# codes assist with 'exploding' arrays?
     
  2. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    As an update:

    I'm working on getting the database entries sent to Unity via a string with each item separated by a comma. My google searching has led me to believe that the 'Split' function should be able to turn this string into an array, using the comma to break the components apart.

    The error I'm getting is 'UnityEngine.UI.Text does not contain a definition for 'Split'. Could be a simple answer to this at least. Can anyone help?
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    'UnityEngine.UI.Text' is not a string, it's a class. If you have an instance of that type, you'll also have access to a member of type 'string' called text which then allows you to call the 'Split' method. That's what you actually want.
     
  4. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Are you tied to a SQL database, or is it just what you're used to using for a database solution? There's lots of ways to store data that're more native to the Unity environment.
     
  5. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    I'm not technically tied to an SQL database, but I am familiar with it and PHP. This makes it a more logical choice for me. I don't know of any alternatives; but they would have to be secure and be usable in a multiplayer game (which I wouldn't expect to be a problem).

    Suddoha; I partly get what you're saying, but I'm not sure how to go about making this change. The code I've got is as follows:

    Code (CSharp):
    1.    
    2.     public string BaseURL;
    3.     public int PlayerID;
    4.     private string PlayerID_URL;
    5.     private Text txt;
    6.  
    7.     IEnumerator Start () {
    8.         txt = gameObject.GetComponent<Text> ();
    9.         PlayerID_URL = BaseURL + PlayerID;
    10.         WWW www = new WWW (PlayerID_URL);
    11.         yield return www;
    12.  
    13.         string[] splitText = www.Split (www, char.Parse (","));
    14.  
    15.         txt.text = www.text;
    The error occurs on the ling starting with 'string'. For some reason, the error has changed to 'Unity.Engine.WWW does not contain a definition for 'Split' and no extension method 'Split' of type 'UnityEngine.WWW could be found'.

    Update:
    Looks like the returned 'www' isn't a string. So Suddoha is right on the ball. I'll keep looking for a way to convert this to a string, but if anyone has an answer, please let me know... I'm already dreading trying to google 'www to string conversion'...
     
    Last edited: Feb 14, 2015
  6. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    Update: problem solved.

    Had the code to split the string wrong. The returned value from the URL is actually a string. A slight adjustment to the code has this working perfectly.

    Change is as follows:
    Code (CSharp):
    1. string[] splitText = www.text.Split (',');
    2. txt.text = splitText[0];
    3. // First entry is the player name, the second is their email address.
    4.  
    5.