Skip to content

UnrealScript Code Snippets

Tajfun403 edited this page Jul 29, 2024 · 1 revision

Overview

This page contains a bunch of code snippets I find myself using often when writing scripts for Mass Effect games. This page is not intended to be beginner-friendly: you should already have some prior programming knowledge in order to make good use of it.

Feel free to add useful code snippets to this page as you wish.

Getting global variables

Getting BioWorldInfo:

local BioWorldInfo WorldRef;

WorldRef = BioWorldInfo(Class'Engine'.static.GetCurrentWorldInfo());

Getting Player:

local SFXPawn_Player Player;

// with existing world reference:
Player = SFXPawn_Player(WorldRef.GetLocalPlayerController().Pawn);

// or directly without a world reference:
Player = SFXPawn_Player(BioWorldInfo(Class'Engine'.static.GetCurrentWorldInfo()).GetLocalPlayerController().Pawn);

Getting last Tick time:

WorldRef.DeltaSeconds

Getting current in-game time:

// Time which does not pause during a pause
WorldRef.TimeSeconds

// Time which pauses ticking when the game is paused. Available only in ME3/LE3
// For ME1/2, manual clocks need to be created.
WorldRef.GameTimeSeconds

Drawing on the interface

How to setup

First, you need to write a function that will be called each Tick (let's call it MyDebugDraw), with a single parameter of type BioHUD:

function MyDebugDraw(BioHUD HUD)
{

}

All drawing can be performed through the passed HUD argument.

Then, you need to subscribe to the game's list of UI painters:

function EnableDebugPanel(bool bEnable)
{
    local BioHUD HUD;
    
    HUD = BioHUD(BioWorldInfo(Class'Engine'.static.GetCurrentWorldInfo()).GetLocalPlayerController().myHUD);
    if (bEnable)
    {
        if (HUD.DebugDrawList.Find(MyDebugDraw) == -1)
        {
            HUD.DebugDrawList.AddItem(MyDebugDraw);
        }
    }
    else
    {
        HUD.DebugDrawList.RemoveItem(MyDebugDraw);
    }
}

Snippets

Draw info on each Pawn currently spawned in the game:

public function MyDebugDraw(BioHUD HUD)
{
    local BioWorldInfo World;
    local BioPawn CurrPawn;
    local Vector ProjectLoc;
    local TPOV CamPov;
    
    World = BioWorldInfo(Class'Engine'.static.GetCurrentWorldInfo());
    CamPov = WorldRef.GetLocalPlayerController().PlayerCamera.CameraCache.POV;
    foreach World.AllActors(Class'BioPawn', CurrPawn)
    {
        // Required in order to skip drawing data for pawns behind the player's camera
        // Otherwise, they'd be placed in mirrored position in front of the camera
        if (Normal(CurrPawn.Location - CamPov.Location) Dot Vector(CamPov.Rotation) < 0.100000001)
        {
            continue;
        }

        // Project returns the screen location of the passed in-world location
        // In this case, we want to project the middle of pawn's body mass
        ProjectLoc = HUD.Canvas.Project(CurrPawn.Location);

        // Now set the UI's cursor position to the projected location.
        // I am doing a float -> int -> float cast in order to round the number to an int, 
        // because half-pixel locations cause the text to be blurry
        HUD.Canvas.SetPos(float(int(ProjectLoc.X)), float(int(ProjectLoc.Y)));

        // Now you can write some text
        // New line is inserted automatically
        HUD.Canvas.DrawText("Currently iterating through " $ string(CurrPawn));
    }
}

Change font size:

HUD.Canvas.Font = Class'Engine'.static.GetTinyFont();
HUD.Canvas.Font = Class'Engine'.static.GetSmallFont();
HUD.Canvas.Font = Class'Engine'.static.GetMediumFont();
HUD.Canvas.Font = Class'Engine'.static.GetLargeFont();
Clone this wiki locally