chapter 7 – Assign commands to followers

February 23rd, 2010 Leave a comment Go to comments



Squad movement

Now that everything is in place, the final segment in this chapter will be about experimenting with switching the main character for a follower and giving basic orders.

Still in the IsometricGamePlayerController, we will modify an existing function that makes the pawn move on click (simple destination). Copy this function over the existing one (or merge with your code) :

/******************************************************************
 *
 *  TUTORIAL FUNCTION
 *
 *  MovePawnToDestination will push a MoveMouseClick state that will make
 *  the pawn go to a single destination with a mouse click and then
 *  stop near the destination.
 *
 ******************************************************************/
function MovePawnToDestination()
{
	local int i;

	`Log("Moving to location without pathfinding!");
	SetDestinationPosition(MouseHitWorldLocation);
	PushState('MoveMouseClick');

	for(i = 0; i < 3; i++)
	{
		FollowerAIController(Followers[i].Controller).SetOrders('Follow', self);
	}
}

Order assignment

When our main player moves, we iterate through the array of followers issuing them the follow command.

Exploration on your side... some homework

Now the next part is pretty easy, but that is not entirely functional... and I will let you try to fix it (its a little homework for you). We will try to play with simple manual pawn possession. By clicking on a follower we will switch control of the player to this pawn. Now be aware that the main player will not receive order (hint hint).

Replace or merge your current PlayerTick function of the IsometricPlayerControllerClass :

/******************************************************************
 *
 *  TUTORIAL FUNCTION
 *
 *  PlayerTick is called once per frame
 *
 ******************************************************************/
event PlayerTick( float DeltaTime )
{
	Local Controller Cont;
	Local Pawn OldPawn;

	super.PlayerTick(DeltaTime);

	//Set the location of the 3d marker that moves with the mouse.
	MouseCursor.SetLocation(MouseHitWorldLocation);	

	//We use the right mouse button to move, change it to suit your need !
	if(bRightMousePressed)
	{
		//accumulate the time for knowing how much time the button was pressed.
		DeltaTimeAccumulated += DeltaTime;

		//Update destination so that while holding the mouse down the destination changes
		//with the mouse move.
		SetDestinationPosition(MouseHitWorldLocation);

		//If its not already pushed, push the state that makes the pawn run to destination
		//until mouse is unpressed. Make sure we do it after the allocated time for a single
		//click or else two states could be pushed simultaneously
		if(DeltaTimeAccumulated >= 0.13f)
		{
			if(!IsInState('MoveMousePressedAndHold'))
			{
				`Log("Pushed MoveMousePressedAndHold state");
				PushState('MoveMousePressedAndHold');
			}
			else
			{
				//Specify execution of current state, starting from label Begin:, ignoring all events and
				//keeping our current pushed state MoveMousePressedAndHold. To better understand why this
				//continually execute each frame from our Begin: label, see
				//http://udn.epicgames.com/Three/MasteringUnrealScriptStates.html,
				//11.3 - BASIC STATE TRANSITIONS
				GotoState('MoveMousePressedAndHold', 'Begin', false, true);
			}
		}
		else
		{
			switch(TraceActor.class)
			{
				case class'FollowerPawn' :
				case class'MyPawn':
						//Reference our current pawn to make it possessed by AI.
						OldPawn = Pawn;
						//Get AI Controller of target pawn
						Cont = Pawn(TraceActor).Controller;
						//Unpossess our selected player
						UnPossess();
						//Unpossess our target player
						Cont.UnPossess();
						//Possess target player
						Possess(Pawn(TraceActor), false);
						//Old player becomes an AI
						Cont.Possess(OldPawn, false);

						bRightMousePressed = false;
				break;
			}
		}
	}

	//DumpStateStack();
}



Now with a right mouse click on a follower you should be able to switch the pawn of your controller class with possess and UnPossess. Have fun !

 

Get chapter 6-7 source code and test map.

 

 


Update me when site is updated
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • PDF
  • Twitter
  1. June 24th, 2010 at 03:43 | #1

    Good stuff!
    Just blasted through this one today, I cannot even tell you how many more of the core concepts around UDK implementation have just been *finally* been made clear to me(mostly just cuz it was all in the same spot AT LAST lol). Awesome work, I am so ready to really tear into UDK now.

    Thank you!

  2. goldendeer
    August 29th, 2010 at 16:25 | #2

    you code worked fine but three followers sometimes doesn’t follow the pawn without any reason

  3. Roychr
    September 8th, 2010 at 11:36 | #3

    Yeah I found that there are some bugs lurking, I have yet to find why it randomly does fail. Again, this should not be production code and merely be used to toy around and deepen your knowledge of Unreal script.

  1. February 23rd, 2010 at 20:52 | #1