TheGamingMann Posted March 23, 2014 Posted March 23, 2014 (edited) 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 March 24, 2014 by Guest
xXMADEXx Posted March 23, 2014 Posted March 23, 2014 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 }, " | " ) )
TheGamingMann Posted March 23, 2014 Author Posted March 23, 2014 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.
Castillo Posted March 23, 2014 Posted March 23, 2014 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.
TheGamingMann Posted March 24, 2014 Author Posted March 24, 2014 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"/>
Castillo Posted March 24, 2014 Posted March 24, 2014 Oh, sorry, I just noticed the problem, copy my code again.
TheGamingMann Posted March 24, 2014 Author Posted March 24, 2014 Sweet it works. Thanks Solidsnake. Now do you think you can point me in the right direction for the vehicle ID issue.
Castillo Posted March 24, 2014 Posted March 24, 2014 getVehicleModelFromName gets the vehicle model from a vehicle name, and you gave as argument a vehicle element.
TheGamingMann Posted March 24, 2014 Author Posted March 24, 2014 Oh okay so what can I use to get the vehicle name that the player is sitting in?
WhoAmI Posted March 24, 2014 Posted March 24, 2014 Just change into local id = getElementModel ( vehicle )
TheGamingMann Posted March 24, 2014 Author Posted March 24, 2014 Ohh okay. Thanks. I must have skimmed past that in the Wiki.. My bad. Thanks guys!
TheGamingMann Posted March 24, 2014 Author Posted March 24, 2014 Hmm. I just tried the getElementModel but I get the invalid argument error in game.
WhoAmI Posted March 24, 2014 Posted March 24, 2014 Change 'source' to 'localPlayer' everywhere in this function.
TheGamingMann Posted March 24, 2014 Author Posted March 24, 2014 It works now. But still outputs an error when not in any car.
TheGamingMann Posted March 24, 2014 Author Posted March 24, 2014 So I have it set to a command of /w it will show the GUI when I type it in when within the vehicle. But outside any car it runs the chatbox thing that I set but runs the error to /scriptdebug 3 saying Bad Argument. But it works so thanks.
TheGamingMann Posted March 24, 2014 Author Posted March 24, 2014 Sweet thanks guys. Works like a charm!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now