Search Unity

How to Add and retreive Items from a nested list

Discussion in 'Scripting' started by jackson_31, Apr 13, 2015.

  1. jackson_31

    jackson_31

    Joined:
    Sep 20, 2014
    Posts:
    84
    Here is an example of a type of game I would like to create that can apply to many different scenarios.
    In this example lets use shopping malls and stores


    I have two Scenes, overview, and mall

    the overview scene allows user to place a single type of game object (shopping malls) at various locations on the overview scene

    the mall scene allows user to place multiple different game objects (stores: ie best buy, Mc Donalds, EBgames etc) at various positions in the mall scene


    For Example:
    Place mall 1 mall 2 and mall 3 at differnt positions in the overview scene.
    Click on mall 1 and go to mall scene for mall 1
    Place stores at various locations in mall 1.
    Click on overview to go back to overview and then click on mall 2 and do the same.

    I place GameControl script in both scenes so that I can add store, mall info and retrieve store, mall info from both scenes

    I have a mall class that contains name of the mall and a list of stores

    the store class contains transform and position of store


    from the overview script I am able to add a mall to the list

    Code (CSharp):
    1.     private List<STORE> store = new List<STORE>();
    2.     GameControl.control.malls.Add (new MALL (mall_label, store));

    however I am not sure how to add a store to the mall from the mall scene
    Also I am not sure How I can iterate through all the malls and show positions of all stores


    This is the Overview script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class OVERVIEW {
    6.     public string mall_name;
    7.     public List<STORE> store = new List<STORE>();
    8.  
    9.  
    10.    
    11.     public OVERVIEW (string new_mall_name, List<STORE> new_store)
    12.     {
    13.         mall_name = new_mall_name;
    14.         store = new_store;
    15.         }
    16. }
    17.  

    this is the Store Script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class STORE {
    5.     public Transform obj;
    6.     public Vector3 obj_pos;
    7.  
    8.  
    9.  
    10.    
    11.     public STORE (Transform new_object, Vector3 new_object_pos)
    12.     {
    13.         obj = new_object;
    14.         obj_pos = new_object_pos;
    15.         }
    16. }



    this is the GameControl Script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class GameControl : MonoBehaviour {
    6.  
    7.     public static GameControl control;
    8.     public List<OVERVIEW> malls = new List<OVERVIEW>();
    9.     public string mall_label;
    10.  
    11.  
    12.  
    13.     void Awake() {
    14.         if (control ==null)
    15.         {
    16.             DontDestroyOnLoad(gameObject);
    17.             control=this;
    18.         }
    19.         else if (control !=this)
    20.         {
    21.             Destroy(gameObject);
    22.         }
    23.    
    24.         }
    25.  
    26.     }
     
  2. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    Well, if I were you, I had one script for mall (something like this, not tested) and I add some events: https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class MALL {
    4.     public List<STORE>  stores;
    5.     private string mallName;
    6.     public string mallLabel { get { return mallName; } }  
    7.    
    8.     public delegate void OnAddStore(STORE _store);
    9.     public delegate void OnRemoveStore(STORE _store);
    10.  
    11.     public MALL (string mall_label, List<STORE> _stores = null)
    12.     {
    13.         this.mallName = mall_label;
    14.         this.stores = _stores == null ? new List<STORE>() : _stores;
    15.         }
    16.  
    17.    public void AddStore( STORE newStore )
    18.    {
    19.         this.stores.add( newStore );
    20.         if( OnAddStore != null ) OnAddStore(newStore);
    21.    }
    22.    
    23.    public STORE GetLastStore()
    24.    {
    25.        STORE _store = this.stores.Count > 0 ? return this.stores[this.stores-1] : null;
    26.     }
    27. //    ...remove here
    28.  
    29. }

    and in your Mall, just add your events/delegates and just use your positions to place them, something like this

    Code (CSharp):
    1. //your mall is OVERVIEW, so just change the class if you dont want rename
    2. //if you use delegate/events you just run this once
    3. foreach( Mall mall in GameControl.malls )
    4. {
    5.    foreach( STORE store in mall.stores )
    6.    {
    7.       Instantiate( store.obj, obj.position, Quaternion.identity );
    8.    }
    9. }
    10.  
    11. //so if you want add new store for your scene, im assuming you're loadlevelasync, just do a function with delegate
    12.  
    13. //somewhere add your delegate like this
    14. mall.OnAddStore += NewStore;
    15. //and your function like this
    16. void NewStore(STORE _store)
    17. {
    18.          Instantiate( _store.obj, _obj.position, Quaternion.identity );
    19. }
    It's a complicated question, because have infinite ways to do. Expected can be clear and light you =)
     
  3. jackson_31

    jackson_31

    Joined:
    Sep 20, 2014
    Posts:
    84
    Thanks for this. I think I understand but I am having a few issues

    I get a stack overflow error when I try to do this in another script
    Code (CSharp):
    1. foreach( Mall mall in GameControl.malls )
    2. Debug.Log(mall.mallLabel);
    but in the mall class if I change
    Code (CSharp):
    1. publicstring mallLabel{ get {return mallName; }}
    to
    Code (CSharp):
    1. publicstring mallLabel;
    and then change
    Code (CSharp):
    1. this.mallName= mall_label;
    to
    Code (CSharp):
    1. mallName= mall_label;
    I no longer get stack overflow error

    Also in this line below there is a lot of stuff happening and I am not sure how it works
    is there any documentation to this type of statement
    Code (CSharp):
    1. this.stores= _stores ==null?new List<STORE>(): _stores;
     
  4. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    Nice. =)

    in this line, is nothing else than an "if statement"

    Code (CSharp):
    1. this.stores= _stores ==null?new List<STORE>(): _stores;
    2.  
    3. is equal
    4.  
    5. if( _stores == null )
    6. {
    7.   this.stores = new List<STORE>();
    8. }
    9. else
    10. {
    11.   this.stores = _stores;
    12. }