Jump to content

[SNIPPET] Player and Vehicle Name AutoComplete


j3sus86

Recommended Posts

Auto-Complete player name and Vehicle Name from a sub string

Well I'm new to lua but i made these and i figured they might be useful to someone.. Sorry in advance if its coded poorly but i tried.

  
-- Completes Player Name From A Sub String 
-- returns for the last match found:  player, theName, and number of matches found 
function complete_PlayerName ( sub ) 
    local players = getElementsByType ( "player" ) 
    sub = string.lower(sub) 
    local success, target  
    local numfound = 0 
    for theKey, theFind in ipairs(players) do  
        success = string.find(string.lower(getClientName ( theFind ) ), sub ) 
        if success then  
            target = theFind 
            numfound = numfound + 1 
        end 
         
    end 
    return target, getClientName(target), numfound 
end 
  

  
-- Completes Vehicle Name From A Sub String 
-- returns the last match found: Name of Vehicle, number of matches, modelid 
function complete_VehicleName ( sub ) 
    local success, target, model 
    local numfound = 0 
    sub = string.lower(sub) 
    for i = 1, 211 do  
        if ( getVehicleNameFromID ( 400 + i ) ~= "" )  then 
            success = string.find(string.lower(getVehicleNameFromID ( 400 + i ) ), sub ) 
            if success then  target = getVehicleNameFromID ( 400 + i ) 
                numfound = numfound + 1 
                model = 400 + i 
            end 
        end 
    end          
    return target, numfound, model 
end 
  

  
-- Example of how its used 
function Cmd_Vname( thePlayer, command, sub ) 
    local theVehicleName, numfound, modelid = complete_VehicleName ( sub ) 
    if not theVehicleName then 
        outputChatBox ( "no matches found: (" .. sub .. ").", thePlayer ) 
    else 
        if numfound > 1 then  
            outputChatBox ( "multiple matches found (" .. numfound .. "). Please be more specific", thePlayer ) 
        else 
            outputChatBox ( "Vehicle Found: " .. theVehicleName .. " (" .. modelid .. ").", thePlayer ) 
        end 
    end 
end 
addCommandHandler("vname", Cmd_Vname) 
  
  

Edit - added weapon name autocomplete

  
-- Completes Weapon Name From A Sub String 
-- returns for the last match found: name, weaponid, number of matches 
function complete_WeaponName ( sub ) 
    local success, target, weaponid 
    local numfound = 0 
    sub = string.lower(sub)  
     
    for i = 1, 46 do  
        if ( getWeaponNameFromID ( i ) ~= false )then 
            success = string.find(string.lower(getWeaponNameFromID ( i ) ), sub ) 
            if success then target = getWeaponNameFromID ( i ) 
                numfound = numfound + 1 
                weaponid = i 
            end 
        end 
    end 
    return target, weaponid, numfound 
end 
  

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...