defold engine, get user screen info in runtime
- User
defold engine, get user screen info in runtime
- Assistant
In the Defold engine, you can obtain information about the user's screen at runtime using Lua scripting within your game. Defold provides functions to query details about the screen, view, and display.
Here are some of the key functions and an example script to get user screen information:
Key Functions
display.get_info(): Retrieves information about the user's display, including width, height, and DPI.render.get_width(): Gets the current width of the window.render.get_height(): Gets the current height of the window.
Example Script
This script retrieves and prints screen information such as screen width, screen height, and device DPI.
luafunction init(self) -- Retrieve display info local display_info = sys.get_config("display") local screen_width = tonumber(display_info.width) local screen_height = tonumber(display_info.height) local screen_dpi = tonumber(display_info.dpi) print(string.format("Screen Width: %d", screen_width)) print(string.format("Screen Height: %d", screen_height)) print(string.format("Screen DPI: %d", screen_dpi)) -- Optionally, you can retrieve window size too (this may differ from screen size in windowed apps) local window_width = render.get_width() local window_height = render.get_height() print(string.format("Window Width: %d", window_width)) print(string.format("Window Height: %d", window_height)) -- Store information in self for later use self.screen_width = screen_width self.screen_height = screen_height self.screen_dpi = screen_dpi self.window_width = window_width self.window_height = window_height endExplanation
sys.get_config("display"): This retrieves the display settings configured in the game's file. While it doesn't give dynamic runtime info, it provides initial configuration details.
- User
I need resolution of youser display
- Assistant
To obtain the resolution of the user's display at runtime in Defold, you can use the
sys.get_sys_info()function, which provides information about the system, such as the device model, language, and display dimensions. This function returns a table with various details, including the physical dimensions of the display.