GetWeaponClipAmmo: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(The code is client-side only, so outputChatBox haven't visibleTo argument.)
(This function works only for custom weapons!)
Line 13: Line 13:


===Example===
===Example===
This example gets your ammo and output it!
This function outputs the remaining ammo in clip of a specific weapon using the command 'getammoinclip'.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
[lua]
[lua]
function getMyAmmo(player) -- Create a function named getMyAmmo and add player as the first parameter of addCommandHandler!
customWeapon = {}
  local weapon = getPedWeapon(player) -- Create a local variable with the value of getPedWeapon ( the weapon of the player )!
 
  local clipAmmo = getWeaponClipAmmo(weapon) -- Now use the local variable and get the weapon's clip ammo!
addEventHandler( "onClientResourceStart", resourceRoot,
  outputChatBox(tostring(clipAmmo)) -- Output the clipAmmo to the player, Please use tostring for it or it will give you a error that it should be a string!
    function()
end -- End the function
        local x, y, z = getElementPosition(localPlayer) -- Get the player's position.
        customWeapon.M4 = createWeapon("m4", x, y, z + 1) -- Create a M4 weapon
        setWeaponClipAmmo(customWeapon.M4, 99999) -- Set the ammo in clip of the weapon to 99999
        setWeaponState(customWeapon.M4, "firing")
        -- Add the 'getammoinclip' command to get the remaining ammo in clip of the weapon.
        addCommandHandler("getammoinclip", getM4WeaponAmmo)
    end
)
 
function getM4WeaponAmmo()
    -- Tell the player the remaining ammo in clip
    outputChatBox( tostring(getWeaponClipAmmo(customWeapon.M4)) )
end
</syntaxhighlight>
</syntaxhighlight>


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

Revision as of 04:13, 15 October 2014

This function gets the amount of ammo in a custom weapons magazine/clip.

Syntax

int getWeaponClipAmmo ( weapon theWeapon )

Required Arguments

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

Returns

Returns the amount of ammo in the custom weapons clip, false otherwise.

Example

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

[lua]
customWeapon = {}

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

function getM4WeaponAmmo()
    -- Tell the player the remaining ammo in clip
    outputChatBox( tostring(getWeaponClipAmmo(customWeapon.M4)) )
end

See Also