Interaction

From Tripwire Interactive Wiki
Jump to: navigation, search
Killing Floor class Interaction (source)
Object >> Interaction


Description

Taken from UDN:

Interaction(s) are graphical user components that can accept key input and render two dimensional graphics to the screen (make sure you read CanvasReference on how to render to the Canvas). The interaction system primarily serves two purposes. First, it's designed to lay the foundation for supporting input and render pipelines for multiple viewports and second, to be as friendly as possible to existing systems.

Currently, it's used extensively for ingame menus. The system is very flexible and powerful, containing functions to transform 3d vectors into 2d vectors (from the world to the screen) and vice versa. It also provides the functionality and flexibility to be applied on an individual basis to players to provide support for real-time targeting systems (see the example below). This document will focus mainly on basic uses of Interactions with respect to the player.

Interactions can be used as interfaces between the player and the game, capturing keyboard input and displaying graphics.

Adding an interaction

Adding an interaction can be accomplished in several ways, though the easiest is via a mutator:

// Simple mutator to add an interaction
class MyMutator extends Mutator;

simulated function Tick(float Delta)
{
	local PlayerController PC;

	// Check if the local PlayerController is available yet
	PC = Level.GetLocalPlayerController();
	if (PC == none)
		return;
	
	// Add the interaction
	PC.Player.InteractionMaster.AddInteraction("MyPackage.MyInteraction", PC.Player);
	
	// Destroy mutator now that we are done with it
	Destroy();
}

defaultproperties
{
	GroupName="KFMyMutator"
	FriendlyName="My Mutator"
	Description="..."
}

It can also effectively be accomplished using a custom PlayerController, HUD, or GameRules class.

Displaying graphics

Interactions that display graphics need to have bVisible=true set in the defaultproperties otherwise their rendering functions will not be called.

When the game is ready to let the interaction draw to the canvas (screen), it will call PostRender():

class MyInteraction extends Interaction;

function PostRender(Canvas C)
{
	C.SetDrawColor(255, 255, 255);
	C.SetPos(C.SizeX * 0.5, C.SizeY * 0.5);
	C.DrawText("Hello!");
}

defaultproperties
{
	bVisible=true
}

This example will set the draw color to white (rgb 255, 255, 255), set the drawing position to the center of the screen, and will write the text "Hello!".

You can also draw lines and even materials to the screen, like so:

function PostRender(Canvas C)
{
	local float CrosshairSize;
	CrosshairSize = 5.0;
	
	// Draw crosshair
	C.SetDrawColor(255, 255, 255);
	C.SetPos(C.SizeX * 0.5 - CrosshairSize, C.SizeY * 0.5 - CrosshairSize);
	C.DrawBox(C, CrosshairSize * 2, CrosshairSize* 2);
	
	// Draw a texture
	C.SetPos(0, 100);
	C.DrawRect(Texture'Engine.S_Pickup', 100, 100);
	
	// Draw player's skin
	C.SetPos(0, 0);
	if (ViewportOwner.Actor.Pawn != none)
		C.DrawTile(ViewportOwner.Actor.Pawn.Skins[0], 100, 100, 0, 0, 
			ViewportOwner.Actor.Pawn.Skins[0].MaterialUSize(),
			ViewportOwner.Actor.Pawn.Skins[0].MaterialVSize());
}

For more information, see Canvas.

Getting user input

Interactions that accept user input need to have bActive=true set in the defaultproperties otherwise their input functions will not be called.

When the game receives input from the user it will call KeyEvent():

class MyInteraction extends Interaction;

function bool KeyEvent(EInputKey Key, EInputAction Action, FLOAT Delta )
{
	if (Action == IST_Press)
		ViewportOwner.Actor.ClientMessage("Key PRESSED:" @ GetFriendlyName(Key));
	else if (Action == IST_Release)
		ViewportOwner.Actor.ClientMessage("Key RELEASED:" @ GetFriendlyName(Key));
 
	return false;
}

defaultproperties
{
	bActive=true
}

In this example, when a user presses or releases a key it will be indicated on the screen. GetFriendlyName() is used to translate the key code Key to a human-readable name.

Axis (mouse/analog stick) input can also be retrieved by checking for IST_Axis:

function bool KeyEvent(EInputKey Key, EInputAction Action, FLOAT Delta )
{
	if (Action == IST_AXIS)
	{
		if (Key == IK_MouseX)
			ViewportOwner.Actor.ClientMessage("X axis movement:" @ Delta);
		else if (Key == IK_MouseY)
			ViewportOwner.Actor.ClientMessage("Y axis movement:" @ Delta);
	}

	return false;
}

Where Key indicates the axis, and Delta indicates the units of movement.

External Links