Search Unity

How to open System.Collections.Generic.List`1[x] from database?

Discussion in 'Scripting' started by Slyrfecso1, Dec 14, 2015.

  1. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Hi,

    I'd like to know how can I open a saved list from phpMyadmin Mysql database?
    This is part of the Unity because I try to save/load the gamestate from Unity, after this my projects will be finished and I can publish that.

    System.Collections.Generic.List`1[felhasznalo+saveddata]
    UnityEngine.Debug:Log(Object)





    Which type is the best for generic list cell?




    I can import back the list from server with Enumerator and text split, but I don't know how can I open it.
    If these aren't enough information just ask and I can give anything.
    Please somebody help me.
    Thanks.
     
  2. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    I tried this, but nothing is in log:

    Code (CSharp):
    1. List<string> loadItems1 = new List<string>();
    2. Debug.Log(loadItems1); //System.Collections.Generic.List`1[System.String]
    3.  
    4. foreach(var msg in loadItems1)
    5. {
    6. Debug.Log(msg);  //Nothing written in console
    7. }

    Any idea?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    If you create a new List, there won't be anything in it, and the foreach won't execute any times. How are you reading the data from the DB?
     
  4. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    //DECLARE SECTION UNITY

    Code (CSharp):
    1. [System.Serializable]
    2.     public struct saveddata
    3.     {
    4.         public string savedName;
    5.         public float savedposx, savedposy, savedposz, savedrotx, savedroty, savedrotz;
    6.     }
    7.  
    8.     public static List<saveddata> savedItems = new List<saveddata>();


    //ADD SECTION UNITY
    Code (CSharp):
    1. if(GUILayout.Button("Mentés",GUIButonStyle)) {
    2.  
    3.                     GameObject[] objects = GameObject.FindGameObjectsWithTag("Player");
    4.                     foreach (GameObject go in objects)
    5.                     {
    6.                         saveddata itm = new saveddata();
    7.                         go.name = go.name.Replace("Ü","U").Trim();
    8.                         itm.savedName = go.name;
    9.                         itm.savedposx = float.Parse(go.transform.position.x.ToString());
    10.                         itm.savedposy = float.Parse(go.transform.position.y.ToString());
    11.                         itm.savedposz = float.Parse(go.transform.position.z.ToString());
    12.                         itm.savedrotx = go.transform.rotation.eulerAngles.x;
    13.                         itm.savedroty = go.transform.rotation.eulerAngles.y;
    14.                         itm.savedrotz = go.transform.rotation.eulerAngles.z;
    15.                         savedItems.Add(itm);
    16.                     }
    17. StartCoroutine ("SaveNames");
    18. }
    19.  

    //SAVE SECTION UNITY
    Code (CSharp):
    1. IEnumerator SaveNames () {
    2.  
    3.     WWWForm Form1 = new WWWForm ();
    4.    //...
    5.     Form1.AddField ("savedItemsE", savedItems.ToString());
    6.    //..
    7.  

    //SAVE SECTION PHP
    Code (CSharp):
    1. //...
    2. $insert = "INSERT INTO `saveloadtable` (`User`, `Email`, `Vezetek`, `Osszeallitas`, `Saveditems`) VALUES ('" . $user2E . "', '" . $email2E . "', '" . $vezetekE . "', '" . $osszeallitasE . "', '" . $savedItemsE . "')";
    3. //...


    //LOAD SECTION PHP
    Code (CSharp):
    1.  
    2. //...
    3. while($row = mysql_fetch_array($result_id2))
    4.             {
    5.                 echo $row['Vezetek'];
    6.                 echo ":";
    7.                 echo $row['Osszeallitas'];
    8.                 echo ":";
    9.                 echo $row['Saveditems'];
    10.                 echo ":";
    11.                 echo "Success";
    12.                 echo ":";
    13.             }
    14. //..
    15.  

    //IMPORT SECTION UNITY
    Code (CSharp):
    1. if (LogText.Contains("Success"))
    2.             {
    3.                 data = LoadAccountWWW.text.Split(':');
    4.  
    5.                 for (int i = 0; i < data.Length; i++)
    6.                     {
    7.                     //Debug.Log(data[i]);
    8.  
    9.  
    10.                 if ( i == 0)
    11.                 {
    12.                 vezetek1 = data [0]; //This is the first name of save
    13.                 osszeallitas1 = data [1]; //This is the last name of save
    14.                 loadItems1 = data[2];  //This is the generic list
    15.                 }
    16.                 }


    //LOAD SECTION UNITY - IT IS WORKING ONLY WITH PLAYERPREF NOT WITH MYSQL!!!
    Code (CSharp):
    1. foreach(saveddata obj in savedItems)
    2. {
    3. GameObject myPrefab = Resources.Load("Prefabs/"+obj.savedName) as GameObject;
    4. GameObject spawnedObject = Instantiate<GameObject>(myPrefab);
    5. spawnedObject.transform.position = new Vector3(obj.savedposx, obj.savedposy, obj.savedposz);
    6. spawnedObject.transform.rotation = Quaternion.Euler(obj.savedrotx, obj.savedroty, obj.savedrotz);
    7. spawnedObject.name = spawnedObject.name.Replace("(Clone)","").Trim();
    8. spawnedObject.name = spawnedObject.name.Replace("U","Ü").Trim();
    9. }
     
    Last edited: Dec 15, 2015
  5. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Here is my solution :):):):):):)
    I don't understand why I need to solve this, why I don't get answer...
    I never learned programming, just tried many time...


    I have sent the datas without generic list and without serialise, I used string with sum.
    Code (CSharp):
    1. GameObject[] objects = GameObject.FindGameObjectsWithTag("Player");
    2.                     foreach (GameObject go in objects)
    3.                     {
    4.                         //go.name = go.name.Replace("Ü","U").Trim();
    5.  
    6.                         savedName += go.name+"\n";
    7.                         savedposx += float.Parse(go.transform.position.x.ToString())+"\n";
    8.                         savedposy += float.Parse(go.transform.position.y.ToString())+"\n";
    9.                         savedposz += float.Parse(go.transform.position.z.ToString())+"\n";
    10.                         savedrotx += go.transform.rotation.eulerAngles.x+"\n";
    11.                         savedroty += go.transform.rotation.eulerAngles.y+"\n";
    12.                         savedrotz += go.transform.rotation.eulerAngles.z+"\n";
    13.  
    14.                     }


    I got back datas from server and I split them.
    !!! The last character is line break, therefor I used length-1. This is removing the last line break!!!.
    Code (CSharp):
    1. string[] loadNames1Split = loadNames1.Split('\n');
    2.                         string[] loadPosx1Split = loadPosx1.Split('\n');
    3.                         string[] loadPosy1Split = loadPosy1.Split('\n');
    4.                         string[] loadPosz1Split = loadPosz1.Split('\n');
    5.                         string[] loadRoty1Split = loadRoty1.Split('\n');
    6.  
    7.  
    8.                         for (int a = 0; a < loadNames1Split.Length-1; a++)
    9.                         {
    10.                          
    11.                             GameObject myPrefab = Resources.Load("Prefabs/"+loadNames1Split[a]) as GameObject;
    12.                             GameObject spawnedObject = Instantiate<GameObject>(myPrefab);
    13.                             spawnedObject.transform.position = new Vector3(float.Parse(loadPosx1Split[a].ToString()), float.Parse(loadPosy1Split[a].ToString()), float.Parse(loadPosz1Split[a].ToString()));
    14.                             spawnedObject.transform.rotation = Quaternion.Euler(spawnedObject.transform.rotation.x, float.Parse(loadRoty1Split[a].ToString()) , spawnedObject.transform.rotation.z);
    15.                         }
     
  6. noclipstudio

    noclipstudio

    Joined:
    Mar 2, 2021
    Posts:
    73

    They don't answer because they don't know how to solve it, the programmer's daily challenge is to solve problems alone, many times people won't be able to help us and you need to chase and challenge yourself one more time, or two, three more, until you succeed.
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    While there is some truth to it, there was no need to bump a 7+ years old thread for this ^^. If I had seen the question back then, I would have told him that this part:
    Code (CSharp):
    1. savedItems.ToString();
    Makes no sense because a generic List does not magically turn itself into a string. By default ToString just returns the class name of an object. A class can of course provide their own implementation to return something more reasonable (like Unity's Vector3 struct for example), though ToString is mainly meant for debugging purposes in the .NET framework, not for serializing data.

    In order to actually serialize the data into a reasonable format (like JSON) you would need a json serializer / deserializer. Unity has the JsonUtility built into the engine, though it's a bit limited. Most use the Newtonsoft Json.NET library.

    So the short answer for the actual question would have been: impossible, since no data was actually stored in the database and a completely different approach is needed to store and load the data. Of course that's what the OP did with a custom serialization format by seperating each value by a new line character. Not really pretty, but does work of course.

    I should add that if that server is actually online and connected to the internet, I highly recommend to shut it down. The PHP code is vulnerable to SQL injection attacks. So if anyone comes across this, never do something like this

    Code (php):
    1. $insert = "INSERT INTO `saveloadtable` (`User`, `Email`, `Vezetek`, `Osszeallitas`, `Saveditems`) VALUES ('" . $user2E . "', '" . $email2E . "', '" . $vezetekE . "', '" . $osszeallitasE . "', '" . $savedItemsE . "')";
    Look up the recent docs how to use prepared statements and how to safely handle userdata.
     
    TaleOf4Gamers likes this.
  8. noclipstudio

    noclipstudio

    Joined:
    Mar 2, 2021
    Posts:
    73
    Awesome!