Drawing Text 3D & 2D (via game natives)
Used to draw text on your screen or in a 3D space. This only works on client-side. This implementation uses natives of the game, for an alternative take a look at official Text Label API added in 15.0.
Client Side
export function drawText2d(
msg,
x,
y,
scale,
fontType,
r,
g,
b,
a,
useOutline = true,
useDropShadow = true,
layer = 0,
align = 0
) {
let hex = msg.match('{.*}');
if (hex) {
const rgb = hexToRgb(hex[0].replace('{', '').replace('}', ''));
r = rgb[0];
g = rgb[1];
b = rgb[2];
msg = msg.replace(hex[0], '');
}
native.beginTextCommandDisplayText('STRING');
native.addTextComponentSubstringPlayerName(msg);
native.setTextFont(fontType);
native.setTextScale(1, scale);
native.setTextWrap(0.0, 1.0);
native.setTextCentre(true);
native.setTextColour(r, g, b, a);
native.setTextJustification(align);
if (useOutline) {
native.setTextOutline();
}
if (useDropShadow) {
native.setTextDropShadow();
}
native.endTextCommandDisplayText(x, y, 0);
}
export function drawText3d(
msg,
x,
y,
z,
scale,
fontType,
r,
g,
b,
a,
useOutline = true,
useDropShadow = true,
layer = 0
) {
let hex = msg.match('{.*}');
if (hex) {
const rgb = hexToRgb(hex[0].replace('{', '').replace('}', ''));
r = rgb[0];
g = rgb[1];
b = rgb[2];
msg = msg.replace(hex[0], '');
}
native.setDrawOrigin(x, y, z, 0);
native.beginTextCommandDisplayText('STRING');
native.addTextComponentSubstringPlayerName(msg);
native.setTextFont(fontType);
native.setTextScale(1, scale);
native.setTextWrap(0.0, 1.0);
native.setTextCentre(true);
native.setTextColour(r, g, b, a);
if (useOutline) {
native.setTextOutline();
}
if (useDropShadow) {
native.setTextDropShadow();
}
native.endTextCommandDisplayText(0, 0, 0);
native.clearDrawOrigin();
}
Example Usage
Client Side
alt.everyTick(() => {
drawText2d('Hello from Top Center of Screen', 0.5, 0.05, 0.4, 4, 255, 255, 255, 255);
const playerPos = { ...alt.Player.local.pos };
drawText3d(`This is You`, playerPos.x, playerPos.y, playerPos.z, 0.3, 4, 255, 255, 255, 255);
});