[UDK]Creating an XP system

Posts 1–5 of 5 · Page 1 of 1
[UDK]Creating an XP system
== Getting Started ==

First of all, make sure you know how to set up your files, because I will not explain that in this tutorial.
See: [Making Tutorial]

Secondly, I expect you have some knowledge of UnrealScript, for example: you know how extending classes works, and you know how to make UDK use your custom gametype and you know how to make your custom gametype use your custom HUD, PlayerController, etc. If you don't know these things, I suggest looking at

My HUD code is based on UDK HUD TUTORIAL

I'd like to mention here the files that we will be creating/editing:
* MyGame.uc
* MyPlayerController.uc
* SeqAct_GiveXP.uc (optional)
* MyHUD.uc
== MyGame.uc ==

This file holds our custom gamemode.
First we declare what class this file holds:
Code:
class MyGame extends UTGame;
Then we call the ScoreKill() function:

Code:
// Function that is executed after each kill
function ScoreKill(Controller Killer, Controller Other)
{
	local MyPlayerController PC;

	super.ScoreKill(Killer, Other);

	// Cast to the custom MyPlayerController class
	PC = MyPlayerController(Killer);
	// Give XP through our custom function to our PlayerController, change 100 to whatever amount you want
	PC.GiveXP(100);
}
And in our DefaultProperties we add the following lines to make sure our custom classes are used in our gamemode:

Code:
DefaultProperties
{
	DefaultPawnClass = class'MyGame.MyPawn'
	PlayerControllerClass = class'MyGame.MyPlayerController'
	HUDType = class'MyGame.MyHUD'
	bUseClassicHud = true //So that our custom HUD is used.
}
== MyPlayerController.uc ==

We use this file to store our XP, level, etc. We use this instead of MyPawn, because we want to keep our XP and level after we die.

First we declare our class, and we declare the constants and variables we are going to use:

Code:
class MyPlayerController extends UTPlayerController;
 
const MAX_LEVEL = 50;
const XP_INCREMENT = 500; // Amount of XP that is added to the amount of XP required for a level, after each level progression
 
var int XP; // Total amount of gathered XP
var int Level; // Current level
var int XPGatheredForNextLevel; // Amount of XP gathered for the next level
var int XPRequiredForNextLevel; // Amount of XP required for the next level
At the start of the game we calculate how much XP we need to reach the next level, through a function we introduce later in this file:

Code:
 simulated function PostBeginPlay()
{
	super.PostBeginPlay();

	// Calculate XP-related properties at the start of the game
	CalculateLevelProgress();
}
Here we declare the function we use when we want to alter the XP through Kismet (this is optional):

Code:
// The function that is called from Kismet
public function AddXP(SeqAct_GiveXP action)
{
	GiveXP(action.Amount); // Give the player the amount of XP specified in the Kismet action
}
Now we declare the function that adds XP:

Code:
public function GiveXP(int amount)
{
	XP += amount;

	CalculateLevelProgress();

	while (XPGatheredForNextLevel >= XPRequiredForNextLevel && Level < MAX_LEVEL)
	{
		Level++;
		
		// Recalculate level progress after leveling up
		CalculateLevelProgress();
	}
}
Next, we declare the function that uses maths to calculate how much XP we need for the next level, and what level we are:

Code:
 private function CalculateLevelProgress()
{
	local int xpToCurrentLevel; // Total amount of XP gathered with current and previous levels
	
	xpToCurrentLevel = 0.5*Level*(Level-1)*XP_INCREMENT;
	XPGatheredForNextLevel = XP - xpToCurrentLevel;
	XPRequiredForNextLevel = Level * XP_INCREMENT;
}
And finally we add our starting XP and Level to our DefaultProperties:


Code:
DefaultProperties
{
	Level = 1;
	XP = 0;
}
== SeqAct_GiveXP.uc (optional) ==

In this file we create a new Sequence Action to be used through Kismet.

First we declare our class, and our variable for the amount of XP:
Code:
class SeqAct_GiveXP extends SequenceAction;
 
var() int Amount; // Amount of XP this action will give
Then we add some info to our DefaultProperties to make it easier to use in Kismet:

