Search Unity

Help with class in Javascript

Discussion in 'Scripting' started by raulcorrales, Dec 6, 2010.

  1. raulcorrales

    raulcorrales

    Joined:
    Dec 6, 2010
    Posts:
    18
    Code (csharp):
    1.  //Define the class
    2.    
    3.     class author{
    4.    
    5.     function author(a:String,b:String,c:int)
    6.    
    7.        {
    8.        
    9.     this.name = a;
    10.    
    11.        this.lastname = b;
    12.    
    13.        this.age = c;
    14.    
    15.        }
    16.    
    17.     //Declaring the variables
    18.    
    19.     var name:String;
    20.    
    21.     var lastname:String;
    22.    
    23.     var age:int;
    24.    
    25.     }
    26.    
    27.     //I make a new object
    28.    
    29.     newBook = new author("J.R.","Tolkien",60);
    30.    
    31.    
    32.     //I make other new object
    33.    
    34.     newBook = new author("Agatha","Christie",42);

    in this code the variable "newBook" to save the whole object, returns to save only the last one that was created...


    Any help???
     
  2. diabloroxx

    diabloroxx

    Joined:
    Jan 20, 2010
    Posts:
    71
    Looks like you are dumping new Author data into the same object again and again. Use different objects for adding different author. You can also use an array of Objects.
     
  3. raulcorrales

    raulcorrales

    Joined:
    Dec 6, 2010
    Posts:
    18
    can you write a small example please?? thank you!
     
  4. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Code (csharp):
    1. newBook = new author("J.R.","Tolkien",60);
    2. newBook2 = new author("Agatha","Christie",42);

    Just to expose the concept of object references:
    Code (csharp):
    1.  
    2. newBook = new author("J.R.","Tolkien",60);
    3. Debug.Log(newBook.lastname);//"Tolkien"
    4. sameBook = newBook;
    5. Debug.Log(sameBook.lastname);//"Tolkien"
    6.  
    7. //have newBook point to a new instance of author
    8. newBook = new author("Agatha","Christie",42);
    9. Debug.Log(newBook.lastname);//"Christie"
    10.  
    11. //sameBook is still pointing to the original instance of newBook
    12. Debug.Log(sameBook.lastname);//"Tolkien"
    13.  
     
    Last edited: Dec 6, 2010
  5. raulcorrales

    raulcorrales

    Joined:
    Dec 6, 2010
    Posts:
    18
    and without changing the name to newBook?
     
  6. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    What do you mean?

    BTW, I edited my post, might help some.
     
  7. raulcorrales

    raulcorrales

    Joined:
    Dec 6, 2010
    Posts:
    18
    ok, thanks. and how I can save the data in a variable of the object itself author? thanks again
     
  8. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Sorry, raulcorrales, I don't understand what you're asking. Can you try rephrasing it please?
     
  9. raulcorrales

    raulcorrales

    Joined:
    Dec 6, 2010
    Posts:
    18
    I make 5 Books

    for(var i : int = 0; i < 5; i++)
    {
    newBook = new author("J.R.","Tolkien",60);
    }



    How i delete all the books?

    How i delete the second book?

    How i change the name of all the books?


    Thanks FizixMan and sorry for may bad english
     
  10. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Ahh, got you. I think you're looking for the concept of collections (arrays and lists).

    Given your questions, likely you'll probably want to use a List in that case:
    Code (csharp):
    1. var books : List.<author> = new List.<author>();
    2.  
    3. for (var i : int = 0; i < 5; i++)
    4. {
    5.     var newBook : author = new author("J.R.","Tolkien",60);
    6.     books.Add(newBook);
    7. }
    8.  
    9.  
    10. //delete all books:
    11. books.Clear();
    12.  
    13.  
    14. //delete the second book:
    15. books.RemoveAt(1); //0 indexed, so 0 is the "first", 1 is the "second"
    16. //note, that this will reorder it, so before you had five books, they all move up.  The original "third" book now becomes the new "second" book
    17.  
    18.  
    19. //change all names:
    20. for (var i : int = 0; i < books.Count; i++
    21. {
    22.     book[i].name = "raulcorrales";
    23. }
    Also, most likely you won't be initializing your collection of books all to Tolkien's work. Instead you might have:
    Code (csharp):
    1. books.Add(new author("J.R.","Tolkien",60));
    2. books.Add(new author("Agatha","Christie",42));
    3. books.Add(new author("Douglas","Adams",42));
    4. books.Add(new author("Jane","Austen",42));
    5. books.Add(new author("Jules","Verne",42));

    EDIT: read through tonyd's introduction to scripting, he touches on arrays, as well as other things you might find useful: http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)
     
  11. diabloroxx

    diabloroxx

    Joined:
    Jan 20, 2010
    Posts:
    71
    You need to make the newBook as an Array in order to access it. Learn what arrays are in Javascript (google for it, http://www.hunlock.com/blogs/Mastering_Javascript_Arrays )
    Once you make it as an Array, you can access them as
    newBook = new author("xyz","sada",50);

    so on...
     
    Last edited: Dec 6, 2010
  12. raulcorrales

    raulcorrales

    Joined:
    Dec 6, 2010
    Posts:
    18
    FizixMan thank you very much for the code and for your inconvenience. works!

    Now the problem is here:

    / / Change all names:
    for (var i: int = 0; i <books.Count; i + +
    {
    book . name = "raulcorrales"
    }



    gives me this error code because the variable "i" already exists.
     
  13. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    It might be because you're missing a ) on the "for" line?

    for (var i: int = 0; i <books.Count; i + +
    to
    for (var i: int = 0; i <books.Count; i++)

    EDIT: it might also be because you declared "i" earlier on, like this:

    var i : int = 9001;

    for (var i : int = 0; i < books.Count; i++)
     
  14. raulcorrales

    raulcorrales

    Joined:
    Dec 6, 2010
    Posts:
    18
    Thank you for all!! :)