GetMouseHitEntity: Difference between revisions

From Onset Developer Wiki
28days (talk | contribs)
No edit summary
No edit summary
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{Info|Function|Client|1.0}}
{{Info|Function|Client|1.0}}


{{FuncDescription|Get entity type and identifier the mouse pointer hits.}}
{{FuncDescription|Get entity type and identifier the mouse pointer hits. This basically traces from the position of the mouse cursor to the next colliding geometry in world space. }}


{{FuncSyntax|GetMouseHitEntity()}}
{{FuncSyntax|GetMouseHitEntity()}}
Line 11: Line 11:


== Example ==
== Example ==
<syntaxhighlight lang="Lua">
Example from the map editor package. https://github.com/BlueMountainsIO/OnsetLuaScripts/blob/master/mapeditor/client/editor.lua
local entityType, entityId = GetMouseHitEntity()
<syntaxhighlight lang="Lua>
function OnKeyPress(key)
if key == "Left Mouse Button" then
if IsEditMode then
local x, y, z, distance = GetMouseHitLocation()
AddPlayerChat("Hit: "..x..", "..y..", "..z..", "..distance)
 
-- if selected location is not valid (i.e. clicking sky) 0.0 is returned. we set it to something high so the check is triggered later on.
if distance == 0.0 then
distance = 999999.9
end
 
SelectedLoc.x = x
SelectedLoc.y = y
SelectedLoc.z = z
SelectedLoc.distance = distance
SelectedLoc.isValid = true
 
local EntityType, EntityId = GetMouseHitEntity()
if (EntityType == HIT_OBJECT) then
if (EntityId ~= 0 and SelectedObject ~= EntityId) then
SelectEditorObject(EntityId)
end
end
end
end
end
AddEvent("OnKeyPress", OnKeyPress)
</syntaxhighlight>
</syntaxhighlight>


{{RelatedFunctions}}
{{RelatedFunctions}}
__EDIT_ME__
{{Template:ClientMouseFunctions}}

Latest revision as of 20:13, 30 August 2020

GetMouseHitEntity

Type: Function
Context: Client
Introduced: v1.0

Description

Get entity type and identifier the mouse pointer hits. This basically traces from the position of the mouse cursor to the next colliding geometry in world space.

Syntax

GetMouseHitEntity()

Parameters

  • This function has no parameters.

Return Value

  • Returns the hit entity type HitType and entity identifier.

Example

Example from the map editor package. https://github.com/BlueMountainsIO/OnsetLuaScripts/blob/master/mapeditor/client/editor.lua

function OnKeyPress(key)
	if key == "Left Mouse Button" then
		if IsEditMode then
			local x, y, z, distance = GetMouseHitLocation()
			AddPlayerChat("Hit: "..x..", "..y..", "..z..", "..distance)

			-- if selected location is not valid (i.e. clicking sky) 0.0 is returned. we set it to something high so the check is triggered later on.
			if distance == 0.0 then
				distance = 999999.9
			end

			SelectedLoc.x = x
			SelectedLoc.y = y
			SelectedLoc.z = z
			SelectedLoc.distance = distance
			SelectedLoc.isValid = true

			local EntityType, EntityId = GetMouseHitEntity()
			if (EntityType == HIT_OBJECT) then
				if (EntityId ~= 0 and SelectedObject ~= EntityId) then
					SelectEditorObject(EntityId)
				end
			end
		end
	end
end
AddEvent("OnKeyPress", OnKeyPress)

See also