Calculate and display performance metrics in Defold games.
You can use Defold Metrics in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:
https://github.com/britzl/defold-metrics/archive/master.zip
Or point to the ZIP file of a specific release.
- In your game.project settings, go to Dependencies,and add https://github.com/britzl/defold-metrics/archive/master.zip.
- In the outline in your scene, right-click and "Add Game Object File" and choose "/metrics/fps.go"
- Change the position on the game object to ex 10, 20, 0 to make it visible.
- If you also want to use the Lua memory counter, add the "/metrics/mem.go" in the same way, but on position 10, 40, 0.
Demo: https://britzl.github.io/Metrics/
The FPS counter uses socket.gettime() to get an accurate timestamp and calculates an average FPS based on a sequence of samples. The FPS counter can be used in several ways:
- Using fps.go() - Draw FPS counter at game object world position using draw text
- Using fps.script - Draw FPS counter at game object world position using draw text
- Using fps.lua - Draw FPS counter at specified position or get current FPS
Create an instance of the FPS counter
PARAMETERS
samples
(number) - Optional sample count. This is the number of samples required before the module will calculate FPS. Defaults to 60.format
(string) - Optional format to draw FPS in. Defaults to "FPS %.2f"position
(string) - Optional position to draw FPS at. Defaults to v3(10, 20, 0)color
(string) - Optional color to use when drawing FPS text. Defaults to v4(0,0,1,1)
RETURNS
instance
(table) - An FPS counter instance
Call this once per frame. Once enough samples have been collected it is possible to call fps() to get the current FPS.
Get the current FPS, based on collected samples.
RETURNS
fps
(number) - The calculated FPS
Draw fps count text using draw_debug_text
.
The memory counter uses collectgarbage("count")
to get the amount of Lua memory used. The memory counter can be used in several ways:
- Using mem.go() - Draw memory usage at game object world position using draw text
- Using mem.script - Draw memory usage at game object world position using draw text
- Using mem.lua - Draw memory usage at specified position or get current memory usage
Create an instance of the memory counter
PARAMETERS
format
(string) - Optional format to draw memory usage in. Defaults to "MEM %dkb"position
(string) - Optional position to draw memory usage at. Defaults to v3(10, 20, 0)color
(string) - Optional color to use when drawing memory usage text. Defaults to v4(0,0,1,1)
RETURNS
instance
(table) - A memory counter counter instance
Call this to get a new memory usage reading.
Get the current memory usage, in kilobytes.
RETURNS
mem
(number) - The Lua memory usage
Draw memory usage text using draw_debug_text
.