Outpost Universe Forums

Projects & Development => Outpost 2 Programming & Development => Topic started by: Flashy on April 29, 2010, 08:50:47 AM

Title: Creating A Unit Handle For New Buildings
Post by: Flashy on April 29, 2010, 08:50:47 AM
You know my problem. The new (rebuild) vehicle factory doesn't work. I found some code here in the forum, but I can't get it to work. Does anyone know how to solve that problem?
Title: Creating A Unit Handle For New Buildings
Post by: Sirbomber on April 29, 2010, 09:17:02 AM
Use an enumerator.  In this case, a PlayerBuildingEnum:
Code: [Select]
// Global Variables
Unit VF1, VF2;
...

// Untested, but should work
SCRIPT_API void CheckForVF2()
{
   // Find the new VF.  Assume VF1 already exists.
   Unit curVF;
   PlayerBuildingEnum vfEnum([AI Player #], mapVehicleFactory);

   // Search through all VFs owned by this player until we find the new one
   while (vfEnum.GetNext(curVF) )
   {
      // Make sure the VF we're looking at isn't VF1
      if (curVF != VF1)
      {
         // Assign this VF with our special handle
         VF2 = curVF;

         // We don't need to run that loop anymore.  Let's get out of it.
         break;

      }

   }

}
Title: Creating A Unit Handle For New Buildings
Post by: Flashy on April 29, 2010, 11:03:19 AM
I meant something different. After the vehicle factory is destroyed and rebuild, it doesn't work. But I found this code somewhere:

Code: [Select]
//Find the vecfac if it was rebuilt
    if(vefac.IsLive())//If the handel is dead, then we need to find the new one
    {
     IUnit Vehfac;
     InRectEnumerator Rectenum(MAP_RECT(53+31, 191-1, 58+31, 195-1));
     //Search the area near the vecfac for a new one
        if(Rectenum.GetNext(Vehfac) == 1)
        {
            if(Vehfac.GetType() == mapVehicleFactory)
            {
             switch(Vehfac.GetBusy())
                {
                    case ctMoDevelop:
                    case ctMoUnDevelop:
                    case ctMoDismantle:
                        break;
                    default://If it's not being built, or destroyed, take it
                        vefac = Vehfac;
                        reinforceGrp.TakeUnit(vefac);
                        break;
                }
            }
        }
    }
And I think it works now...
But thanks.
Title: Creating A Unit Handle For New Buildings
Post by: Sirbomber on April 29, 2010, 01:18:57 PM
Quote
I meant something different. After the vehicle factory is destroyed and rebuild, it doesn't work.
Uhhh, that's what my code did assuming you also did something like this:
Code: [Select]
void AIProc()
{
   if (!VF2.IsLive() )
   {
      CheckForVF2();
   }

}
Title: Creating A Unit Handle For New Buildings
Post by: Flashy on April 29, 2010, 02:30:06 PM
Isn't there the risk of giving a new VF1 the unit handle VF2?
Title: Creating A Unit Handle For New Buildings
Post by: Sirbomber on April 29, 2010, 02:41:22 PM
Quote
Isn't there the risk of giving a new VF1 the unit handle VF2?
Code: [Select]
// Make sure the VF we're looking at isn't VF1
      if (curVF != VF1)

Granted, if both VFs are destroyed and then rebuilt, then yeah, what was VF1 may become VF2 and vice-versa, but I really don't see that being a problem since most of the time VFs are built right next to each other anyways.

Edit: Not to mention that code assumes you have more than one VF.  If you've only got the one then no problem.
Title: Creating A Unit Handle For New Buildings
Post by: Flashy on April 30, 2010, 07:18:07 AM
Ok.