AddVehicleUpgrade: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(→‎Example: changed example to use command handler rather than onConsole event)
Line 14: Line 14:
==Example==
==Example==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler ( "onPlayerConsole", root, "onPlayerConsole" )
-- add a console command
function onPlayerConsole ( text )
addCommandHandler ( "addupgrade", "consoleAddUpgrade" )
  command = split ( text, 1, 32 )
function consoleAddUpgrade ( player, commandName, id )
  if ( command == "nos" ) then
    if ( player ) then
    vehicle = getPlayerOccupiedVehicle ( source )
        if ( isPlayerInVehicle ( player ) ) then
    if ( vehicle ) then
            -- conver the ID to a number
      addVehicleUpgrade ( vehicle, 1010 ) -- nos-10x
            id = tonumber ( id )
            -- get the player's vehicle
            local vehicle = getPlayerOccupiedVehicle ( player )
            -- add the requested upgrade to the vehicle
            local success = addVehicleUpgrade ( vehicle, id )
            if ( success ) then
                -- get the name of the upgrade and display it
                outputConsole ( getVehicleUpgradeSlotName ( id ) .. " upgrade added.", player )
            else
                outputConsole ( "Failed to add upgrade.", player )
            end
        else
            outputConsole ( "You must be in a vehicle!", player )
        end
     end
     end
  end
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 05:35, 18 June 2007

This function adds an upgrade to an existing vehicle, eg: nos, hyrdraulics. Defined in San Andreas\data\maps\veh_mods\veh_mods.ide.

Syntax

bool addVehicleUpgrade ( vehicle theVehicle, int upgrade )

Required Arguments

  • theVehicle: The element representing the vehicle you wish to add the upgrade to.
  • upgrade: The id of the upgrade you wish to add.

Returns

Returns true if the upgrade was successfully added to the vehicle, otherwise false.

Example

-- add a console command
addCommandHandler ( "addupgrade", "consoleAddUpgrade" )
function consoleAddUpgrade ( player, commandName, id )
    if ( player ) then
        if ( isPlayerInVehicle ( player ) ) then
            -- conver the ID to a number
            id = tonumber ( id )
            -- get the player's vehicle
            local vehicle = getPlayerOccupiedVehicle ( player )
            -- add the requested upgrade to the vehicle
            local success = addVehicleUpgrade ( vehicle, id )
            if ( success ) then
                -- get the name of the upgrade and display it
                outputConsole ( getVehicleUpgradeSlotName ( id ) .. " upgrade added.", player )
            else
                outputConsole ( "Failed to add upgrade.", player )
            end
        else
            outputConsole ( "You must be in a vehicle!", player )
        end
    end
end

See Also