Jump to content

GRIDLIST - Category does not expand


Madalin2009

Recommended Posts

Posted

I've made a gridlist panel for spawning vehicles using the tutorial on the Wiki and my personal GUI that I made using the GUI editor.

However, when I double-click one of the categories, it doesn't expand. What is wrong? Thank you.

You can find the specified clientside scripts (panel.lua, vehicles.xml) in the following package:

http://www.mediafire.com/?qxbv511owfufx13

-------

EDIT:

panel.lua:

addEventHandler("onClientResourceStart",resourceRoot, 
    function() 
        wdwVehicles = guiCreateWindow(295,166,643,524,"Vehicles - Multi Theft Awesome [v0.01]",false) 
        guiWindowSetSizable(wdwVehicles,false) 
        wdwVehicles_Image = guiCreateStaticImage(9,21,625,494,"images/vehicles.png",false,wdwVehicles) 
        wdwVehicles_objGridlist = guiCreateGridList(51,130,517,296,false,wdwVehicles_Image) 
        -- guiGridListSetSelectionMode(wdwVehicles_objGridlist,2) 
        -- add a "Vehicle" and a "Type" collumn to the gridlist 
        guiGridListAddColumn(wdwVehicles_objGridlist,"Vehicle",0.2) 
        guiGridListAddColumn(wdwVehicles_objGridlist,"Type",0.2) 
        -- set the default width of the columns to roughly half the size of the gridlist (each) 
        guiGridListSetColumnWidth(wdwVehicles_objGridlist,1,0.4,true) 
        guiGridListSetColumnWidth(wdwVehicles_objGridlist,2,0.5,true) 
        wdwVehicles_buttonOk = guiCreateButton(52,440,242,40,"Okay! I've chosen a vehicle.",false,wdwVehicles_Image) 
        guiSetAlpha(wdwVehicles_buttonOk,1) 
        guiSetFont(wdwVehicles_buttonOk,"default-bold-small") 
        wdwVehicles_buttonCancel = guiCreateButton(325,440,242,40,"Sorry, but I don't need a vehicle.",false,wdwVehicles_Image) 
        guiSetAlpha(wdwVehicles_buttonCancel,1) 
        guiSetFont(wdwVehicles_buttonCancel,"default-bold-small") 
        addEventHandler ( "onClientGUIClick", wdwVehicles_buttonCancel, closeVehicleSelection) 
        addEventHandler ( "onClientGUIClick", wdwVehicles_buttonOk, createVehicleHandler, false) 
        guiSetVisible(wdwVehicles, false) 
        -- this will add all the vehicle options onto the gridlist, it is explained later in this tutorial 
        populateGridlist() 
    end 
) 
  
     
function closeVehicleSelection() 
    guiSetVisible(wdwVehicles, false) 
    showCursor(false) 
    guiSetInputEnabled(false) 
end 
  
function showVehicleSelection() 
    -- if the window isnt visible, show it 
    if not guiGetVisible(wdwVehicles) then 
        guiSetVisible(wdwVehicles,true) 
  
        showCursor(true,true) 
    end 
end 
  
function populateGridlist() 
    local rootnode = xmlLoadFile("vehicles.xml") 
  
    -- create a blank global table to store our imported data 
    vehicleTable = {} 
  
    if rootnode then         
        for _,group in ipairs(xmlNodeGetChildren(rootnode)) do 
            -- create an entry in the gridlist for every vehicle "group" 
            local row = guiGridListAddRow(wdwVehicles_objGridlist) 
  
            local name = xmlNodeGetAttribute(group,"type") 
  
            guiGridListSetItemText(wdwVehicles_objGridlist,row,1,name,false,false)   
  
            -- add an entry containing the group name into the table 
            vehicleTable[name] = {} 
  
            -- we will use the custom data "header" to indicate that this entry can be expanded/collapsed 
            guiGridListSetItemData(wdwVehicles_objGridlist,row,1,"header")   
  
            -- then, for every group that we find, loop all of its children (the vehicle nodes) and store them in a table 
            for _,vehicle in ipairs(xmlNodeGetChildren(group)) do 
                local vname = xmlNodeGetAttribute(vehicle,"name") 
                local id = xmlNodeGetAttribute(vehicle,"id")             
  
                -- insert both the vehicle id and the vehicle description into the table 
                table.insert(vehicleTable[name],{id,vname}) 
            end 
        end  
  
        -- set element data on the gridlist so we know which group is currently showing 
        setElementData(wdwVehicles_objGridlist,"expanded","none") 
  
        -- unload the xml file now that we are finished 
        xmlUnloadFile(rootnode) 
    end 
end 
  
function createVehicleHandler(button,state) 
    if button == "left" and state == "up" then 
        -- get the selected item in the gridlist 
        local row,col = guiGridListGetSelectedItem(wdwVehicles_objGridlist) 
  
        -- if something is selected 
        if row and col and row ~= -1 and col ~= -1 then 
            -- get the vehicle id data from the gridlist that is selected 
            local selected = guiGridListGetItemData(wdwVehicles_objGridlist,row,col) 
  
            -- make sure the vehicle id is a number not a string 
            selected = tonumber(selected) 
  
            -- get the players position and rotation 
            local rotz = getPedRotation(getLocalPlayer()) 
            local x,y,z = getElementPosition(getLocalPlayer()) 
            -- find the position directly infront of the player 
            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 
                -- trigger the server 
                triggerServerEvent("createVehicleFromGUI",getRootElement(),selected,x,y,z) 
  
                -- hide the gui and the cursor 
                guiSetVisible(windowVehicleSelection,false) 
                showCursor(false,false) 
            else 
                outputChatBox("Invalid arguments.") 
            end 
        else 
            -- otherwise, output a message to the player 
            outputChatBox("Please select a vehicle.") 
        end 
    end 
