FireWeapon: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Removed redundant template)
(added another example that does more than fire a single shot)
Line 14: Line 14:


==Example==  
==Example==  
<section name="Example 1" class="client" show="true">
This function creates and fires a weapon.
This function creates and fires a weapon.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 22: Line 23:
addEventHandler("onClientResourceStart", resourceRoot, createAndFire)
addEventHandler("onClientResourceStart", resourceRoot, createAndFire)
</syntaxhighlight>
</syntaxhighlight>
</section>
<section name="Example 2" class="client" show="true">
This example will create an M4, attach it to the local player and fire it every frame. Be aware that neither the weapon nor the shots are synced between players and that normally weapons don't fire every frame.
<syntaxhighlight lang="lua">
local wep
function resStart()
wep = createWeapon("m4",0,0,0) -- when the resource starts, create the M4 and attach it to the local player with an offset to place it above their head facing forwards
attachElements(wep,getLocalPlayer(),0,0,1,0,0,90)
end
addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()), resStart)
function wepFire() -- fire the M4 every frame using the "onClientRender" event
fireWeapon(wep)
end
addEventHandler("onClientRender",getRootElement(),wepFire)
</syntaxhighlight>
</section>


==Requirements==
==Requirements==

Revision as of 15:43, 23 January 2023

Fires one shot from a custom weapon.

Syntax

bool fireWeapon ( weapon theWeapon )

OOP Syntax Help! I don't understand this!

Method: weapon:fire(...)


Required Arguments

  • theWeapon: The weapon to be fired.

Returns

Returns true if the shot weapon is valid and therefore the shot was fired, false otherwise.

Example

Click to collapse [-]
Example 1

This function creates and fires a weapon.

function createAndFire()
    local weapon = createWeapon("mp5", 0, 0, 3) -- Create a MP5 at the coordinates 0, 0, 3
    fireWeapon(weapon) -- Fire the weapon we spawned
end
addEventHandler("onClientResourceStart", resourceRoot, createAndFire)
Click to collapse [-]
Example 2

This example will create an M4, attach it to the local player and fire it every frame. Be aware that neither the weapon nor the shots are synced between players and that normally weapons don't fire every frame.

local wep
function resStart()
	wep = createWeapon("m4",0,0,0) -- when the resource starts, create the M4 and attach it to the local player with an offset to place it above their head facing forwards
	attachElements(wep,getLocalPlayer(),0,0,1,0,0,90)
end
addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()), resStart)

function wepFire() -- fire the M4 every frame using the "onClientRender" event
	fireWeapon(wep)
end
addEventHandler("onClientRender",getRootElement(),wepFire)

Requirements

This template will be deleted.

See also