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 thaiscorpion · Nov 12, 2013 at 04:15 PM · javascriptlistgenerics

How to add to a list excluding certain items

Hi, I'm trying to make a system that will give me 5 items from a list but exclude items with the same id so that I don't get any repeats. The system gives me 5 items but I keep getting repeats and I don't know what I'm doing wrong.

Here is the code from my system:


 static function SetAvailableMissions(){

         usedIDs.Clear();
         usedTypes.Clear();
         availableMissions.Clear();

         for (var tempMission:Mission in activeMissions){
             if (tempMission != null){
                 usedIDs.Add(tempMission.ID);
                 usedTypes.Add(tempMission.type);
             }

         }

         for (var tempMission:Mission in missions){            
             
             
             if (usedIDs.Count){
                 for (var i : int = 0; i < usedIDs.Count; i++){

                     if(tempMission.ID == usedIDs[i]){
                         continue;        
                     }
                      

 if(tempMission.type == usedTypes[i]){
                             continue;
                         }

                     availableMissions.Add(tempMission);
                 }
             } else {

                 availableMissions.Add(tempMission);
             }

         }

 }                        
                                                     
                                                                                                             
 static function ReplaceMission(position:int) {

         SetAvailableMissions();
         var tempRandom = Random.Range(0,availableMissions.Count);
         activeMissions[position] = availableMissions[tempRandom];
         activeMissions[position].position = position;

 }
 
 static function CheckActiveMissions() { 
     
     for (var i : int = 0; i < activeMissions.length; i++){

         if (!activeMissions[i]){

             ReplaceMission(i);
         }
     }    
 }




this is the Mission class used:


class Mission {

 var ID            :int;
 var stringID    :String;
 var type        :int;
 var targetValue    :int;
 var position     :int = 0; //Position in the active mission list
 
     function Mission (thisID:int, thisStringID:String, thisType:int, thisTargetValue:int) {
     
         ID             = thisID;
         stringID     = thisStringID;
         type         = thisType;
         targetValue = thisTargetValue;            
         
     }    
 }




This is how the list of missions is inserted:

 MissionManager.AddMission(0,"text",1,10);
 MissionManager.AddMission(1,"text",1,10);
 MissionManager.AddMission(2,"text",2,30);
 MissionManager.AddMission(3,"text",2,40);
 MissionManager.AddMission(4,"text",3,140);
 MissionManager.AddMission(5,"text",3,100);
 MissionManager.AddMission(6,"text",4,300);
 MissionManager.AddMission(7,"text",4,400);
 MissionManager.AddMission(8,"text",5,1400);
 MissionManager.AddMission(9,"text",5,1400);
 MissionManager.Start();

after executing MissionManager.Start();, activeMissions sometimes has Missions with the same ID or type.

Any ideas how to do this correctly?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hoeloe · Nov 12, 2013 at 04:49 PM

If you're trying to avoid repeats, I'd recommend not using lists. There are data structures, such as Dictionaries and Sets, that by definition cannot contain repeats. While Sets are also unordered, Dictionaries have the concept of a "key", which you can use to index into them. I suggest using one of those.

Having said that, I don't see, at a glance, anything that could add multiple missions, but this may be because none of the code you've shown actually does anything to the list of active missions.

Also, you should never call Start manually - Start is called by Unity when the object is created, calling it twice can cause all sorts of errors. If you need something initialised before Start occurs, try putting it in the Awake method (which is called before Start, but doesn't guarantee all scene objects are available yet).

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 thaiscorpion · Nov 12, 2013 at 04:55 PM 0
Share

Hi Hoeloe thanks for your feedback, I will have a look at those data structures.

About the script not doing anything to active$$anonymous$$issions, I'm asigning missions to it on line 45.

I didn't add the whole script to try and keep it as clean as posible :D but the Start function is actually in a class $$anonymous$$ission$$anonymous$$anager is that also an issue?

avatar image Hoeloe · Nov 12, 2013 at 05:09 PM 0
Share

Ah, I see it now. If you're calling that function more than once, there's nothing stopping the random call from picking the same number twice, which may be causing your issue.

Fair enough. If $$anonymous$$ission$$anonymous$$anager inherits $$anonymous$$onoBehaviour, then yes, it's still an issue, because Unity is still calling it. If it doesn't inherit from $$anonymous$$onoBehaviour, then Unity doesn't ever touch it, so then it's fine. If you want a function that initialises things in a class that inherits from $$anonymous$$onoBehaviour, and you don't want it to be called by Unity automatically, call it something other than Start (Initialise, for example).

avatar image thaiscorpion · Nov 12, 2013 at 05:16 PM 0
Share

Oky thanks I'm going to fix that and see if it was the issue.

avatar image thaiscorpion · Nov 12, 2013 at 07:10 PM 0
Share

I tried useing the data structures like you said but I still have the same issue, I need to exclude things from the list so that the ID's and the Type are unique. These only help me keep the ID unique. Also changed the start function issue but it's still the same.

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

17 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JS Argument out of range 1 Answer

Can you define a jagged Generic List in UnityScript? 1 Answer

A node in a childnode? 1 Answer

PrefabHell - How to have scripts of prefabs instances reference the variables of the prefab instance they're attached to? 1 Answer

How can i store the Amount of an item i have in an inventory? 0 Answers


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