Code:
DefaultProperties
{
	// Name that will apear in the Kismet Editor
	ObjName="Add XP"
 
	// Name of the function that will be called when this action is triggered
	HandlerName="AddXP"
 
	Amount = 0
 
	// Expose the Amount property in Kismet
	VariableLinks(1)=(ExpectedType=class'SeqVar_Int', LinkDesc="Amount", bWriteable=true, PropertyName=Amount)
}
== MyHUD.uc ==

This file is used to draw our custom HUD on the screen

Again, first we declare our class:

Code:
class MyHUD extends UTHUD;
Then we create our DrawBar() function:

Code:
function DrawBar(String  Title, float Value, float MaxValue,int X, int Y, int R, int G, int B) // Create our function used to draw bars
{
	local int PosX; //Declare our variable representing our postition on the X-axis
	local int BarSizeX; //Declare our variable representing the size of our bar

	PosX = X; //Where we should draw the rectangle
	BarSizeX = 300 * FMin(Value / MaxValue, 1); // size of active rectangle (change 300 to however big you want your bar to be)
 
	//Displays active rectangle
	Canvas.SetPos(PosX,Y);
	Canvas.SetDrawColor(R, G, B, 200);
	Canvas.DrawRect(BarSizeX, 12);
 
	//Displays empty rectangle
	Canvas.SetPos(BarSizeX+X,Y);
	Canvas.SetDrawColor(255, 255, 255, 80);
	Canvas.DrawRect(300 - BarSizeX, 12); //Change 300 to however big you want your bar to be
 
	//Draw our title
	Canvas.SetPos(PosX+300+5, Y); //Change 300 to however big your bar is
	Canvas.SetDrawColor(R, G, B, 200);
	Canvas.Font = class'Engine'.static.GetSmallFont();
	Canvas.DrawText(Title);
 
}
In our DrawGameHud() function, we use our DrawBar() function to draw our health-, level- and XP-bars:

Code:
function DrawGameHud()
{
	local MyPlayerController PC; 
	PC = MyPlayerController(PlayerOwner);

	//If player is not dead or spectating... ( you could also use DrawLivingHud() )
	if ( !PlayerOwner.IsDead() && !UTPlayerOwner.IsInState('Spectating'))
	{		
		DrawBar("Health:"@playerowner.Pawn.Health$"%",PlayerOwner.Pawn.Health, PlayerOwner.Pawn.HealthMax,20,20,200,80,80); //...draw our health-bar
		DrawBar("Level:"@PC.Level, PC.Level, PC.MAX_LEVEL ,20,40,200,200,200); //...and our level-bar
		if ( PC.Level != PC.MAX_LEVEL ) //If our player hasn't reached the highest level...
		{
			DrawBar("XP:"@PC.XPGatheredForNextLevel$"/"$PC.XPRequiredForNextLevel, PC.XPGatheredForNextLevel, PC.XPRequiredForNextLevel, 20, 60, 80, 255, 80); //...draw our XP-bar
		}
	}
}
In this file, the DefaultProperties is empty:

Code:
DefaultProperties
{
}


== Using Kismet to change XP ==

First, you want to make a trigger, by right-clicking anywhere in the editor and then go to Add Actor > Add Trigger.

Next, select the trigger, and go into Kismet. Right-click anywhere in the Kismet field, and choose New Event Using Trigger_0 > Touch.
Then right-click again in the Kismet field, and under New Action, you should find our ''Add XP''-action. Create it.
Then link the ''Touched'' of our trigger-event to the ''In'' of our Add XP-action.
Then create a New Variable > Player > Player and link that to the ''Target'' of our custom action.
All you have to do now is create a New Variable > Int > Int and link it to the ''Amount'' of our action, and give it a certain value.
If you want, you can change the amount of times you can trigger the event in Kismet.

Now save your map, play, and enjoy!
Looks great man, but boy this looks hard to do
Quote Originally Posted by Terell. View Post
Looks great man, but boy this looks hard to do
No Trust me its very easy. Unity seems complicated on teh first week then gets a lot easier.
Then you must be real talented at this stuff .
Quote Originally Posted by Terell. View Post
Then you must be real talented at this stuff .
I meant to say UDK. Unity is way easier in my opinion. If you need any help contact me VM/PM.
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us