Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by castor · Nov 20, 2012 at 10:52 PM · arrayclasscustomadd

Add custom declared variables in a Array

So I made a custom class and added several variables to it. My goals is to add all these new variables into a array called 'allDialogs'

 class DialogBalloon {
     var id : int;
     var nextId = new Array();        //next dialog line to go to
     var actor : Transform;
     var listener : Transform;
     var title : String;
     var text : String;
     
     var isPlayerQuestion : boolean;        //If its a Question that the player can ask(needs to be unlocked to show)
     var isStatement : boolean;  //just a statement with no answer.
     
     var isUnlocked : boolean;  //once unlocked the player can use it as option.
     var wasDisplayed : boolean;    //if it has already been said    
 }
 
 var currentDialogDisplay : DialogBalloon;
 
 var welcomeHome00 : DialogBalloon = new DialogBalloon();
 var welcomeHome01 : DialogBalloon = new DialogBalloon();
 var welcomeHome02 : DialogBalloon = new DialogBalloon();
 var welcomeHome03 : DialogBalloon = new DialogBalloon();
 var traffic01 : DialogBalloon = new DialogBalloon();
 var    traffic02 : DialogBalloon = new DialogBalloon();    
 var    dinner01 : DialogBalloon = new DialogBalloon();
 var    dinner02 : DialogBalloon = new DialogBalloon();    
 var    herDayA01 : DialogBalloon = new DialogBalloon();
 var    herDayA02 : DialogBalloon = new DialogBalloon();
 var    herDayB01 : DialogBalloon = new DialogBalloon();
 var    herDayB02 : DialogBalloon = new DialogBalloon();    
 var    phoneA01 : DialogBalloon = new DialogBalloon();
 var    phoneA02 : DialogBalloon = new DialogBalloon();
 var    phoneB01 : DialogBalloon = new DialogBalloon();
 var    phoneB02 : DialogBalloon = new DialogBalloon();
 
 var allDialogs = new Array();    //Array that contains all the game dialogs


So under Start() I tried to add these to the Array.I found that if I do the long way it works well:

     allDialogs.Add(welcomeHome00);
     allDialogs.Add(welcomeHome01);
     allDialogs.Add(welcomeHome02);
     allDialogs.Add(dinner01);
     allDialogs.Add(traffic01);

But I would like to get something that is automatic, this is what I tried to write but it just doesn't work...

     for (var DialogBalloon in Dialog_Manager){
         allDialogs.Add(DialogBalloon);
     }

Is it possible to build a loop that will include all these new variables? Hope the question was formulated in a clear way.

Thanks in advance!

//SMALL UPDATE// I forgot to add, all these are being written in a script called 'Dialog_Manager', that is the reason why I added it to the for loop.

And also all these variables have actual values that are declared in the Start() function, here are some of them:

 //WelcomeHome
         welcomeHome00.id = 1;
         welcomeHome00.nextId.Add(welcomeHome01);
         welcomeHome00.nextId.Add(welcomeHome02);
         welcomeHome00.nextId.Add(welcomeHome03);
         welcomeHome00.actor = objManager.actorWife;
         welcomeHome00.listener = objManager.actorPlayer;
         welcomeHome00.text = "How did your day go?";
         
         welcomeHome01.id = 2;
         welcomeHome01.title = "Ok";
         welcomeHome01.actor = objManager.actorPlayer;
         welcomeHome01.listener = objManager.actorWife;
         welcomeHome01.text = "It was ok.";
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Nov 20, 2012 at 11:01 PM 0
Share

Do you need to have the vars like herDayA01 and phoneB02 around? or are those just temporary?

avatar image castor · Nov 20, 2012 at 11:11 PM 0
Share

you mean the way they were named? They can be called differently, that was just a temporary layout.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Nov 20, 2012 at 11:17 PM

Well, you have 16 of these things, so you could just do:

 for (var i = 0; i < 16; i++)
   allDialogs.Add (new DialogBalloon());

But you would have to have some constants or something defined to help you know which of those are which, like

allDialogs[Phone02]

Which may be tedious. You could do that with an enum. Probably the most efficient as code goes.

You could also use a HashTable:

 var allDialogs : HashTable;
 
 allDialogs = new HashTable();
 
 allDialogs["phone02"] = new DialogBalloon();

That would give you a list you can iterate on, as well as keys you can human-read.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image castor · Nov 21, 2012 at 01:05 AM 0
Share

Thanks for the suggestion, going to try this at home this evening! As for the Hashtable, I was actually using a GenericDictionary but realized that for this specific case I don't need it yet (but have a strong feeling I'll need to re-implement it later)

avatar image castor · Nov 21, 2012 at 01:16 AM 0
Share

Just realized, the line allDialogs.Add (new DialogBalloon()); wouldnt it just create 16 new emtpy DialogBalloon classes? each one of those variables that I created actually have their own specific values so they are the ones that need to be added. I'm adding the details on the question to make it more clear.

avatar image DaveA · Nov 21, 2012 at 01:50 AM 0
Share

Right. So in this case, yeah, it's a big mess, but you may just need to grunt it out. If it will change a lot, you may consider reading that data from a file. I suppose if there's any wrist-saving, you could do something like:

allDialogs = new Array (welcomeHome00, welcomeHome01, welcomHome02,....

avatar image castor · Nov 21, 2012 at 01:54 AM 0
Share

I see. So there is no clean way I could do a for loop in the start() and just pick up all those variables?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Class Array Serialization Problem 0 Answers

C# Adding to an Array 1 Answer

"Null Reference Error" when using a custom class as an array 1 Answer

Error message when accessing class array 1 Answer

class array removeAt 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges