ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Client functions
  3. dxDrawText

Function

dxDrawText

Draws text on the screen for one frame. Call it in onClientRender to keep it visible.

Syntax

Short form

bool dxDrawText(string text, float left, float top [, float right, float bottom, int color, float scale, font font, string alignX, string alignY, bool clip, bool wordBreak, bool postGUI, bool colorCoded])

With an options table

bool dxDrawText(string text, float left, float top, float right, float bottom, int color, table options)

Arguments

textstring
The text to draw.
left, topfloat
Top-left corner of the text area in pixels.
right, bottomfloat, optional = left, top
Bottom-right corner of the area. Needed to align to the centre or the right.
colorint, optional = tocolor(255, 255, 255)
Colour from tocolor(r, g, b, a).
scalefloat, optional = 1
Text scale. You can give two numbers: horizontal and vertical.
fontstring|font, optional = default
Name of a built-in font (default, default-bold, pricedown...) or a font from dxCreateFont.
alignXstring, optional = left
left, center or right.
alignYstring, optional = top
top, center or bottom.
clipbool, optional = false
Cuts off text that sticks out of the area.
wordBreakbool, optional = false
Wraps long text onto several lines within the area.
postGUIbool, optional = false
Draws above GUI windows.
colorCodedbool, optional = false
Enables colours in the text written like #FF7A22.
optionstable, optional
Instead of the argument list: a table with font, scale, alignX, alignY, shadow, outline, fit, ellipsis and more.Different type in MTA:SA: bool subPixelPositioning = false

Differences from MTA:SA

  • Arguments in a different colour differ from MTA:SA.
  • These MTA:SA arguments are not here: float fRotation = 0.0, float fRotationCenterX = 0.0, float fRotationCenterY = 0.0, float fLineSpacing = 0.0

In MTA:SA

dxDrawText(string text, float leftX, float topY, float rightX = leftX, float bottomY = topY, int color = 0xFFFFFFFF, float textSize, string/dx-font font = "default", string alignX = "left", string alignY = "top", bool clip = false, bool wordBreak = false, bool postGUI = false, bool colorCoded = false, bool subPixelPositioning = false, float fRotation = 0.0, float fRotationCenterX = 0.0, float fRotationCenterY = 0.0, float fLineSpacing = 0.0)

Returns

true if the text was drawn, or false on a bad argument.

Example

A speedometer in the bottom-right corner, with an outline like San Andreas text.

local sw, sh = guiGetScreenSize()

addEventHandler("onClientRender", root, function()
    local car = getPedOccupiedVehicle(localPlayer)
    if not car then return end

    local vx, vy, vz = getElementVelocity(car)  -- metres per second
    local kmh = math.floor((vx * vx + vy * vy + vz * vz) ^ 0.5 * 3.6)

    dxDrawText(kmh .. " km/h", 0, 0, sw - 40, sh - 40, tocolor(255, 255, 255), {
        font = "pricedown", scale = 2,
        alignX = "right", alignY = "bottom",
        outline = 2
    })
end)

Notes

Options table. Instead of a long argument list you can pass an options table. It also has extras: a custom shadow (shadow), an outline (outline), scaling to fit the width (fit) and cutting off with an ellipsis (ellipsis).

Text rotation. The rotation arguments are accepted, but text is not rotated yet. The script gets a single warning in the console.

One frame only. The text disappears on the next frame. To keep it visible, draw it every frame in onClientRender.

See also