end 
  
function createMyVehicle(vehicleid,x,y,z) 
    -- check all the arguments exist 
    if vehicleid and x and y and z then 
        createVehicle(vehicleid,x,y,z) 
    end 
end 
  
function changeGridlistState(group) 
    if group then 
        -- if the group is already expanded, we want to collapse it 
        if getElementData(wdwVehicles_objGridlist,"expanded") == group then 
            -- first, we clear all previous data from the gridlist 
            guiGridListClear(wdwVehicles_objGridlist) 
  
            -- now, loop all the group entries in the vehicle table that we created earlier 
            for group,_ in pairs(vehicleTable) do 
                -- add each group to the list and mark them with "header"            
                local row = guiGridListAddRow(wdwVehicles_objGridlist) 
  
                guiGridListSetItemText(wdwVehicles_objGridlist,row,1,group,false,false)  
                guiGridListSetItemData(wdwVehicles_objGridlist,row,1,"header")   
            end 
  
            -- update the data to indicate that no groups are currently expanded 
            setElementData(wdwVehicles_objGridlist,"expanded","none") 
  
        -- otherwise, we want to expand it 
        else 
            guiGridListClear(wdwVehicles_objGridlist) 
  
            local row = guiGridListAddRow(wdwVehicles_objGridlist) 
  
            guiGridListSetItemText(wdwVehicles_objGridlist,row,1,group,false,false)  
            guiGridListSetItemData(wdwVehicles_objGridlist,row,1,"header")               
  
            -- loop every vehicle in the specified groups table 
            for _,vehicle in ipairs(vehicleTable[group]) do 
                -- add them to the gridlist and set the vehicle id as data 
                row = guiGridListAddRow(wdwVehicles_objGridlist) 
  
                -- format a "-" into the string to make it visually easier to distinguish between groups and vehicles 
                guiGridListSetItemText(wdwVehicles_objGridlist,row,1,"- "..vehicle[2],false,false)   
                guiGridListSetItemText(wdwVehicles_objGridlist,row,2,getVehicleType" class="kw2">getVehicleType" class="kw2">getVehicleType(tonumber(vehicle[1])),false,false)   
  
                guiGridListSetItemData(wdwVehicles_objGridlist,row,1,tostring(vehicle[1])) 
            end  
  
            -- update the data to indicate which group is currently expanded 
            setElementData(wdwVehicles_objGridlist,"expanded",group)             
        end 
    end 
end 
 function processDoubleClick(button,state) 
    if button == "left" and state == "up" then 
        local row,col = guiGridListGetSelectedItem(wdwVehicles_objGridlist) 
  
        -- if something in the gridlist has been selected 
        if row and col and row ~= -1 and col ~= -1 then      
            -- if it is a header 
            if guiGridListGetItemData(wdwVehicles_objGridlist,row,col) == "header" then 
                local selected = guiGridListGetItemText(wdwVehicles_objGridlist,row,col) 
  
                -- call the function to collapse or expand the menu and pass which section it is as an argument 
                changeGridlistState(selected) 
            end 
        end 
    end 
end 
addEvent("createVehicleFromGUI",true) 
addEventHandler("createVehicleFromGUI",root,createMyVehicle) 
-- attach the onClientGUIDoubleClick event to the gridlist and set it to call processDoubleClick 
addEventHandler("onClientGUIDoubleClick",wdwVehicles_objGridlist,processDoubleClick,false) 
  
-- add the command /vehicleselection and set it to call the showVehicleSelection function 
addCommandHandler("v",showVehicleSelection) 
 

vehicles.xml:

<root> 
    <group type="Bikes"> 
        <vehicle id="581" name="BF-400"/> 
        <vehicle id="463" name="Freeway"/> 
        <vehicle id="481" name="BMX"/> 
    </group> 
    <group type="Boats"> 
        <vehicle id="472" name="Coastguard"/> 
        <vehicle id="452" name="Speeder"/> 
    </group> 
    <group type="Helicopters"> 
        <vehicle id="487" name="Maverick"/> 
        <vehicle id="469" name="Sparrow"/>   
    </group> 
    <group type="Planes"> 
        <vehicle id="593" name="Dodo"/> 
        <vehicle id="513" name="Stuntplane"/>    
    </group> 
    <group type="Sports Cars"> 
        <vehicle id="565" name="Flash"/> 
        <vehicle id="559" name="Jester"/> 
        <vehicle id="477" name="ZR-350"/>    
    </group> 
    <group type="2-Door"> 
        <vehicle id="474" name="Hermes"/> 
        <vehicle id="475" name="Sabre"/> 
    </group> 
    <group type="Emergency"> 
        <vehicle id="416" name="Ambulance"/> 
        <vehicle id="599" name="Police ranger"/> 
    </group> 
    <group type="Industrial"> 
        <vehicle id="573" name="Dune"/> 
        <vehicle id="552" name="Utility van"/>   
    </group> 
    <group type="Misc"> 
        <vehicle id="457" name="Caddy"/> 
        <vehicle id="583" name="Tug"/> 
    </group> 
</root> 

Posted

line 191 in panel.lua, move the double click event handler into the function that you create the gui in. Currently you are trying to add an event handler onto a gridlist that doesn't yet exist

Do '/debugscript 3' in-game (you need to be logged in) and you will see these errors for yourself

Posted

23112111458-orig.jpg

That's when I get now.

The categories expand, but when I try to spawn a vehicle by either doubleclicking on it or clicking the 'OK' button, I get the last error/warning shown in the image.

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