Jump to content

[SOLVED] Random info pulled from XML/Getting Players Veh ID


Recommended Posts

I have yet to make a script that does this but I would like some help being pushed in the right direction. I have this script and I want to pull things from an XML file. (I can do that part.) but I only want 3 random things showing in the gridlist not all of them. I am not sure how to do this.

One other issue I am having is trying to get the ID of a vehicle. Here is the code I am using now:

function showLoad() 
    local vehicle = getPedOccupiedVehicle(source) 
    local id = getVehicleModelFromName(vehicle) 
    if id == 403 then 
        if not guiGetVisible(wdwLoad) then 
            guiSetVisible(wdwLoad, true) 
            showCursor(true, true) 
        end 
    else 
        outputChatBox("you are not in a car!", source) 
    end 
end 
addCommandHandler("work", showLoad) 
addCommandHandler("w", showLoad) 

Edited by Guest
Link to comment

You need to put them into a table, and you can use this function:

function table.rand ( tb ) 
    local d = { } 
    for i, v in pairs ( tb ) do 
        table.insert ( d, v ) 
    end 
    return d [ math.random ( #d ) ] 
end 

Example:

  
local data = {  900, 950, 364981, 3591, 5953, 1656, 5621 } 
local value1 = table.rand ( data ) 
local value2 = table.rand ( data ) 
local value3 = table.rand ( data ) 
outputChatBox ( table.concat ( { value1, value2, value3 }, " | " ) ) 
  

Link to comment

Sorry. I am quite new to scripting MTA. I have loaded the XML file and filled the list like this:

function createLoad() 
    local screenW, screenH = guiGetScreenSize() 
    wdwLoad = guiCreateWindow((screenW-609)/2, (screenH-180)/2, 609, 180, "", false) 
    guiWindowSetSizable(wdwLoad, false) 
    glistLoadList = guiCreateGridList((609-547)/2, (180-112)/2, 547, 112, false, wdwLoad) 
    guiGridListAddColumn(glistLoadList, "Cargo", 0.25) 
    guiGridListAddColumn(glistLoadList, "Pickup", 0.25) 
    guiGridListAddColumn(glistLoadList, "Drop Off", 0.25) 
    guiGridListAddColumn(glistLoadList, "Pay", 0.25) 
    btnSelect = guiCreateButton(249, 150, 111, 20, "Select", false, wdwLoad) 
     
    --addEventHandler("onClientGUIClick", btnSelect, selectLoad, false) 
     
    fillLoadList() 
end 
  
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), 
    function() 
        createLoad() 
        guiSetVisible(wdwLoad, false) 
    end 
) 
  
function showLoad() 
    if not guiGetVisible(wdwLoad) then 
        guiSetVisible(wdwLoad, true) 
        showCursor(true, true) 
    end 
end 
addCommandHandler("work", showLoad) 
addCommandHandler("w", showLoad) 
  
function hideLoad() 
    guiSetVisible(wdwLoad, false) 
    showCursor(false, false) 
end 
  
function fillLoadList() 
    local file = xmlLoadFile("loads.xml") 
    if file then 
        for _, loads in ipairs (xmlNodeGetChildren(file)) do 
            local row = guiGridListAddRow(glistLoadList) 
            local cargo = xmlNodeGetAttribute(loads, "cargo") 
            local pickup = xmlNodeGetAttribute(loads, "pickup") 
            local dropoff = xmlNodeGetAttribute(loads, "dropoff") 
            local startx = xmlNodeGetAttribute(loads, "startx") 
            local starty = xmlNodeGetAttribute(loads, "starty") 
            local startz = xmlNodeGetAttribute(loads, "startz") 
            local endx = xmlNodeGetAttribute(loads, "endx") 
            local endy = xmlNodeGetAttribute(loads, "endy") 
            local endz = xmlNodeGetAttribute(loads, "endz") 
            local pay = xmlNodeGetAttribute(loads, "pay") 
            guiGridListSetItemText(glistLoadList, row, 1, cargo, false, false) 
            guiGridListSetItemData(glistLoadList, row, 1, {startx, starty, startz}) 
            guiGridListSetItemText(glistLoadList, row, 2, pickup, false, false) 
            guiGridListSetItemText(glistLoadList, row, 3, dropoff, false, false) 
            guiGridListSetItemText(glistLoadList, row, 4, pay, false, false) 
        end 
        xmlUnloadFile(file) 
    end 
