GetWeaponTarget: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Remove obsolete Requirements section)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client function}}
{{Client function}}
{{Needs Example}}
This functions gets the target of a [[Element/Weapon|custom weapon]].
Get the target of the custom weapon.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">element getWeaponTarget ( weapon theWeapon )</syntaxhighlight>
<syntaxhighlight lang="lua">nil/element/float getWeaponTarget ( weapon theWeapon )</syntaxhighlight>
{{OOP|Variable is read only.|[[Element/Weapon|weapon]]:getTarget|target|setWeaponTarget}}


===Required Arguments===
===Required Arguments===
Line 11: Line 11:


===Returns===
===Returns===
Returns the ''target element'' of the custom weapon, ''false'' otherwise.
* Returns the ''target'' of the [[Element/Weapon|custom weapon]], which can be:
**''[[nil]]'' if the weapon is in rotation based targeting.
**3 [[float|floats]] if the weapon is firing at a fixed point.
**an [[element]] if the weapon is firing an entity.
* Returns ''false'' if the weapon element is not valid.


==Example==
==Example==
Create Weapon and get weapon target.When it founded - fire weapon to the target all time,if it in the weapon sight.
This example gets the weapon target when the player hit the colshape and outputs it to the chatbox.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">local col = createColSphere(1647.33984375,1785.03125,10.671875,8) -- Create col sphere near to LV hospital
function CreateWeapon ()
local weapon = createWeapon ("m4",1647.33984375,1785.03125,10.671875) -- Create the weapon
  local weapon = createWeapon ("m4", 0, 0, 0)  --create gun "m4" in 0,0,0 position.
 
  local weapontarget = getWeaponTarget ( weapon )
function onClientColShapeHit(element, matchDim )
    if weapontarget then
  if (element == getLocalPlayer()) then -- Checks whether the entering element is the local player
    setTimer (function()
    if weapon then -- if the weapon exist then
    fireWeapon ( weapon )
        setWeaponTarget (weapon,element,8) -- Set the weapon target to the localPlayer
    end,100,0)
        local target = getWeaponTarget (weapon) -- get weapon target
     end
          if target and isElement(target) and getElementType(target) == "player" then
            outputChatBox("The target of the custom weapon: "..getPlayerName(target)) -- output to the chatbox
          end
      end  
     end  
end
end
addEventHandler ("onClientResourceStart",getRootElement(),CreateWeapon)
addEventHandler("onClientColShapeHit",col,onClientColShapeHit)</syntaxhighlight>
</syntaxhighlight>
 
==Requirements==
{{Requirements|n/a|1.3.0-9.04555|}}


==See Also==
==See also==
{{Client weapon creation functions}}
{{Client weapon creation functions}}

Latest revision as of 17:07, 7 November 2024

This functions gets the target of a custom weapon.

Syntax

nil/element/float getWeaponTarget ( weapon theWeapon )

OOP Syntax Help! I don't understand this!

Note: Variable is read only.
Method: weapon:getTarget(...)
Variable: .target
Counterpart: setWeaponTarget


Required Arguments

  • theWeapon: The weapon to get the target of.

Returns

  • Returns the target of the custom weapon, which can be:
    • nil if the weapon is in rotation based targeting.
    • 3 floats if the weapon is firing at a fixed point.
    • an element if the weapon is firing an entity.
  • Returns false if the weapon element is not valid.

Example

This example gets the weapon target when the player hit the colshape and outputs it to the chatbox.

local col = createColSphere(1647.33984375,1785.03125,10.671875,8) -- Create col sphere near to LV hospital
local weapon = createWeapon ("m4",1647.33984375,1785.03125,10.671875) -- Create the weapon

function onClientColShapeHit(element, matchDim )
   if (element == getLocalPlayer()) then  -- Checks whether the entering element is the local player 
     if weapon then -- if the weapon exist then
        setWeaponTarget (weapon,element,8) -- Set the weapon target to the localPlayer 
        local target = getWeaponTarget (weapon) -- get weapon target
          if target and isElement(target) and getElementType(target) == "player" then 
            outputChatBox("The target of the custom weapon: "..getPlayerName(target)) -- output to the chatbox
          end 
       end 
    end 
end
addEventHandler("onClientColShapeHit",col,onClientColShapeHit)

See also