Author Topic: Problem with for loop  (Read 407 times)

Legacy_Ed Venture

  • Full Member
  • ***
  • Posts: 200
  • Karma: +0/-0
Problem with for loop
« on: September 30, 2013, 02:21:48 am »


               The for loop below was used without problems before.
It is intended to have the boy running around in the background.
it was originaly put on enter of a trigger where PC enters area by way of JumpToLocation. This script also had another npc walking about. The result was that the npc moved as expected, but the boy would only do the loop once.
I removed the loop to a seperate script and had the trigger script execute it. Same result.
I then decided to isolate it and put it on the on used of a lever. Same result. This told me it's the script.
I removed the lever and changed the ++ to +1. Freezes the game.
Question; it woked before, why not now.


void main()
{
 object oPC = GetFirstPC();
 object oBoy = GetObjectByTag("Michan");
 object oWayb = GetWaypointByTag("WpKidPlay01");
 object oWayc = GetWaypointByTag("WpKidPlay02");
 object oWayd = GetWaypointByTag("WpKidPlay03");
 int nLoop;
 
 for (nLoop = 1; nLoop < 5; nLoop ++)
 {
   AssignCommand(oBoy,ActionMoveToObject(oWayd,TRUE));
   DelayCommand(6.0,AssignCommand(oBoy,ActionMoveToObject(oWayc,TRUE)));
   DelayCommand(14.0,AssignCommand(oBoy,ActionMoveToObject(oWayb,TRUE)));
 }
}
Ed
               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Problem with for loop
« Reply #1 on: September 30, 2013, 02:41:47 am »


               The loop is working, but not as you expect. After the script finishes, the NPC will try three times to move to oWayd. After six seconds, he will be given three more commands to move to oWayc. After 14 seconds, he will be given three commands to move to oWayb.

The correct way to do this depends on whether you want him to pause at each waypoint or to continue the circuit uninterrupted.

To do the loop uninterrupted:

void main()
{
        object oBoy  = GetObjectByTag("Michan");
        object oWayb = GetWaypointByTag("WpKidPlay01");
        object oWayc = GetWaypointByTag("WpKidPlay02");
        object oWayd = GetWaypointByTag("WpKidPlay03");
        int nLoop;

        for (nLoop = 1; nLoop < 5; nLoop ++)
        {
                AssignCommand(oBoy, ActionMoveToObject(oWayd, TRUE));
                AssignCommand(oBoy, ActionMoveToObject(oWayc, TRUE));
                AssignCommand(oBoy, ActionMoveToObject(oWayb, TRUE));
        }
}

To wait at each waypoint:

void main()
{
        object oBoy  = GetObjectByTag("Michan");
        object oWayb = GetWaypointByTag("WpKidPlay01");
        object oWayc = GetWaypointByTag("WpKidPlay02");
        object oWayd = GetWaypointByTag("WpKidPlay03");
        int nLoop;

        for (nLoop = 1; nLoop < 5; nLoop ++)
        {
                AssignCommand(oBoy, ActionMoveToObject(oWayd, TRUE));
                AssignCommand(oBoy, ActionWait(6.0));
                AssignCommand(oBoy, ActionMoveToObject(oWayc, TRUE));
                AssignCommand(oBoy, ActionWait(6.0));
                AssignCommand(oBoy, ActionMoveToObject(oWayb, TRUE));
                AssignCommand(oBoy, ActionWait(6.0));
        }
}

               
               

               


                     Modifié par Squatting Monk, 30 septembre 2013 - 01:48 .
                     
                  


            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Problem with for loop
« Reply #2 on: September 30, 2013, 02:51:05 am »


               The important point here is that objects in NWN have an action queue. When you assign an action to an object (using, for example, the ActionMoveToObject() function), it is added to the object's action queue. The object will then attempt to execute these actions in the order in which they appear in the queue.

For example, if we add actions A, B, and C to the queue, the object will do A, then (when it has finished doing A) move on to B, then (when it has finishes doing 'B)', move on to C.

Your use of DelayCommand() was just changing when the actions were being added to the queue. All of the movements to oWayd were added first, then the movements to oWayc, then the movements to oWayb. Now, you could use DelayCommand() to accomplish what you're trying to do (by increasing the delay as the loop progresses), but it's a little more finicky, and there's not really a point in doing that in this instance.

Instead of doing all that, you can just add the actions to the action queue (as in my example) and let the game handle executing them for you.
               
               

               


                     Modifié par Squatting Monk, 30 septembre 2013 - 01:51 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Problem with for loop
« Reply #3 on: September 30, 2013, 03:25:07 am »


               good catch on the delay command.  there is still nothing in the script though that will cause the boy to do the loop more then once.  

The result was that the npc moved as expected, but the boy would only do the loop once.


Perhaps he is looking for something like this.

void RunLoop()
{
  
  object oWayb = GetWaypointByTag("WpKidPlay01");
  object oWayc = GetWaypointByTag("WpKidPlay02");
  object oWayd = GetWaypointByTag("WpKidPlay03");
  int nLoop;

  for (nLoop = 1; nLoop < 5; nLoop ++)
  {
      ActionMoveToObject(oWayd, TRUE);
      ActionMoveToObject(oWayc, TRUE);
      ActionMoveToObject(oWayb, TRUE);
      ActionDoCommand(RunLoop());
  }
}

void main()
{
  object oBoy = GetObjectByTag("Michan");
 AssignCommand(oBoy  RunLoop());
}
               
               

               


                     Modifié par Lightfoot8, 30 septembre 2013 - 02:31 .
                     
                  


            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Problem with for loop
« Reply #4 on: September 30, 2013, 03:34:58 am »


               Err... why not? Won't it add actions to the queue for each execution of the loop?

Using the ActionDoCommand() version you posted is probably a better way to do it (it allows you to run the waypoint loop forever instead of just four times), but if you go that route, you'd either wanna put it outside of the for loop or just get rid of the for loop entirely.
               
               

               


                     Modifié par Squatting Monk, 30 septembre 2013 - 02:38 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Problem with for loop
« Reply #5 on: September 30, 2013, 03:37:45 am »


               True.   

I guess I miss read it a bit myself
               
               

               


                     Modifié par Lightfoot8, 30 septembre 2013 - 02:38 .
                     
                  


            

Legacy_Ed Venture

  • Full Member
  • ***
  • Posts: 200
  • Karma: +0/-0
Problem with for loop
« Reply #6 on: September 30, 2013, 05:12:24 am »


               Squatting Monk & Lightfoot8,
Just ran the script you gave me and it worked.
When I used the script before, I used it as a spawn. I did not know all that stuff about the action queue. I do now, Thanks to you.
Ed