end 

But it will fill it with the whole XML file. I would only like 3 random ones. How can I go about it now? Sorry if this is bugging you but I am quite new so.

Link to comment
function fillLoadList ( ) 
    local file = xmlLoadFile ( "loads.xml" ) 
    if ( file ) then 
        local childs = xmlNodeGetChildren ( file ) 
        local randomChilds = { } 
        for index = 1, 3 do 
            table.insert ( randomChilds, childs [ math.random ( #childs ) ] ) 
        end 
  
        for _, loads in ipairs ( randomChilds ) do 
            local row = guiGridListAddRow ( glistLoadList ) 
            local attrs = xmlNodeGetAttributes ( loads ) 
            local endx = attrs.endx 
            local endy = attrs.endy 
            local endz = attrs.endz 
            guiGridListSetItemText ( glistLoadList, row, 1, attrs.cargo, false, false ) 
            guiGridListSetItemData ( glistLoadList, row, 1, { attrs.startx, attrs.starty, attrs.startz } ) 
            guiGridListSetItemText ( glistLoadList, row, 2, attrs.pickup, false, false ) 
            guiGridListSetItemText ( glistLoadList, row, 3, attrs.dropoff, false, false ) 
            guiGridListSetItemText ( glistLoadList, row, 4, attrs.pay, false, false ) 
        end 
        xmlUnloadFile ( file ) 
    end 
end 

Try it.

Link to comment

I tried that and now it is just loading the last item in the loads.xml. Here is the new code. And the XML file.

c_load.lua

function fillLoadList() 
    local file = xmlLoadFile("loads.xml") 
    if (file) then 
        local childs = xmlNodeGetChildren(file) 
        local randomChilds = {} 
        for index = 1, 3 do 
            table.insert(randomChilds, childs[#childs]) 
        end 
        for _, loads in ipairs (randomChilds) do 
            local row = guiGridListAddRow(glistLoadList) 
            local attrs = xmlNodeGetAttributes(loads) 
            local startx = attrs.startx 
            local starty = attrs.starty 
            local startz = attrs.startz 
            local endx = attrs.endx 
            local endy = attrs.endy 
            local endz = attrs.endz 
            guiGridListSetItemText(glistLoadList, row, 1, attrs.cargo, false, false) 
            guiGridListSetItemData(glistLoadList, row, 1, {attrs.startx, attrs.starty, attrs.startz}) 
            guiGridListSetItemText(glistLoadList, row, 2, attrs.pickup, false, false) 
            guiGridListSetItemText(glistLoadList, row, 3, attrs.dropoff, false, false) 
            guiGridListSetItemText(glistLoadList, row, 4, attrs.pay, false, false) 
        end 
        xmlUnloadFile(file) 
    end 
end 

loads.xml

    "Food1" pickup="Here" dropoff="There" startx="100" starty="100" startz="100" endx="101" endy="101" endz="101" pay="100"/> 
    "Food2" pickup="Here" dropoff="There" startx="100" starty="100" startz="100" endx="101" endy="101" endz="101" pay="100"/> 
    "Food3" pickup="Here" dropoff="There" startx="100" starty="100" startz="100" endx="101" endy="101" endz="101" pay="100"/> 
    "Food4" pickup="Here" dropoff="There" startx="100" starty="100" startz="100" endx="101" endy="101" endz="101" pay="100"/> 
    "Food5" pickup="Here" dropoff="There" startx="100" starty="100" startz="100" endx="101" endy="101" endz="101" pay="100"/> 
    "Food6" pickup="Here" dropoff="There" startx="100" starty="100" startz="100" endx="101" endy="101" endz="101" pay="100"/> 

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