Jump to content

[HELP] Vehicle Gridlist


Backsage

Recommended Posts

Hey guys. I've been trying to follow this tutorial on gridlists: https://wiki.multitheftauto.com/wiki/Sc ... Tutorial_1

There's 3 ways to do it. The first 2 ways that I've done it don't work. It always gives me "Invalid Arguments." But the 3rd method using XML worked. (I'm still very confused on how gridlists work)

Anyway, here's the first way:

client:

  
----------------------------------------------------------------------------------------------------- 
-- VEHICLE GRIDLIST 
----------------------------------------------------------------------------------------------------- 
local vehicleSelectionTable = { 
        ["Sparrow"] = 469, 
        ["Stuntplane"] = 513, 
        ["BF-400"] = 581, 
        ["Freeway"] = 463, 
        ["Speeder"] = 452, 
        ["Jester"] = 559, 
        ["Sabre"] = 475, 
        ["Police Ranger"] = 599, 
        ["Utility Van"] = 552, 
        ["Tug"] = 583 
} 
  
function createVehicleSelection() 
  
    local sWidth, sHeight = guiGetScreenSize() 
     
    local Width, Height = 376, 259 
    local X = (sWidth/2) - (Width/2) 
    local Y = (sHeight/2) - (Height/2) 
    windowVehicleSelection = guiCreateWindow(X, Y, Width, Height, "Vehicle Selection Window", false) 
     
    gridlistVehicleSelection = guiCreateGridList(10, 26, 357, 192, false, windowVehicleSelection) 
    guiGridListAddColumn(gridlistVehicleSelection, "Vehicle", 0.2) 
    guiGridListAddColumn(gridlistVehicleSelection,"Type", 0.2) 
     
    guiGridListSetColumnWidth(gridlistVehicleSelection, 1, 0.4, true) 
    guiGridListSetColumnWidth(gridlistVehicleSelection, 1, 0.5, true) 
     
    buttonCreate = guiCreateButton(121, 227, 120, 20, "Create", false, windowVehicleSelection) 
     
    addEventHandler("onClientGUIClick", buttonCreate, createVehicleHandler, false) 
     
    guiSetVisible(windowVehicleSelection, false) 
     
    populateGridlist() 
end 
  
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), 
        function() 
                createVehicleSelection() 
        end 
) 
  
function showVehicleSelection() 
  
    if not guiGetVisible(windowVehicleSelection) then 
            guiSetVisible(windowVehicleSelection, true) 
             
            showCursor(true,true) 
    end 
end 
addCommandHandler("vehicleselection", showVehicleSelection) 
  
function populateGridlist() 
        for name, vehicle in pairs(vehicleSelectionTable) do 
            local row = guiGridListAddRow(gridlistVehicleSelection) 
              
             guiGridListSetItemText(gridlistVehicleSelection, row, 1, name, false, false) 
              
             guiGridListSetItemText(gridlistVehicleSelection, row, 2, getVehicleType(vehicle), false, false) 
              
             guiGridListSetItemText(gridlistVehicleSelection, row, 1, tostring(vehicle), false, false) 
        end 
end 
  
function createVehicleHandler(button,state) 
        if button == "left" and state == "up" then 
                local row, col = guiGridListGetSelectedItem(gridlistVehicleSelection) 
                 
                if row and col and row ~= -1 and col ~= -1 then 
                        local selected = guiGridListGetItemData(gridlistVehicleSelection, row, col) 
                         
                        selected = tonumber(selected) 
                         
                        local rotz = getPedRotation(getLocalPlayer()) 
                        local x,y,z = getElementPosition(getLocalPlayer()) 
                         
                        x = x + (math.cos (math.rad (rotz+90)) * 3) 
                        y = y + (math.sin (math.rad (rotz+90)) * 3) 
                         
                        if selected and x and y and z then 
                                triggerServerEvent("createVehicleFromGUI", getRootElement(), selected, x, y, z) 
                                 
                                guiSetVisible(windowVehicleSelection, false) 
                                showCursor(false, false) 
                        else 
                                outputChatBox("Invalid arguments.") 
                        end 
                else 
                        outputChatBox("Please select a vehicle.") 
                end 
        end 
end 
  

server:

  
----------------------------------------------------------------------- 
-- VEHICLE GRIDLIST 
----------------------------------------------------------------------- 
function createMyVehicle(vehicleid,x,y,z) 
        if vehicleid and x and y and z then 
                createVehicle(vehicleid, x, y, z) 
        end 
end 
  
addEvent("createVehicleFromGUI", true) 
addEventHandler("createVehicleFromGUI", root, createMyVehicle) 
  

Second way:

client:

  
----------------------------------------------------------------------------------------------------- 
-- VEHICLE GRIDLIST 
----------------------------------------------------------------------------------------------------- 
local vehicleSelectionTable = { 
        ["Sparrow"] = 469, 
        ["Stuntplane"] = 513, 
        ["BF-400"] = 581, 
        ["Freeway"] = 463, 
        ["Speeder"] = 452, 
        ["Jester"] = 559, 
        ["Sabre"] = 475, 
        ["Police Ranger"] = 599, 
        ["Utility Van"] = 552, 
        ["Tug"] = 583 
} 
  
function createVehicleSelection() 
  
    local sWidth, sHeight = guiGetScreenSize() 
     
    local Width, Height = 376, 259 
    local X = (sWidth/2) - (Width/2) 
    local Y = (sHeight/2) - (Height/2) 
    windowVehicleSelection = guiCreateWindow(X, Y, Width, Height, "Vehicle Selection Window", false) 
     
    gridlistVehicleSelection = guiCreateGridList(10, 26, 357, 192, false, windowVehicleSelection) 
    guiGridListAddColumn(gridlistVehicleSelection, "Vehicle", 0.2) 
    guiGridListAddColumn(gridlistVehicleSelection,"Type", 0.2) 
     
    guiGridListSetColumnWidth(gridlistVehicleSelection, 1, 0.4, true) 
    guiGridListSetColumnWidth(gridlistVehicleSelection, 1, 0.5, true) 
     
    buttonCreate = guiCreateButton(121, 227, 120, 20, "Create", false, windowVehicleSelection) 
     
    addEventHandler("onClientGUIClick", buttonCreate, createVehicleHandler, false) 
     
    guiSetVisible(windowVehicleSelection, false) 
     
    populateGridlist() 
end 
  
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), 
        function() 
                createVehicleSelection() 
        end 
) 
  
function showVehicleSelection() 
  
    if not guiGetVisible(windowVehicleSelection) then 
            guiSetVisible(windowVehicleSelection, true) 
             
            showCursor(true,true) 
    end 
end 
addCommandHandler("vehicleselection", showVehicleSelection) 
  
function populateGridlist() 
        local rootnode = xmlLoadFile("vehicles.xml") 
         
        if rootnode then 
            for _,group in ipairs(xmlNodeGetChildren(rootnode)) do 
                local row = guiGridListAddRow(gridlistVehicleSelection) 
                 
                local name = xmlNodeGetAttribute(group,"type") 
                guiGridListSetItemText(gridlistVehicleSelection,row,1,name,true,false) 
                 
                for _,vehicle in ipairs(xmlNodeGetChildren(group)) do 
                    row = guiGridListAddRow(gridlistVehicleSelection) 
                     
                    name = xmlNodeGetAttribute(vehicle, "name") 
                    local id = xmlNodeGetAttribute(vehicle, "id") 
                     
                    guiGridListSetItemText(gridlistVehicleSelection,row,1,name,false,false) 
                    guiGridListSetItemText(gridlistVehicleSelection,row,2,getVehicleType(tonumber(id)),false,false) 
                    guiGridListSetItemText(gridlistVehicleSelection,row,1,tostring(id), false, false) 
                end 
            end 
            xmlUnloadFile(rootnode) 
        end 
end 
  
function createVehicleHandler(button,state) 
        if button == "left" and state == "up" then 
                local row, col = guiGridListGetSelectedItem(gridlistVehicleSelection) 
                 
                if row and col and row ~= -1 and col ~= -1 then 
                        local selected = guiGridListGetItemData(gridlistVehicleSelection, row, col) 
                         
                        selected = tonumber(selected) 
                         
                        local rotz = getPedRotation(getLocalPlayer()) 
                        local x,y,z = getElementPosition(getLocalPlayer()) 
                         
                        x = x + (math.cos (math.rad (rotz+90)) * 3) 
                        y = y + (math.sin (math.rad (rotz+90)) * 3) 
                         
                        if selected and x and y and z then 
                                triggerServerEvent("createVehicleFromGUI", getRootElement(), selected, x, y, z) 
                                 
                                guiSetVisible(windowVehicleSelection, false) 
                                showCursor(false, false) 
                        else 
                                outputChatBox("Invalid arguments.") 
                        end 
                else 
                        outputChatBox("Please select a vehicle.") 
                end 
        end 
end 
  

server:

  
----------------------------------------------------------------------- 
-- VEHICLE GRIDLIST 
----------------------------------------------------------------------- 
function createMyVehicle(vehicleid,x,y,z) 
        if vehicleid and x and y and z then 
                createVehicle(vehicleid, x, y, z) 
        end 
end 
  
addEvent("createVehicleFromGUI", true) 
addEventHandler("createVehicleFromGUI", root, createMyVehicle) 
  

Any help?

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...