get height and width from game project in lua script (defold engine)
- User
get height and width from game project in lua script (defold engine)
- Assistant
get height and width from game project in lua script (defold engine)
In the Defold game engine, you typically manage the aspects related to the game window, viewport, and aspect ratios through project settings and scripts. When you want to get the height and width of the game project or the actual window size at runtime, you can achieve this through the render.get_window_width() and render.get_window_height() functions in a script attached to a render script.
Here’s a simple example of how you might go about retrieving and printing the width and height values of your game window in a Defold project using a Lua script:
Create or Edit Your Render Script: The Defold engine uses a specific script to control rendering operations, known as a render script. You may modify the existing render script or create a new one.
Access Window Dimensions: Use render.get_window_width() and render.get_window_height() functions within your render script to get the current window dimensions.
Example Render Script Modification:
Open your render script file (e.g., default.render_script or a custom one you've made) and update the update() function or create a suitable custom function to include the dimension retrieval code. You might want to print these dimensions or use them for your custom calculations.
Here's a simple example of how this could be done:
function update(self)
local width = render.get_window_width()
local height = render.get_window_height()
print("Window Width: " .. width)
print("Window Height: " .. height)
-- Your existing update code goes here
end
Place this code in the appropriate function in your render script where you need to know the window size. The update function is called every frame, so you might see the printed dimensions every frame in the console.
Remember, render.get_window_width() and render.get_window_height() are meant to be used inside render script where the render.* utilities are available. If you try to use it outside, for instance in a game object script (*.script), it will not work because render.* functions are specific to the context of rendering.
Adjusting for Different Devices and Resolutions: Defold is designed for cross-platform development, meaning your game might be displayed on devices with very different screen sizes and resolutions. The actual design and layout of your game should consider this. You may need to use more complex logic to adjust your game's UI and gameplay elements based on the window size or the device's resolution. Defold offers mechanisms for handling screen resizing and different aspect ratios, including setting up your game project with "Adjust Mode" in game.project settings under the "display" section to manage how your game scales on different devices.
Keep experimenting with Defold's rendering settings and scripts to find the best setup for your specific project requirements.
Not yet included in a published release.