Search Unity

Advanced Arrays in C++?

Discussion in 'Scripting' started by Faestus, Jul 27, 2014.

  1. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    I really need some crucial tasks relating array.

    In flash, they look like:
    Array.push(Item) - add an item to an array
    Array[ItemName] - get an array by name

    I know you can usually do them in C# by using std::vector and other fancy scripts, but that doesn't work in nity.

    Is there a way around?
     
    Last edited: Jul 27, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unity doesn't use C++. Look here for some of the collections you can use in C# and Unityscript.

    --Eric
     
  3. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Sorry, I ment C#.

    Edit: That does solve SOME of my problems. Not sure how to add named indexes though...
     
  4. GakHyeong

    GakHyeong

    Joined:
    Jul 2, 2013
    Posts:
    2
    I think you should use the "Dictionary" Class for it.
    This class will have a "key" and a "value". I recommend you to use this class like this.
    Dictionary < key, value> test_Dic = new Dictionary<key, value>();
    The 'key' part should be written into type of variable.
    For example
    Dictionary < int, value> test_Dic = new Dictionary< int, value>();
    if you are using Item with Item_ID(int) then you should use int for 'key' value.
    and 'value' can be anything. It can be written into type of variable like 'key'.
    For example
    Dictionary <int, int> or Dictionary < int, string>
    like this.
    And finally I like to say you can use 'value' into class type. This is very important part.
    Because you can use like this!

    Dictionary <string(this is for Item name), Item(this is item class)> = new Dictionary <string, Item>();

    This is very useful and you will use "Dictionary" class very often with "ArrayList".
    Before I end up this commend, you should add top of the code like this(blue one).

    using UnityEngine;
    using System.Collection;
    using System.Collection.Generic;

    if you are more interested in Dictionary class, go into this site.

    http://msdn.microsoft.com/en-US/library/vstudio/xfhwa508(v=vs.110).aspx
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    You mentioned std::vector, though, so I suggest refreshing on the differences between the two. They're both called C-something and they can often look similar or even identical, but they are quite different languages and life will be hard if you get them mixed up.
     
  6. GakHyeong

    GakHyeong

    Joined:
    Jul 2, 2013
    Posts:
    2
    Hmm.... I used Dictionary before in C# already when I was making with inventory system....
     
  7. hammil

    hammil

    Joined:
    Jun 5, 2013
    Posts:
    56
    The C# equivalent of std::vector is List<T>