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.

  4. April 9th, 2011 at 21:03 | #4

    Well I just got to the end and I’m going to say very very nice tutorial! I have learnt a heck of alot and will be coming back time and time again to read through it. Managed to compile it all with no errors to speak of. The only problem I have to sort now is getting the cursor to show itself so I can actually move around properly (As I said in the comments of Chapter 2 – Getting a real mouse cursor and a video in the comments of Chapter 3 – Making the pawn move to our will, after doing the compile), and getting the followers to show up. Everything in the log shows fine, so a bit baffled why they don’t show. But anyway, carn’t really thank you enough for this, its given me something to play around with :) .

  5. Roychr
    April 10th, 2011 at 17:25 | #5

    Hey no problem, this tutorial was set up based on my experiences fidling with UDK, I am now getting back to it since I bought a GTablet recently for Unity development and UDK said they would make android builds available : http://droidgamers.com/index.php/component/content/article/1474-epic-does-a-ninja-switch-move-says-udk-will-eventually-come-to-android.

    I now feel anyway that the BETA tag to UDK will stick for a while (been for 2 years now), so now is as good a time then any to upgrade the tutorial. The website also needs a beauty lift ;)

  6. April 11th, 2011 at 12:50 | #6

    @Roychr
    I look forward to it! :)
    Let me know if you need anything made in photoshop, give and take ya know!

  1. February 23rd, 2010 at 20:52 | #1
You must be logged in to post a comment.