- Home /
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?
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).
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?
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).
Oky thanks I'm going to fix that and see if it was the issue.
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
Follow this Question
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
How can i store the Amount of an item i have in an inventory? 0 Answers