Requested display width and height is not always obeyed?

TL;DR:
action.x and action.y are the action positions in virtual coordinates, which are needed to detect clicks in the GUI using gui.pick_node(). Most likely, you don’t need them for any manual calculations.
Use action.screen_x and action.screen_y instead.


Full Explanation

The engine does the following steps to get these values:

  1. Raw Input: Defold receives mouse/touch coordinates in actual screen pixels (where the user clicked).
  2. Virtual Scaling: These pixel coordinates are scaled to match your game’s virtual resolution (set in game.project under display.width and display.height).
  3. Pixel Centering: A 0.5 pixel offset ensures coordinates point to the center of pixels rather than their corners.

What This Means for You

  • action.x and action.y are always in your game’s virtual coordinate space.
  • If your game is set to 960x640 virtual resolution, coordinates will be 0–960 horizontally and 0–640 vertically.
  • This works regardless of the actual screen size — a 1920x1080 screen or a 480x320 screen will both give you the same virtual coordinates.

What Should You Use Then?

It’s better to use action.screen_x and action.screen_y for everything you want to calculate yourself.

Screen space is a universal space that can be used to convert different coordinates between different systems. For example:

  • From world to GUI
  • From GUI to world (depends on camera setup)
  • From a GUI node with one scale setting to a GUI node with another

Helpful Functions for Screen-Space Conversions

We don’t have such functions for go because they highly depend on the camera/render_script setup. (We plan to add them under the camera.* or go.* namespaces.)

In the meantime, here are examples of helper functions you can use:

5 Likes