Scripting
This page is mainly a place holder for me to store useful links and code.
// get rid of the UDK hud, create new class
class MYHUD extends UTHUD;
function DrawHUD()
{
}
DefaultProperties
{
}
Set game info class to use that hud:
HUDType=MyGame.MYHUD
//////////////////////////////////////////////////////////////////////////////////////////////
state PlayerSpidering
{
ignores SeePlayer, HearNoise, Bump;
event bool NotifyHitWall(vector HitNormal, actor HitActor)
{
Pawn.SetPhysics(PHYS_Spider);
Pawn.SetBase(HitActor, HitNormal);
return true;
}
// if spider mode, update rotation based on floor
function UpdateRotation(float DeltaTime)
{
local rotator ViewRotation;
local vector MyFloor, CrossDir, FwdDir, OldFwdDir, OldX, RealFloor;
if ( (Pawn.Base == None) || (Pawn.Floor == vect(0,0,0)) )
{
MyFloor = vect(0,0,1);
}
else
{
MyFloor = Pawn.Floor;
}
if ( MyFloor != OldFloor )
{
// smoothly transition between floors
RealFloor = MyFloor;
MyFloor = Normal(6*DeltaTime * MyFloor + (1 - 6*DeltaTime) * OldFloor);
if ( (RealFloor dot MyFloor) > 0.999 )
{
MyFloor = RealFloor;
}
else
{
// translate view direction
CrossDir = Normal(RealFloor Cross OldFloor);
FwdDir = CrossDir cross MyFloor;
OldFwdDir = CrossDir cross OldFloor;
ViewX = MyFloor * (OldFloor dot ViewX) + CrossDir * (CrossDir dot ViewX) + FwdDir * (OldFwdDir dot ViewX);
ViewX = Normal(ViewX);
ViewZ = MyFloor * (OldFloor dot ViewZ) + CrossDir * (CrossDir dot ViewZ) + FwdDir * (OldFwdDir dot ViewZ);
ViewZ = Normal(ViewZ);
OldFloor = MyFloor;
ViewY = Normal(MyFloor cross ViewX);
Pawn.mesh.SetRotation(OrthoRotation(ViewX,ViewY,ViewZ));
}
}
if ( (PlayerInput.aTurn != 0) || (PlayerInput.aLookUp != 0) )
{
// adjust Yaw based on aTurn
if ( PlayerInput.aTurn != 0 )
{
ViewX = Normal(ViewX + 2 * ViewY * Sin(0.0005*DeltaTime*PlayerInput.aTurn));
}
// adjust Pitch based on aLookUp
if ( PlayerInput.aLookUp != 0 )
{
OldX = ViewX;
ViewX = Normal(ViewX + 2 * ViewZ * Sin(0.0005*DeltaTime*PlayerInput.aLookUp));
ViewZ = Normal(ViewX Cross ViewY);
// bound max pitch
if ( (ViewZ dot MyFloor) < 0.707 )
{
OldX = Normal(OldX - MyFloor * (MyFloor Dot OldX));
if ( (ViewX Dot MyFloor) > 0)
{
ViewX = Normal(OldX + MyFloor);
}
else
{
ViewX = Normal(OldX - MyFloor);
}
ViewZ = Normal(ViewX cross ViewY);
}
}
// calculate new Y axis
ViewY = Normal(MyFloor cross ViewX);
}
ViewRotation = OrthoRotation(ViewX,ViewY,ViewZ);
SetRotation(ViewRotation);
Pawn.FaceRotation(ViewRotation, deltaTime );
//SET PAWN ROTATION WITH RESPECT TO FLOOR NORMALS HERE
//Pawn.mesh.SkeletalMesh.Rotation = Pawn.Rotation;
}
function bool NotifyLanded(vector HitNormal, Actor FloorActor)
{
Pawn.SetPhysics(PHYS_Spider);
return bUpdating;
}
event NotifyPhysicsVolumeChange( PhysicsVolume NewVolume )
{
if ( NewVolume.bWaterVolume )
{
GotoState(Pawn.WaterMovementState);
}
}
function ProcessMove(float DeltaTime, vector NewAccel, eDoubleClickDir DoubleClickMove, rotator DeltaRot)
{
if ( Pawn.Acceleration != NewAccel )
{
Pawn.Acceleration = NewAccel;
}
if ( bPressedJump )
{
Pawn.DoJump(bUpdating);
}
}
function PlayerMove( float DeltaTime )
{
local vector NewAccel;
local eDoubleClickDir DoubleClickMove;
local rotator OldRotation, ViewRotation;
local bool bSaveJump;
GroundPitch = 0;
ViewRotation = Rotation;
//Pawn.CheckBob(DeltaTime,vect(0,0,0));
// Update rotation.
SetRotation(ViewRotation);
OldRotation = Rotation;
UpdateRotation(DeltaTime);
// Update acceleration.
NewAccel = PlayerInput.aForward*Normal(ViewX - OldFloor * (OldFloor Dot ViewX)) + PlayerInput.aStrafe*ViewY;
if ( VSize(NewAccel) < 1.0 )
{
NewAccel = vect(0,0,0);
}
if ( bPressedJump && Pawn.CannotJumpNow() )
{
bSaveJump = true;
bPressedJump = false;
}
else
bSaveJump = false;
ProcessMove(DeltaTime, NewAccel, DoubleClickMove, OldRotation - Rotation);
bPressedJump = bSaveJump;
}
event BeginState(Name PreviousStateName)
{
//if ( Pawn.Mesh == None )
// Pawn.SetMesh();
OldFloor = vect(0,0,1);
GetAxes(Rotation,ViewX,ViewY,ViewZ);
DoubleClickDir = DCLICK_None;
Pawn.ShouldCrouch(false);
bPressedJump = false;
if (Pawn.Physics != PHYS_Falling)
{
Pawn.SetPhysics(PHYS_Spider);
}
GroundPitch = 0;
Pawn.bCrawler = true;
}
event EndState(Name NextStateName)
{
GroundPitch = 0;
if ( Pawn != None )
{
Pawn.ShouldCrouch(false);
Pawn.bCrawler = Pawn.default.bCrawler;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
Input
Mouse Position
//Position x,y can be accessed from almost anywhere trough.
class'UIRoot'.static.GetCursorPosition( x, y );
Mouse Click
//A fast way to know if a mouse button is clicked when prototyping
//without changing to much code is to overload the following functions
//in your own PlayerController Class
exec function StartFire(optional byte FireModeNum)
exec function StartAltFire(optional byte FireModeNum)
//Better example needed
UIInteraction
Retriving
//From any extension of PlayerController you can use
GetUIController();
//Global access point
class'UIRoot'.static.GetCurrentUIController();
LocalPlayer
Retriving
//Trough UIInteraction, if you named your UIInteraction var to UIController
UIController.GetLocalPlayer(0); //Shouldn't be more then one local player in a ordiary game.
Useful Members
//The Players GameViewportClient
ViewportClient
Mouse Position To World Position
DeProjection
//There is two classes that will allow you to DeProject the mouse to the world.
//Trough the Canvas in the PostRender(Canvas Canvas) of a Hud class
Canvas.DeProject(out HitLocation, out HitNormal ...
//Or trough a LocalPlayer, the comment says that this is slower then using the canvas.
//Important remember to divide the mouse position by the size of the viewport to get a value
//between 0-1 before doing the DePoject.[/I]
LocalPlayer.DeProject(out HitLocation, out HitNormal ...
UISceneObject //like UIScene and buttons
Useful functions
//A good point to set your own start up values
other then defautlProperties
event Initialized()
// Or, depending if you want to set the values before or after the current objects children
event PostInitialize()
UIScene
Is also
UISceneObject
Useful Members
//The Viewports size, necessary for DeProject
CurrentViewportSize
//The Scenes LocalPlayer Owner
PlayerOwner
//Get the UISceneClient
Useful Functions
//Get The WorldInfo instance
GetWorldInfo();
UISceneClient
Useful Functions
//Open UIScene
OpenScene(UIScene scene ...
Actor
Useful functions
//Getting hold of other actors in the scene based on criteria
//For detailed input values look at Engine/Actor.uc line 1898-1939
AllActors(...
DynamicActors(...
ChildActors(...
BasedActors(...
TouchingActors(...
TraceActors(...
VisibleActors (...
VisibleCollidingActors(...
CollidingActors(...
//Get The Local Player Controller in a single player game, or random
//local in a splitscreen game
GetALocalPlayerController();
//Function called before game starts
event PreBeginPlay()
//Main Update function
event Tick(float DeltaTime)
Pawn
Is also
Actor
Useful Members
The current Pawn Controller
Controller
Get the current Pawns Weapon
Weapon
Pawns Inventory Manager
InvManager
PlayerController
Useful Functions
//Before the game start
simulated event PostBeginPlay()
//Player Update, only happens client side and if the Controller got a PlayerInput Object.
event PlayerTick( float DeltaTime )
//Make the current Controller take controll of a pawn
event Possess(Pawn aPawn, bool bVehicleTransition)
//Access to the hud and canvas
function DrawHUD( HUD H )
//If the pawn Died
function PawnDied(Pawn P)
[I]//Camera Rotation function
function UpdateRotation( float DeltaTime )
Useful Members
//The current controllers PlayerInput instans.
PlayerInput
//The current PlayerCamera
PlayerCamera
//myHUD is the default HUD reference, it can be converted to your current HUD class.
//Say you have a HUD class named NinjaHUD you could access it trough
//local NinjaHUD MyNinjaHUD = NinjaHud(myHUD);
myHUD
InventoryManager
Useful Functions
//iterator for all inventory items (called inventory, a inventory is resposible for droping
pickup and handeling a carried object)
InventoryActors( class<Inventory> BaseClass, out Inventory Inv )
GameInfo
Useful Functions
//Rating function to determine best start position for spawn, good place to override, if for instance dying
//would require the player to go to jail, or to value spawnpoints close to team mates higher, or based on
//claimed checkpoints
RatePlayerStart(PlayerStart P, byte Team, Controller Player)
//If you need to do magic before tha game begins, setting own var values
event PreBeginPlay()
//Or after the game started, setting values based on what other objects exist at the moment
event PostBeginPlay()
WorldInfo
Is also
Actor
Retrieving
//Get the WorldInfo from anywhere.
'WorldInfo'.static.GetWorldInfo()
Taken from:
http://forums.epicgames.com/showthread.php?t=709479
//////////////////////////////////////////////////////////////////////////////////////////////
class MyHUD extends GameHUD config(Game);
Then:
function vector2D GetMouseCoordinates()
{
local Vector2D mousePos;
local UIInteraction UIController;
local GameUISceneClient GameSceneClient;
UIController = PlayerOwner.GetUIController();
if ( UIController != None)
{
GameSceneClient = UIController.SceneClient;
if ( GameSceneClient != None )
{
mousePos.X = GameSceneClient.MousePosition.X;
mousePos.Y = GameSceneClient.MousePosition.Y;
}
}
return mousePos;
}
//////////////////////////////////////////////////////////////////////////////////////////////
Useful links
http://udn.epicgames.com/Three/UDKProgrammingHome.html
http://code.google.com/p/steam-punk-pirates-mod/wiki/TutorialPage
http://forums.epicgames.com/showthread.php?t=598656
http://svn.assembla.com/svn/white/Tags/0.2%20Diana/Src/WhitePlayer/Classes/WPawn.uc
http://www.etc.cmu.edu/projects/coyote210/Docs/undox/Engine.Pawn.html
No comments yet.