Create a speed overlay

In this small example we will create a text overlay that will display your current 2D speed. First lets make sure the code we will write will be called every frame and thus update the display correctly.

function speedOverlay()
    --Nothing here yet
end

engine.addEventListener("render", speedOverlay)

Now for us to render our speed we have to first obtain it. The player namespace provides a method for this that we can use, called get2DSpeed. Let's plug that into the code we have.

function speedOverlay()
    local speed = player.get2DSpeed()
end

engine.addEventListener("render", speedOverlay)

We want to render this data to the screen, for example to the lower middle of it. For anything drawing related the draw namespace provides methods for most primitive shapes and text. The draw.text method expects arguments that we have to pass, namely:

  • content - This will be the speed we obtained

  • pos - For example the lower middle of your screen

  • color - The color of the speed, as a Vec4, e.g. Vec4:new(255, 0, 0, 255) for red

  • fontSize - The size of the text in pixels

  • fontName - The name of the font

Let's also floor our speed and convert it to a string and save it in another variable:

function speedOverlay()
    local speed = player.get2DSpeed()
    local speedString = tostring(math.floor(speed))
    draw.text(speedString, Vec2:new(960, 540), Vec4:new(255, 0, 0, 255), 36, "Verdana")
end

engine.addEventListener("render", speedOverlay)

The result shoud look similiar to this:

Last updated