Skip to Content
defold-typescript libraries
indiesoftby

indiesoftby/defold-pointer-lock/pointer_lock

Mouse lock state and a virtual in-game cursor. Add pointer_lock/pointer_lock.script to the bootstrap collection: it locks the cursor on a click and unlocks it on key_esc, and keeps locked current. The cursor fields are plain module fields; assigning one goes through a cast, since a namespace import is read-only: (pointer as { cursor_visible: boolean }).cursor_visible = true.

  • GitHub: indiesoftby/defold-pointer-lock — pinned to 2.1.0
    1. Pick a release from indiesoftby/defold-pointer-lock releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as pointer_lock from "pointer_lock.pointer"

Functions

transform_input<A extends { x?: number; y?: number; }>(action_id: Hash | undefined, action: A): LuaMultiReturn<[ Hash | undefined, A & { x: number; y: number; } ]>

Converts on_input arguments into the virtual in-game cursor system: sets action.x and action.y to the cursor position, on the same table it returns, and returns undefined as the action id unless the pointer is locked and the cursor is visible. Call it first in on_input: [action_id, action] = pointer.transform_input(action_id, action).

Parameters

  • action_id: Hash | undefined
  • action: A

Returns

  • LuaMultiReturn<[ Hash | undefined, A & { x: number; y: number; } ]>

update_cursor_position(action: { dx: number; dy: number; })

Moves the virtual cursor by the action's dx and dy, clamped to the display size. Pass it a pointer-movement action, which carries both.

Parameters

  • action: { dx: number; dy: number; }
    • dx: number
    • dy: number

update_lock_state()

Copies the window's mouse lock state into locked.

Variables

DISPLAY_WIDTH: number

The display.width project setting, read once when the module loads.

DISPLAY_HEIGHT: number

The display.height project setting, read once when the module loads.

locked: boolean

Whether the pointer is locked. Read-only for a project: pointer_lock.script sets it, and update_lock_state copies the window's mouse lock into it. Starts false.

cursor_x: number

The X position of the virtual in-game cursor, between 0 and DISPLAY_WIDTH. Starts at the middle of the display; update_cursor_position moves it.

cursor_y: number

The Y position of the virtual in-game cursor, between 0 and DISPLAY_HEIGHT. Starts at the middle of the display; update_cursor_position moves it.

cursor_visible: boolean

Whether the virtual in-game cursor is shown. Starts false. transform_input passes the action id through only while the pointer is locked and this is true.

cursor_render_order: number

The GUI render order of the virtual cursor, 15 being the top. Starts at 15.

On this page