- Home /
Check if List contains specific object values (JS)
I'm trying to optimise my code:
for (var a = 0; a < GhostObjects.Count; a++) {
if (GhostObjects[a].name == "GhostObj Pos_" + Current_Pos && GhostObjects[a].transform.position.x < 1) {
Death();
}
}
As you can see, I'm going through a list and checking to see if there is an object with a name of "GhostObj Pos_34" (If the current pos was 34), and then I'm checking to see if that object has a position of x less than 1.
If both criteria is matched, it will then go ahead and kill the player.
Here lies the problem: as the game progresses, there are more and more Ghost Objects added to the list, and therefore the code becomes slower because it iterates through all of the ghost objects.
Please can you suggest ways I can get around this problem by optimising the code to run at a constant speed whatever the case maybe.
Please note, I have tried List.Contains, but I don't know how I would insert multiple criteria of the same object into the contains.
Thanks!!
How many objects are we talking? And have you used the profiler to see if this is indeed a problem?
Around 45 ghost objects. The profiler points directly to this update sequence. I disabled it and it ran smooth constantly.
Hmmm, 45 is nothing really. How is GhostObjects
being populated? Doing a Find
every frame could be an issue.
Can multiple objects have the same name? If there's just a single ghost at any position, you should be able to find that in the list directly, then just compare x... (this is C#, so convert to JS as needed)
int i = GhostObjects.IndexOf("GhostObj Pos_" + Current_Pos);
if (i >= 0)
{
if (GhostObjects[i].transform.position.x < 1)
{
Death();
}
}
An arguably better option is to set up triggers on your objects and let Unity notify you of collisions.
Only problem is that since it's a list of objects, GhostObjects.IndexOf("GhostObj Pos_" + Current_Pos);
should be searching for gameobjects with that name rather than that specific string.
I'm unsure how to convert the list of objects to a list of names of the objects (strings) using the least amount of performance (i.e. Avoid creating another list).
I've been trying things like:
print (GhostObjects.Find(obj => string.Equals(obj.name, "GhostObj Pos_" + Current_Pos ));
..or..
print (GhostObjects.Find(obj=>obj.name=="GhostObj Pos_" + Current_Pos));
and it tells me that obj is an unexpected token
Answer by fuego_see_money · Jul 24, 2015 at 11:46 PM
You should just make a script, called GhostDeath
or something, and attach it to every Ghost object. Much simpler, and doesn't require a for loop.
Create a empty game object with a bounding box where you want the Ghosts to delete, and then tag it "`DeathBox`". Then, just have each Ghost check for collisions with it:
void OnCollisionEnter(Collision c)
{
if(c.gameObject.tag == "DeathBox")
{
Die();
}
}
It's also more object oriented (the objects are checking themselves for death).
O$$anonymous$$G I suck. Im sorry, I forgot this was a Javascript question...
Haha, no that's fine. I'll see if it works out (I've been avoiding collision/trigger detection because all movement is coded via rotation)..
Remodified all my scripts, and now I'm using collision detection. Works like a charm.
Your answer
Follow this Question
Related Questions
Java Lists 1 Answer
A node in a childnode? 1 Answer
Javascript Enum Write List 0 Answers
How long should it take me to make my game 2 Answers
JS Argument out of range 1 Answer