GetWeaponClipAmmo: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(This function works only for custom weapons!)
mNo edit summary
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client function}}
{{Client function}}
This function gets the amount of ammo in a custom weapons magazine/clip.
This function gets the amount of ammo left in a [[Element/Weapon|custom weapon]]'s magazine/clip.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">int getWeaponClipAmmo ( weapon theWeapon )</syntaxhighlight>
<syntaxhighlight lang="lua">int getWeaponClipAmmo ( weapon theWeapon )</syntaxhighlight>
{{OOP||[[Element/Weapon|weapon]]:getClipAmmo|clipAmmo|setWeaponClipAmmo}}


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


===Returns===
===Returns===
Returns the amount of ammo in the custom weapons clip, false otherwise.
Returns the amount of ammo in the [[Element/Weapon|custom weapon]]'s clip, ''false'' if an error occured.


===Example===
===Example===
This function outputs the remaining ammo in clip of a specific weapon using the command 'getammoinclip'.
This function outputs the remaining ammo in clip of a specific weapon using the command ''/getammoinclip''.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
[lua]
local customWeapon
customWeapon = {}


addEventHandler( "onClientResourceStart", resourceRoot,
addEventHandler( "onClientResourceStart", resourceRoot,
     function()
     function()
         local x, y, z = getElementPosition(localPlayer) -- Get the player's position.
         local x, y, z = getElementPosition(localPlayer) -- Get player position
         customWeapon.M4 = createWeapon("m4", x, y, z + 1) -- Create a M4 weapon
         customWeapon = createWeapon("m4", x, y, z + 1) -- Create a M4
         setWeaponClipAmmo(customWeapon.M4, 99999) -- Set the ammo in clip of the weapon to 99999
         setWeaponClipAmmo(customWeapon, 99999) -- Set the ammo in clip of the weapon to 99999, so it never should reload
         setWeaponState(customWeapon.M4, "firing")
         setWeaponState(customWeapon, "firing") -- Fire it permanently
         -- Add the 'getammoinclip' command to get the remaining ammo in clip of the weapon.
         -- Add the 'getammoinclip' command to get the remaining ammo in clip of the weapon
         addCommandHandler("getammoinclip", getM4WeaponAmmo)
         addCommandHandler("getammoinclip", getM4WeaponAmmo)
     end
     end
Line 30: Line 30:


function getM4WeaponAmmo()
function getM4WeaponAmmo()
     -- Tell the player the remaining ammo in clip
     if customWeapon then
    outputChatBox( tostring(getWeaponClipAmmo(customWeapon.M4)) )
        -- Tell the player the remaining ammo in clip
        outputChatBox(getWeaponClipAmmo(customWeapon))
    else
        -- Weapon was not created, give an error
        outputChatBox("There is no weapon to get clip ammo of.")
    end
end
end
</syntaxhighlight>
</syntaxhighlight>


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

Latest revision as of 21:44, 7 May 2017

This function gets the amount of ammo left in a custom weapon's magazine/clip.

Syntax

int getWeaponClipAmmo ( weapon theWeapon )

OOP Syntax Help! I don't understand this!

Method: weapon:getClipAmmo(...)
Variable: .clipAmmo
Counterpart: setWeaponClipAmmo


Required Arguments

  • theWeapon: the weapon to get the clip ammo of.

Returns

Returns the amount of ammo in the custom weapon's clip, false if an error occured.

Example

This function outputs the remaining ammo in clip of a specific weapon using the command /getammoinclip.

local customWeapon

addEventHandler( "onClientResourceStart", resourceRoot,
    function()
        local x, y, z = getElementPosition(localPlayer) -- Get player position
        customWeapon = createWeapon("m4", x, y, z + 1) -- Create a M4
        setWeaponClipAmmo(customWeapon, 99999) -- Set the ammo in clip of the weapon to 99999, so it never should reload
        setWeaponState(customWeapon, "firing") -- Fire it permanently
        -- Add the 'getammoinclip' command to get the remaining ammo in clip of the weapon
        addCommandHandler("getammoinclip", getM4WeaponAmmo)
    end
)

function getM4WeaponAmmo()
    if customWeapon then
        -- Tell the player the remaining ammo in clip
        outputChatBox(getWeaponClipAmmo(customWeapon))
    else
        -- Weapon was not created, give an error
        outputChatBox("There is no weapon to get clip ammo of.")
    end
end

See also