Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

saving data in mysql , condition to save GO with tag "products" not working

Discussion in 'Scripting' started by vahi2008, Jul 21, 2014.

  1. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    Hello,

    in my script i am saing data to mysql, i wanted to do a little change in it, i only wanted to save Game Objects in the scene with tag "products" , for that i wrote this code

    Code (CSharp):
    1. IEnumerator PutData(string tag){
    2.         window = "";
    3.         loginGUI logingui = gameObject.GetComponent<loginGUI>();
    4.         var form = new WWWForm();
    5.         form.AddField( "direction", "put" );
    6.         form.AddField( "tag", tag );
    7.         form.AddField( "owner", logingui.roomID);
    8.         string entries = "";
    9.  
    10.         foreach(ObjectInPlay obj in objectsInPlay){
    11.             entries += obj.ToString()+"\n";
    12.         }
    13.  
    14.         form.AddField("data", entries);
    15.        
    16.         WWW www = new WWW("http://funiks.com/shop3d/getObjects.php", form);
    17.         yield return www;
    18.         Debug.Log(www.text);
    19.     }

    and called this as -
    Code (CSharp):
    1. if (GUI.Button(new Rect(10, 20, 100, 20), "Save Products")) StartCoroutine(PutData("products"));
    but it is still saving the game objects with other tags, what am i doing wrong ??

    This is my entire script -
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Text.RegularExpressions;
    5.  
    6. public class resources : MonoBehaviour {
    7.     public static GameObject[] prefabs;
    8.    
    9.     public static List<ObjectInPlay> objectsInPlay;
    10.     private string window = "";
    11.    
    12.     //F***ing screen rectangles for GUI boxes, but seems they are never used!
    13.     private Rect MainRect = new Rect(20, 20, 120, 120);
    14.     private Rect CreateRect = new Rect(20, 20, 120, 120);
    15.     private Rect LoadSaveRect = new Rect(20, 40, 120, 160);
    16.     private Rect LoadTexRect = new Rect(220, 40, 220, 180);
    17.     private Rect ProdInfoRect = new Rect(220,40,220,150);
    18.    
    19.     private ObjectInPlay selectedObject;
    20.     private string clickedObject;//onclicking we add values in the fields
    21.     private string result = "";// result of the downloaded data
    22.     private int numberOfRecordsinDB; // for loading 1 of 20 wala
    23.    
    24.     //local variable for setting GUI about window
    25.     private string textureLink = "";
    26.     private string urlPageLink = "";
    27.     private string price = "0.0";
    28.     private string prodName = "Your product name";
    29.     private string prodInfo = "Add about your product";
    30.     private bool isObject = false;
    31.    
    32.     //some S*** you can ignore, if you wanted to debug it, go ahead write a whole app from start, easier
    33.     public bool client;
    34.     public List<string> categories;
    35.     public Dictionary<string, object[]> items = new Dictionary<string, object[]>();
    36.     private string currentCategory;
    37.     public Transform loadingGUI;
    38.     private uint totalTextures = 0;
    39.     private uint loadedTextures = 0;
    40.    
    41.     //no one ever knows what this does
    42.     public static ObjectInPlay GetObject(GameObject test){
    43.         foreach(ObjectInPlay obj in objectsInPlay){
    44.             if(obj.go == test){
    45.                 return obj;
    46.             }
    47.         }
    48.         return null;
    49.     }
    50.    
    51.     void Start(){
    52.         objectsInPlay = new List<ObjectInPlay>();
    53.        
    54.         //loading new products items
    55.         foreach(string category in categories)
    56.         {
    57.             items[category] = Resources.LoadAll(category , typeof(GameObject));;
    58.         }
    59.         object[] objs = Resources.LoadAll("" , typeof(GameObject));
    60.         prefabs = new GameObject[objs.Length];
    61.         for(int i=0; i<objs.Length; i++){prefabs[i] = (GameObject)objs[i];}
    62.         Rect rect = CreateRect;
    63.         rect.height = 200;
    64.         CreateRect = rect;
    65.         loadingGUI.guiText.enabled = false;
    66.     }
    67.     public void loadRoom()
    68.     {
    69.         //loads an individual cell from database
    70.         StartCoroutine(GetData());
    71.     }
    72.     //main loop, needs to be rewritten
    73.     void Update()
    74.     {
    75.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    76.         RaycastHit hit;
    77.         int layerMask = 1 << 9;
    78.         string pageUrl = "http://google.com";
    79.         if(Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0)
    80.         {
    81.             if (Physics.Raycast (ray, out hit, 100.0f, layerMask))
    82.             {
    83.                 if(GetObject(hit.collider.gameObject) != null)
    84.                 {
    85.                     selectedObject = GetObject(hit.collider.gameObject);
    86.  
    87.                 }
    88.                 else
    89.                 {
    90.                     Debug.LogError("found no object");
    91.                 }
    92.                 if(selectedObject != null && SceneType.LeftClickUrl == false)
    93.                 {
    94.                    
    95.                     clickedObject = hit.collider.gameObject.name;
    96.                     Debug.Log("name of Object is: "+ clickedObject);
    97.                    
    98.                     foreach(ObjectInPlay obj in objectsInPlay){
    99.                         if(selectedObject == obj)
    100.                         {
    101.                             textureLink=selectedObject.texLink;
    102.                             urlPageLink=selectedObject.urlLink;
    103.                             prodInfo=selectedObject.prod_info;
    104.                             prodName=selectedObject.prod_name;
    105.                             price=selectedObject.price;
    106.  
    107.                             if(selectedObject.isObject == 0)
    108.                             {
    109.                                 isObject=false;
    110.                             }
    111.                             else
    112.                             {
    113.                                 isObject=true;
    114.                             }
    115.                            
    116.                             Debug.Log("textureLink:"+textureLink+"\n"+
    117.                                     "urlPageLink:"+urlPageLink+"\n"+
    118.                                     "prodInfo:"+prodInfo+"\n"+
    119.                                      "prodName:"+prodName+"\n"+
    120.                                     "price:"+price+"\n");
    121.                            
    122.                             break;
    123.                         }
    124.                     }
    125.                     /*if(Regex.IsMatch(result, clickedObject))
    126.                     {
    127.                         //Debug.Log(result);
    128.                         string[] splitdata = result.Split('\n');
    129.                         for(int i = 0; i < splitdata.Length; i++)
    130.                         {
    131.                             string[] data2 = splitdata[i].ToString().Split(',');
    132.                             if(data2[0] == clickedObject)
    133.                             {
    134.                                
    135.                                 textureLink=data2[11];
    136.                                 urlPageLink=data2[12];
    137.                                 prodInfo=data2[13];
    138.                                 price=data2[14];
    139.                                 Debug.Log("textureLink:"+textureLink+"\n"+
    140.                                     "urlPageLink:"+urlPageLink+"\n"+
    141.                                     "prodInfo:"+prodInfo+"\n"+
    142.                                     "price:"+price+"\n");
    143.                             }
    144.                         }
    145.                     }*/
    146.                     window = "TexLink";
    147.                     //add the url of this image in the field
    148.                 }
    149.                
    150.                
    151.                 if(SceneType.LeftClickUrl == true)
    152.                 {
    153.                     pageUrl=selectedObject.urlLink;// need to assign value here
    154.                     /*Debug.Log("Entered inside");
    155.                       if (Application.platform == RuntimePlatform.WindowsWebPlayer || Application.platform == RuntimePlatform.OSXWebPlayer)
    156.                       {  
    157.                            Application.ExternalCall("LoadExternalWebpage", pageUrl);
    158.                            //audio.Play ();
    159.                     }*/
    160.                 }
    161.             }
    162.         }
    163.        
    164.         if(Input.GetMouseButtonDown(0) && (SceneType.LeftClickUrl == true) && GUIUtility.hotControl == 0)
    165.         {
    166.  
    167.             if (Physics.Raycast (ray, out hit, 100.0f, layerMask))
    168.             {
    169.                 if(GetObject(hit.collider.gameObject) != null)
    170.                 {
    171.                     selectedObject = GetObject(hit.collider.gameObject);
    172.                 }
    173.                 clickedObject = hit.collider.gameObject.name;
    174.                    
    175.                     if(Regex.IsMatch(result, clickedObject))
    176.                 {
    177.                         Debug.Log("SELECTED OBJECT: "+result);
    178.                         string[] splitdata = result.Split('\n');
    179.                         for(int i = 0; i < splitdata.Length; i++)
    180.                         {
    181.                             string[] data2 = splitdata[i].ToString().Split(',');
    182.                             if(data2[0] == clickedObject)
    183.                             {
    184.                            
    185.                                 prodInfo=data2[13];
    186.                             }
    187.                         }
    188.                     }
    189.                 if(selectedObject != null && SceneType.LeftClickUrl == true && selectedObject.isObject == 0){
    190.                     window="ProdInfo";
    191.                 }
    192.             }
    193.         }
    194.        
    195.     }
    196.    
    197.    
    198.     //better be named OnFuckingGUI
    199.     void OnGUI()
    200.     {
    201.         if(client == false && window == "")  MainRect = GUI.Window(0, MainRect, DoMainWindow, "Create");
    202.         if(client == false && window == "create") CreateRect = GUI.Window(0, CreateRect, DoCreate, "Create");
    203.         if(window == "io") LoadSaveRect = GUI.Window(0, LoadSaveRect, DoLoadSave, "Load/Save");
    204.         if(window == "TexLink") {
    205.             LoadTexRect = GUI.Window(1, LoadTexRect, DoTexSave, "Add");
    206.         }
    207.         if(window == "ProdInfo")
    208.         {
    209.            
    210.             ProdInfoRect = GUI.Window(1, ProdInfoRect, ShowProd, "About");
    211.         };
    212.        
    213.         if(client == true)
    214.         {
    215.             if (GUI.Button(new Rect(Screen.width - 100, 40, 80, 20), "Checkout")){
    216.                 openLink("http://funiks.com/shop3d/admin/cart.php?checkout");
    217.             }
    218.         }
    219.     }
    220.     //does its job well, main menu
    221.     void DoMainWindow(int windowID){
    222.         //if (GUI.Button(new Rect(10, 20, 100, 20), "Create")) window = "create";
    223.         Rect rect = MainRect;
    224.         rect.height = ((categories.Count+1) * 30) + 20;
    225.         MainRect = rect;
    226.        
    227.         for(int i=0; i<categories.Count; i++){
    228.             if (GUI.Button(new Rect(10, 20 + i * 30, 100, 20), categories[i]))
    229.             {
    230.                 window = "create";
    231.                 currentCategory = categories[i];
    232.             }
    233.         }
    234.         if (GUI.Button(new Rect(10, 20 + (categories.Count) * 30, 100, 20), "Load/Save")) window = "io";
    235.     }
    236.    
    237.     //does actually create an object, something pretty dependant on everything else
    238.     void DoCreate(int windowID)
    239.     {
    240.         Rect rect = CreateRect;
    241.         rect.height = ((items[currentCategory].Length+1) * 30) + 20;
    242.         CreateRect = rect;
    243.        
    244.         MouseOrbit orbitScript = Camera.main.GetComponent<MouseOrbit>();
    245.         Vector3 target = orbitScript.target.position;
    246.         prefabs = new GameObject[items[currentCategory].Length];
    247.        
    248.         for(int i=0; i<items[currentCategory].Length; i++){prefabs[i] = (GameObject)items[currentCategory][i];}
    249.         for(int i=0; i<prefabs.Length; i++){
    250.             if (GUI.Button(new Rect(10, 20 + i * 30, 100, 20), prefabs[i].name))
    251.             {
    252.                 ObjectInPlay oip = new ObjectInPlay(prefabs[i].name+"^"+target.x+"^"+target.y+"^"+target.z);
    253.             }
    254.         }
    255.         if(GUI.Button(new Rect(10, 20 + items[currentCategory].Length * 30, 100, 20), "Done")) window = "";
    256.     }
    257.    
    258.     //save windows, it promises to do what you tell it, all fine
    259.     void DoLoadSave(int windowID) {
    260.         //if (GUI.Button(new Rect(10, 20, 100, 20), "Load")) StartCoroutine(GetData());
    261.         if (GUI.Button(new Rect(10, 20, 100, 20), "Save Products")) StartCoroutine(PutData("products"));
    262.         if (GUI.Button(new Rect(10, 50, 100, 20), "Save Acces..")) StartCoroutine(PutData2());
    263.         if (GUI.Button(new Rect(10, 80, 100, 20), "Clear")) ClearObjectsInPlay();
    264.         if(GUI.Button(new Rect(10, 110, 100, 20), "Done")) window = "";
    265.     }
    266.    
    267.     // might not be the source for all this S***, but at least has good code ident
    268.     void DoTexSave(int windowID) {
    269.         textureLink = GUILayout.TextField(textureLink);
    270.         if(!isObject)
    271.         {
    272.             urlPageLink = GUILayout.TextField(urlPageLink);
    273.             selectedObject.prod_name    = GUILayout.TextArea(selectedObject.prod_name);
    274.             prodInfo    = GUILayout.TextArea(prodInfo);
    275.             price    = GUILayout.TextArea(price);
    276.         }
    277.         isObject = GUILayout.Toggle ( isObject, "Toggle");
    278.        
    279.         if (GUI.Button(new Rect(5, 145, 60, 20), "OK")){
    280.             if(textureLink!=null)UpdateTexture(selectedObject, textureLink);
    281.             UpdateLink(selectedObject, urlPageLink);
    282.  
    283.             UpdateProdName(selectedObject, prodName);
    284.             UpdateProdInfo(selectedObject, prodInfo);
    285.             UpdatePrice(selectedObject, price);
    286.             UpdateIsObject(selectedObject, isObject);
    287.            
    288.             textureLink = "";
    289.             urlPageLink = "";
    290.             window = "";
    291.             Debug.Log("SO: "+selectedObject);
    292.         }
    293.         if (GUI.Button(new Rect(70, 145, 70, 20), "Cancel")) window="";
    294.         if (GUI.Button(new Rect(150, 145, 60, 20), "Delete"))
    295.         {
    296.             foreach(ObjectInPlay obj in objectsInPlay){
    297.                 if(selectedObject == obj)
    298.                 {
    299.                     objectsInPlay.Remove(obj);
    300.                     Destroy(obj.go);
    301.                     break;
    302.                 }
    303.             }
    304.             window="";
    305.         }
    306.         GUI.DragWindow();
    307.     }
    308.     //open link
    309.     void openLink(string theLink)
    310.     {
    311.         if (Application.platform == RuntimePlatform.WindowsWebPlayer || Application.platform == RuntimePlatform.OSXWebPlayer)
    312.             Application.ExternalCall ("LoadExternalWebpage", theLink);
    313.         else
    314.             Application.OpenURL(theLink);
    315.     }
    316.     //shows products about box
    317.     void ShowProd(int windowID) {
    318.         selectedObject.prod_name = GUILayout.TextArea(selectedObject.prod_name);
    319.         selectedObject.prod_info = GUILayout.TextArea(selectedObject.prod_info);
    320.         selectedObject.price = GUILayout.TextArea(selectedObject.price);
    321.         if (GUI.Button(new Rect(25, 80, 80, 20), "Add to cart")){
    322.             //StartCoroutine(AddToCart());
    323.             openLink("http://funiks.com/shop3d/admin/cart.php?action=add&product_id="+selectedObject.id);
    324.         }
    325.         if (GUI.Button(new Rect(115, 80, 80, 20), "Read more")){
    326.             openLink(selectedObject.urlLink);
    327.         }
    328.         if (GUI.Button(new Rect(25, 120, 80, 20), "OK")){
    329.             Debug.Log("textureLink: "+textureLink);
    330.             UpdateTexture(selectedObject, textureLink);
    331.             window = "";
    332.         }
    333.         GUI.DragWindow();
    334.     }
    335.     //what the hell? what this an ienumator to start with? then say write it in c#, write it in c#, why the F*** should i write it in c#? noob programmers who say only what you have learnt.
    336.      IEnumerator GetData(){
    337.         window = "";
    338.        
    339.         //ClearObjectsInPlay();
    340.        
    341.         loginGUI logingui = gameObject.GetComponent<loginGUI>();
    342.    
    343.         var form = new WWWForm();
    344.         form.AddField( "direction", "get" );
    345.         form.AddField( "owner", logingui.roomID);
    346.         WWW www = new WWW("http://funiks.com/shop3d/getObjects.php", form);
    347.         //WWW www = new WWW("http://localhost/upload/getObjects.php", form);
    348.         yield return www;
    349.         result = www.text;
    350.         // text while is loading
    351.         Debug.Log("DATA RECIEVED FROM SERVER:\n"+www.text);
    352.         string[] lines = result.Split('\n');
    353.         numberOfRecordsinDB = lines.Length-1; // for loading 1 of 20 wala
    354.         Debug.Log("NUMBER OF RECORDS IN DB WAS: "+numberOfRecordsinDB);
    355.         foreach(string line in lines){
    356.             if(line != ""){
    357.                 ObjectInPlay obj = new ObjectInPlay(line);
    358.                 if(obj.texLink != "") StartCoroutine(SetTexture(obj));
    359.             }
    360.         }
    361.     }
    362.     //command the most complicated php code on earth for a save operation, but it does it fine no matter how messy code
    363.     IEnumerator PutData(string tag){
    364.         window = "";
    365.         loginGUI logingui = gameObject.GetComponent<loginGUI>();
    366.         var form = new WWWForm();
    367.         form.AddField( "direction", "put" );
    368.         form.AddField( "products", tag );
    369.         form.AddField( "owner", logingui.roomID);
    370.         string entries = "";
    371.        
    372.         foreach(ObjectInPlay obj in objectsInPlay){
    373.             entries += obj.ToString()+"\n";
    374.         }
    375.  
    376.         form.AddField("data", entries);
    377.        
    378.         WWW www = new WWW("http://funiks.com/shop3d/getObjects.php", form);
    379.         yield return www;
    380.         Debug.Log(www.text);
    381.     }
    382.    
    383.    
    384.        
    385.        
    386.    
    387.    
    388.     IEnumerator PutData2(){
    389.         window = "";
    390.         loginGUI logingui = gameObject.GetComponent<loginGUI>();
    391.         var form = new WWWForm();
    392.         form.AddField( "direction", "put" );
    393.         form.AddField( "owner", logingui.roomID);
    394.         string entries = "";
    395.        
    396.         foreach(ObjectInPlay obj in objectsInPlay){
    397.             entries += obj.ToString()+"\n";
    398.         }
    399.  
    400.         form.AddField("data", entries);
    401.        
    402.         WWW www = new WWW("http://funiks.com/shop3d/getObjects.php", form);
    403.         yield return www;
    404.         Debug.Log(www.text);
    405.     }
    406.    
    407.    
    408.    
    409.     //again it does it job good
    410.     IEnumerator AddToCart(){
    411.         //window = "";
    412.         var form = new WWWForm();
    413.         form.AddField( "action", "add" );
    414.         form.AddField( "product_id", selectedObject.id);
    415.         Debug.Log("http://funiks.com/shop3d/admin/cart.php?action=add&product_id="+selectedObject.id);
    416.         WWW www = new WWW("http://funiks.com/shop3d/admin/cart.php", form);
    417.         yield return www;
    418.         Debug.Log("Added to cart: "+www.text);
    419.     }
    420.     //clears objects in play!
    421.     void ClearObjectsInPlay(){
    422.         foreach(ObjectInPlay obj in objectsInPlay){
    423.             Destroy(obj.go);
    424.         }
    425.         objectsInPlay = new List<ObjectInPlay>();
    426.         window = "";
    427.     }
    428.    
    429.     void UpdateTexture(ObjectInPlay obj, string ptexLink){
    430.         if(obj.texLink != ptexLink)
    431.         {
    432.             obj.texLink = ptexLink;
    433.             UpdateTexture(obj);
    434.         }
    435.     }
    436.    
    437.     void UpdateLink(ObjectInPlay obj, string urlLink){
    438.         obj.urlLink = urlPageLink;
    439.         Debug.Log("Updating with:"+urlPageLink);
    440.        
    441.     }
    442.    
    443.    
    444.     void UpdatePrice(ObjectInPlay obj, string pprice){
    445.         obj.price = pprice;
    446.     }
    447.     void UpdateIsObject(ObjectInPlay obj, bool pisObject){
    448.         if(pisObject == false)
    449.             obj.isObject = 0;
    450.         else
    451.             obj.isObject = 1;
    452.     }
    453.    
    454.     void UpdateProdInfo(ObjectInPlay obj, string prod_info){
    455.         obj.prod_info = prodInfo.Replace("^", "");
    456.     }
    457.     void UpdateProdName(ObjectInPlay obj, string prod_name){
    458.         obj.prod_name = prodName.Replace("^", "");
    459.     }
    460.    
    461.     void UpdateTexture(ObjectInPlay obj){if(obj.texLink != "")StartCoroutine(SetTexture(obj));}
    462.    
    463.     //loads external texture image
    464.     private IEnumerator SetTexture(ObjectInPlay obj){
    465.         totalTextures ++;
    466.         Debug.Log(">"+loadedTextures);
    467.         loadingGUI.guiText.text = "Loading textures: "+(loadedTextures+1)+"/"+totalTextures;
    468.         loadingGUI.guiText.enabled = true;
    469.        
    470.         WWW www = new WWW(obj.texLink);
    471.        
    472.         yield return www;
    473.         if(obj.go.renderer)
    474.         {
    475.             loadedTextures++;
    476.             Debug.Log("<"+loadedTextures);
    477.             obj.go.renderer.material.mainTexture = www.texture;
    478.             loadingGUI.guiText.text = "Loading textures: "+loadedTextures+"/"+totalTextures;
    479.            
    480.             if(totalTextures == loadedTextures)
    481.             {
    482.                 totalTextures = 0;
    483.                 loadedTextures = 0;
    484.                 loadingGUI.guiText.enabled = false;
    485.                
    486.             }
    487.         }
    488.     }
    489.  
    490. }
    491.  
    492.  
     
  2. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    I have no idea what a lot of that means! It seems really advanced
     
  3. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    Didnt see any filtering based on tag? I mean in PutData you have modified it to take in string tag, but you only use it in form.Addfield ..i suppose you should add filtering to this loop:

    Code (CSharp):
    1.         foreach(ObjectInPlay obj in objectsInPlay){
    2.             entries += obj.ToString()+"\n";
    3.         }
    like this:

    Code (CSharp):
    1.         foreach(ObjectInPlay obj in objectsInPlay){
    2. if (obj.name == tag)
    3.             entries += obj.ToString()+"\n";
    4.         }
    your code is kinda hard to follow at least for me :)