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:
    4,440
    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.                         }
     
unityunity