Skip to content

Latest commit

 

History

History
57 lines (42 loc) · 3.88 KB

cursor.md

File metadata and controls

57 lines (42 loc) · 3.88 KB

Cursor

Use the cursor script to simplify user interaction such as clicking and dragging of game objects.

Usage

Attach the cursor.script to a game object that should act as a cursor. The game object must have either a kinematic or trigger collision object with a group and mask that matches other collision objects that should be able to interact with the cursor.

IMPORTANT: You must make sure to configure the cursor.script action_id field (see below) to match the binding you have in your input bindings for the left mouse button/mouse button 1. The script assumes that the binding has action set to touch.

Script properties

The script has the following script properties:

  • action_id - (hash) The action_id that corresponds to a press/click/interact action (default: "touch")
  • drag - (boolean) If the cursor should be able to drag game objects
  • drag_threshold - (number) Distance the cursor has to move from a pressed object before it's considered dragged
  • collisionobject_filter - (hash) Id of a collision object component. If specified the cursor script will only accept collision messages from this id.
  • acquire_input_focus - (boolean) Check if the script should acquire input and handle input events itself
  • notify_own_gameobject - (boolean) Check if cursor messages should be sent not only to the interacting game object but also to the game object this script is attached to.

Input handling

You can let the cursor react to input in several ways:

  • Enable the acquire_input_focus property. This will make the script automatically respond to input events. If this isn't checked any on_input() calls are completely ignored.
  • Pass input messages. This will feed input events from an external source. This is useful if the app uses a camera solution or render script where screen coordinates doesn't translate to world coordinates and where conversion is required (using a screen_to_world function or similar). The input message is expected to have two fields; action_id and action in the same way as the on_input lifecycle function works.

Messages

The script will generate messages to game objects the cursor is interacting with and to the game object the cursor script is attached to for the following situations:

  • cursor_over - The cursors moves over the game object (cursor.OVER)
  • cursor_out - The cursor moves out from the game object (cursor.OUT)
  • pressed - When cursor is pressed (on the game object) (cursor.PRESSED)
  • released - When cursor is released (on the game object) (cursor.RELEASED)
  • clicked - When cursor clicked (on the game object) (cursor.CLICKED)
  • drag_start - When starting to drag the game object (cursor.DRAG_START)
  • drag_end - When no longer dragging the game object (cursor.DRAG_END)

The messages sent to the game object where the cursor script is attached will have id and group as well as x and y (from action.x and action.y) passed in the message.

Blocking input events

You can listen for events generated by the cursor script and block execution of the events. This can be used to block drag, pressed and over events.

cursor.listen(cursor_script_url, cursor_event, callback_fn)

Set up a listener for a specific cursor event from a specific cursor script.

  • cursor_script_url (url) - URL to the script you wish to listen to
  • cursor_event (hash) - The cursor message to listen for (see above)
  • callback_fn (function) - Function to call. Signature (message_id, message). Return true from the function to block execution of a cursor.OVER, cursor.PRESSED or cursor.DRAG_START event.

Example:

local cursor = require "in.cursor"

cursor.listen(msg.url("#cursor"), cursor.DRAG_START, function(message_id, message)
	-- prevent dragging of blue aliens
	if message.group == hash("blue") then
		return true -- return true to block the event
	end
end)