Lua Graphics Module Documentation
Overviewβ
The Lua Graphics Module provides a range of functions for rendering various graphical elements in Lua scripts. This module empowers developers to create visually engaging user interfaces and enhance in-game visuals.
Register Graphics Callbackβ
This callback should only be used for graphics, as explained in Core - Overview
core.menu.register_on_render_callback(callback: function)
- This function registers the menu for interaction. Same like with other callbacks, you can also pass an anonymous function. This is how you would call the callback:
core.menu.register_on_render_callback(function()
-- your rendering code here
end)
Or:
local function my_render_function()
-- your rendering code here
end
core.menu.register_on_render_callback(my_render_function)
Functions π οΈβ
Line Of Sight ποΈβ
In most cases, you should NOT use this function, since it's very expensive. Instead, you should instead use the spell_helper:is_spell_in_line_of_sight() function. See Spell Helper - LOS
core.graphics.is_line_of_sight(caster, target)
caster: game_object - The caster object.target: game_object - The target object.
boolean:trueif thetargetis in line of sight from thecaster,falseotherwise.
Determines if the target is within the line of sight of the caster.
You can use this function to check visibility between two game objects.
Cursor World Position ποΈβ
Syntaxcore.graphics.get_cursor_world_position()
- vec3 : The current cursor position's coordinates transformed to 3D dimensions.
Retrieves the current cursor position screen coordinates (2D) and returns it after transforming them to 3D.
Trace Line π§β
Syntaxcore.graphics.trace_line(pos1, pos2, flags)
pos1: vec3 - Starting position.pos2: vec3 - Ending position.flags: Collision flags that determine which objects to consider during tracing.
boolean:trueif there is a valid trace line betweenpos1andpos2,falseotherwise.
Indicates if there is a valid trace line between pos1 and pos2 following the collision flags you provide.
Collision Flags
None,
DoodadCollision = 0x00000001,
DoodadRender = 0x00000002,
WmoCollision = 0x00000010,
WmoRender = 0x00000020,
WmoNoCamCollision = 0x00000040,
Terrain = 0x00000100,
IgnoreWmoDoodad = 0x00002000,
LiquidWaterWalkable = 0x00010000,
LiquidAll = 0x00020000,
Cull = 0x00080000,
EntityCollision = 0x00100000,
EntityRender = 0x00200000,
Collision = DoodadCollision | WmoCollision | Terrain | EntityCollision,
LineOfSight = WmoCollision | EntityCollision
The collision flags are located in enums.collision_flags. You should import the enums module:
---@type enums
local enums = require("common/enums")
Avoid using their values directly, as this is not recommended since these values might change in the future. By importing the enums module, any updates will automatically be reflected in your code.
You can use this function to see which enemies are not in line of sight. You can adjust the flags according to your requirements.
Example:
Here's an example function to gather all units that are not in line of sight within a search distance:---@type unit_helper
local unit_helper = require("common/utility/unit_helper")
---@type enums
local enums = require("common/enums")
---@param search_distance number
---@return table<game_object> | nil
local function get_enemies_that_are_not_in_los(search_distance)
local local_player = core.object_manager.get_local_player()
if not local_player then
return nil
end
local local_player_position = local_player:get_position()
local enemies = unit_helper:get_enemy_list_around(local_player_position, search_distance, true)
local not_in_los_enemies = {}
for _, enemy in ipairs(enemies) do
local enemy_pos = enemy:get_position()
-- trace_line will return true if the enemy is in line of sight, as long as we pass the enums.collision_flags.LineOfSight flag
local is_in_los = core.graphics.trace_line(local_player_position, enemy_pos, enums.collision_flags.LineOfSight)
if not is_in_los then
table.insert(not_in_los_enemies, enemy)
end
end
return not_in_los_enemies
end
For this example, to check if something is in LOS (line of sight), you could also use the core.graphics.is_line_of_sight function, defined earlier.
World to Screen πβ‘οΈπ₯οΈβ
Syntaxcore.graphics.w2s(position)
position: vec3 - The 3D world position to convert.
- vec2: The 2D screen position corresponding to the 3D world position.
Converts a 3D world position to a 2D screen position, facilitating the rendering of objects in screen space.
Before using the return value, you should make sure it's not nil, since a vec3 out of the screen won't be converted and will return nil.
Example:
Drawing text at the 2D position of a given unit---@type color
local color = require("common/color")
---@param unit game_object
---@param text string
local function draw_text_at_unit_screen_position(unit, text)
if not unit then
return
end
local unit_position = unit:get_position()
local unit_screen_position = core.graphics.w2s(unit_position)
if not unit_screen_position then
return
end
core.graphics.text_2d(text, unit_screen_position, 16, color.cyan(230))
end
Is Menu Open ππβ
Syntaxcore.graphics.is_menu_open()
boolean:trueif the main menu is visible,falseotherwise.
Checks if the main menu is currently open.
Get Screen Size π₯οΈπβ
Syntaxcore.graphics.get_screen_size()
- vec2: The width and height of the screen.
Retrieves the current screen size in pixels.
Render 2D Text πβ
Syntaxcore.graphics.text_2d(text, position, font_size, color, centered, font_id)
text:string- The text to render.position: vec2 - The position where the text will be rendered.font_size:number- The font size of the text.color: color - The color of the text.centered(Optional):boolean- Indicates whether the text should be centered at the specified position. Default isfalse.font_id(Optional):integer- The font ID. Default is0.
Renders 2D text on the screen at the specified position with the given font size and color.