Announcement

Collapse
No announcement yet.

Generic find function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Generic find function

    Guys, is there a way in uscript to write generic find function, like:

    function /*result (generic)*/ findNearest(/*generic class here, once it can be weapon pickup, once it could be health pickup, once it could be something else*/)

    Thank you.

    in C++ this would look something like:

    template<class T>
    T findNearest(T toFind)
    {
    //look for T
    return T;
    }

    #2
    There's a foreach loop in UnrealScript that can be used with the AllActors() (this function only available if your class extends Actor) function to loop through all actors with the same class as the one passed to the function in the game world. Combine that with logic to find the nearest one and you can create a FindNearest() function of your own.

    Comment


      #3
      There are a lot of iterators that you can use to loop through specific types of Actors. Here is the UDN link that displays them: http://udn.epicgames.com/Three/Unrea...Iterators.html

      You can also use Dynamic Arrays to store things like health / weapon pickups which have a .Find( ) but it's more for finding the specific instance of a class in the array.

      Something like this can be done:

      Code:
      function Actor GetNearestActor( )
      {
      	local Actor A, Closest;
      
      	foreach WorldInfo.AllActors( class'Actor', A )
      	{
      		if ( Closest != none )
      		{
      			if ( VSize( A.Location - Pawn.Location ) < VSize( Closest.Location - Pawn.Location ) )
      			{
      				Closest = A;
      			}				
      		}
      		else
      		{
      			Closest = A;
      		}
      	}
      
      	return Closest;
      }

      Comment


        #4
        Guys, thank you for your replies, but I need to specify at compile time the type of the actor and GetNearestActor, for example would find all objects which extend Actor class, and that is not what I need. In my code I need to be able to do something like:

        findNearest(Health);
        findNearest(Ammo);
        etc.

        You getting my gist, don't ya?

        Comment


          #5
          Change Actor to the class you are looking for.

          If your health or ammo class extends Actor (which I can assume it does), you can still use:

          Code:
           foreach WorldInfo.AllActors( class'MY_HEALTH_OR_AMMO_CLASS', A )
          {
          
          }

          Comment


            #6
            @Vawx
            How would I pass that argument to that iterator? That is, how can I have that construct in my code (function call);

            findNearest(Health);

            findNearest(Ammo);
            How the definition of that function (findNearest) looks like? That is, how to make it so it is generic and able to find any type of actor and I have to write just one definition of that function?

            Comment


              #7
              Working from memory, I don't guarantee this at all


              Code:
              function Actor FindNearest ( class<Actor> TypeFilter )
              {
                  local Actor nearestActor = None;
                  local float NearestDistance = MaxFloat;
                  local Actor A;
                  local float d;
              
                  foreach WorldInfo.AllDynamicActors( TypeFilter, A )
                  {
                      d = VSize( A.location - location );
                      if( d < nearestDistance )
                      {
                          NearestActor = A;
                          NearestDistance = d;
                      }
                  }
              
                  return NearestActor;
              }
              
              function Test ()
              {
                  local Actor nearest = FindNearest( class'HealthKit' );
              }

              Comment


                #8
                Wow didn't see one of your posts for a long time spoof hehe nice.

                Comment


                  #9
                  Spoof, you are the best! Thanks!

                  Comment

                  Working...
                  X