sm.areaTrigger

Associated object type: AreaTrigger

An area trigger is an invisible collider in the world that can trigger events when objects move in or out of it. This allows the script to, for instance, detect when a character enters a door, or count the number of shapes there are in a room.

Example usage:

	function MyClass.server_onCreate( self )
		local size = sm.vec3.new( 1, 1, 1 )
		local position = self.shape:getWorldPosition()

		self.myArea = sm.areaTrigger.createBox( size, position )
		self.myArea:bindOnEnter( "onEnter" )
	end

	function MyClass.onEnter( self, trigger, results )
		for i, object in ipairs( results ) do
			print( object, "just entered" )
		end
	end

Example with a filter:

	function MyClass.server_onCreate( self )
		local size = sm.vec3.new( 10, 10, 5 )
		local position = sm.vec3.new( 50, 40, 30 )

		-- Only detect characters
		local filter = sm.areaTrigger.filter.character

		self.myArea = sm.areaTrigger.createBox( size, position, filter )
		self.myArea:bindOnStay( "onStay" )
	end

	-- Callback receives a list of characters
	function MyClass.onStay( self, trigger, results )
		if #results > 0 then
			print( "Intruder alert!" )
		end
	end

Constants:

Functions:

  • createAttachedBox
  • createAttachedSphere
  • createBox
  • createBoxWater
  • createSphere
  • destroy

  • sm.areaTrigger.filter table

    Filters are used to specify what object types an area trigger is able to detect. If an area trigger is created with a filter, it will only react to objects of that type. Filters can be combined by adding them.

    The filters are:

    dynamicBody 1
    staticBody 2
    character 4
    areatrigger 8
    harvestable 512
    lift 1024
    voxelTerrain 32768
    all 34319

    sm.areaTrigger.createAttachedBox(interactable, dimension, position=nil, rotation=nil, filter=nil, userdata=nil)

    Creates an area trigger box with a given size that stays attached to an interactable

    If a filter is specified, the trigger area will only be able to detects objects of that certain type. See sm.areaTrigger.filter for more information about filters.

    Parameters:

    TypeNameDescription
    InteractableinteractableThe host interactable
    Vec3dimensionThe size of the box
    Vec3position=nilThe position offset (Defaults to sm.vec3.zero)
    Quatrotation=nilThe rotation offset (Defaults to sm.quat.identity)
    integerfilter=nilThe object types the area trigger may detect. (See sm.areaTrigger.filter). (Defaults to sm.areaTrigger.filter.all)
    tableuserdata=nilAn optional table of user data

    Returns:

    TypeDescription
    AreaTriggerThe created area trigger.

    sm.areaTrigger.createAttachedSphere(interactable, radius, position=nil, rotation=nil, filter=nil, userdata=nil)

    Creates an area trigger sphere with a given size that stays attached to an interactable

    If a filter is specified, the trigger area will only be able to detects objects of that certain type. See sm.areaTrigger.filter for more information about filters.

    Parameters:

    TypeNameDescription
    InteractableinteractableThe host interactable
    numberradiusThe radius of the sphere.
    Vec3position=nilThe position offset (Defaults to sm.vec3.zero)
    Quatrotation=nilThe rotation offset (Defaults to sm.quat.identity)
    integerfilter=nilThe object types the area trigger may detect. (See sm.areaTrigger.filter). (Defaults to sm.areaTrigger.filter.all)
    tableuserdata=nilAn optional table of user data

    Returns:

    TypeDescription
    AreaTriggerThe created area trigger.

    sm.areaTrigger.createBox(dimension, position, rotation=nil, filter=nil, userdata=nil)

    Creates a new box area trigger at a given position with a given size.

    If a filter is specified, the trigger area will only be able to detects objects of that certain type. See sm.areaTrigger.filter for more information about filters.

    Parameters:

    TypeNameDescription
    Vec3dimensionThe dimensions of the box.
    Vec3positionThe world position.
    Quatrotation=nilThe world rotation. (Defaults to sm.quat.identity)
    integerfilter=nilThe object types the area trigger may detect. (See sm.areaTrigger.filter). (Defaults to sm.areaTrigger.filter.all)
    tableuserdata=nilAn optional table of user data

    Returns:

    TypeDescription
    AreaTriggerThe created area trigger.

    sm.areaTrigger.createBoxWater(dimension, position, rotation=nil, filter=nil, userdata=nil)

    Creates a new box area trigger that represent water ie. certain objects cant be placed in it.

    If a filter is specified, the trigger area will only be able to detects objects of that certain type. See sm.areaTrigger.filter for more information about filters.

    Parameters:

    TypeNameDescription
    Vec3dimensionThe dimensions of the box.
    Vec3positionThe world position.
    Quatrotation=nilThe world rotation. (Defaults to sm.quat.identity)
    integerfilter=nilThe object types the area trigger may detect. (See sm.areaTrigger.filter). (Defaults to sm.areaTrigger.filter.all)
    tableuserdata=nilAn optional table of user data

    Returns:

    TypeDescription
    AreaTriggerThe created area trigger.

    sm.areaTrigger.createSphere(radius, position, rotation=nil, filter=nil, userdata=nil)

    Creates a new sphere area trigger at a given position with a given size.

    If a filter is specified, the trigger area will only be able to detects objects of that certain type. See sm.areaTrigger.filter for more information about filters.

    Parameters:

    TypeNameDescription
    numberradiusThe radius of the sphere.
    Vec3positionThe world position.
    Quatrotation=nilThe world rotation. (Defaults to sm.quat.identity)
    integerfilter=nilThe object types the area trigger may detect. (See sm.areaTrigger.filter). (Defaults to sm.areaTrigger.filter.all)
    tableuserdata=nilAn optional table of user data

    Returns:

    TypeDescription
    AreaTriggerThe created area trigger.

    sm.areaTrigger.destroy(areaTrigger)

    Destroys an area trigger.

    Parameters:

    TypeNameDescription
    AreaTriggerareaTriggerThe area trigger to be destroyed.