Author Topic: GetFirst/Next Area  (Read 252 times)

Legacy_Cursed Eclipse

  • Full Member
  • ***
  • Posts: 132
  • Karma: +0/-0
GetFirst/Next Area
« on: September 28, 2015, 10:27:32 am »


               
I accidentally discovered the function "GetFirstArea / GetNextArea"

into nwnx_cool.

 

I'd like to use this function to teleport an NPC in a random area, but how i should use it?

 

 



void main() 

object oArea = GetFirstArea(); 

    while(GetIsObjectValid(oArea))
     { 
      ???????; //<--- how to pick a random area and teleport the NPC? 
    oArea = GetNextArea(); 
    } 
}


               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
GetFirst/Next Area
« Reply #1 on: September 28, 2015, 02:00:59 pm »


               

if you know about how many areas you are dealing with you can use a counter in the loop and then pick a random number. When the count equals your random number use that area.  If you are willing to go through twice you can count the number of areas first to get an accurate total number of areas.



               
               

               
            

Baaleos

  • Administrator
  • Hero Member
  • *****
  • Posts: 1916
  • Karma: +0/-0
GetFirst/Next Area
« Reply #2 on: October 06, 2015, 03:59:17 pm »


               

What I would recommend with regards to area loops is this:


 


As you loop through the areas for the first time.


Store a reference to the area on the module in association with the areas index in the module.


 


Eg:  (Pseduo-code)


int i = 0;
while (Area == Valid)
{
        SetLocalObject(oModule,"Area_"+IntToString(i),Area);
         i++;
        Area = NextArea();
}
SetLocalInt(oModule,"MAX_AREAS",i);

What this means is that you can then later refer to those areas - without having to do a costly loop.


This becomes important if you end up having 500+ areas - as looping through 500-700 areas just so you can pick one at random, is a costly and wasteful exercise.


 


int iRandom = RandomNumber(0,i);
object RandomArea = GetLocalObject(oModule,"Area_"+IntToString(iRandom));

 


 


What Meaglyn said about looping twice to keep the area list accurate stands true.


If you use plugins such as nwnx_Areas - then the area list you store on the module will become out of date when the new area is added.


It wont affect your ability to interact with those areas (unless they were destroyed), but you just wont be able to access newly added areas unless you do the loop/harvest step again.


 


I recommend doing these costly loops infrequently.


Eg: OnModLoad 


      OnClientExit  (only if there are no characters left in the module)


               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
GetFirst/Next Area
« Reply #3 on: October 06, 2015, 04:21:44 pm »


               

I agree with all that.  I was trying to answer the question based on the OP already having the loop, not describing how to handle lists of areas and such.  Personally, I just put a waypoint tagged with a unique number in each area. Then GetWaypointByTag can be used  (Check for valid object and do it a couple of time if you have holes in the numbering scheme).  That saves you from adding 500+ more variables to the module.  That's something you want to do while building though, although you could script something to fix it up after if needed I suppose.