Jump to content

[SOLVED] Issue with find() on a vector


photo

Recommended Posts

So, I have a custom class MyClass.  In an event handler I create a vector of these objects like :

 

class EventHandler

{

  MyClass mVector[0];

 

  void addMyClass(MyClass aObject)

  {

    mVector.append(aObject).

  }

 

  int findObject(MyClass aObject)

  {

    int found = 0;

 

    if(mVector.find(aObject, NULL) != NULL)

    {

      found = 1;

    }

 

    return found;

  }

};

 

Adding objects to the vector works with no problems.  I can check this by giving each object a unique ID and printing those out.  The problem is find().  If the MyObject I'm looking for is at index 0 it then Unigine will not find it.  find() works as expected if the object is at any other index, well, so far as I can tell anyhow.

Link to comment

Hey Joshua!

 

First of all, find will return an index, not an object. By passing some value as a second argument you're telling the container to return that value if there was no object. If you're not passed second argument then find will return -1 if it doesn't find anything.

 

Check this article: https://developer.unigine.com/en/docs/1.0/scripting/language/containers/container_functions#container_find_one_arg

 

Also, NULL is basically 0, that's it. :)

 

So in your case what you're doing is "please find my object and if there's no such object then return zero otherwise return its index".

 

Your code could be easily fixed:

int findObject(MyClass aObject) { return mVector.find(aObject) != -1; }
Link to comment
×
×
  • Create New...