Author Topic: Creating A Unit Handle For New Buildings  (Read 1612 times)

Offline Flashy

  • Sr. Member
  • ****
  • Posts: 392
Creating A Unit Handle For New Buildings
« 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?
Praise the mighty light towers!!!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3208
Creating A Unit Handle For New Buildings
« Reply #1 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;

      }

   }

}
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Flashy

  • Sr. Member
  • ****
  • Posts: 392
Creating A Unit Handle For New Buildings
« Reply #2 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.
Praise the mighty light towers!!!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3208
Creating A Unit Handle For New Buildings
« Reply #3 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();
   }

}
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Flashy

  • Sr. Member
  • ****
  • Posts: 392
Creating A Unit Handle For New Buildings
« Reply #4 on: April 29, 2010, 02:30:06 PM »
Isn't there the risk of giving a new VF1 the unit handle VF2?
Praise the mighty light towers!!!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3208
Creating A Unit Handle For New Buildings
« Reply #5 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.
« Last Edit: April 29, 2010, 02:42:00 PM by Sirbomber »
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Flashy

  • Sr. Member
  • ****
  • Posts: 392
Creating A Unit Handle For New Buildings
« Reply #6 on: April 30, 2010, 07:18:07 AM »
Ok.
Praise the mighty light towers!!!