DriFtyZ Posted May 17, 2017 Share Posted May 17, 2017 Hello community, i started learning lua few days ago and i want to create a carshop gui script but with custom vehicles and i want the gui to load their tables automatically but ican't figure it out any help? heres part of the script below(rest is useless in current situation) function createVehicleList () Carshop.gridlist[1] = guiCreateGridList(24, 37, 465, 397, false, Carshop.window) Carshop.column[1] = guiGridListAddColumn(Carshop.gridlist[1], "Vehicle", 0.5) Carshop.column[2] = guiGridListAddColumn(Carshop.gridlist[1], "Price", 0.2) if ( Carshop.column[1] ) then for a, b in pairs( VehiclesNames ) do local carName = b local carListRow = guiGridListAddRow ( Carshop.gridlist[1] ) guiGridListSetItemText ( Carshop.gridlist[1], carListRow, Carshop.column[1], carName , false, false ) end for c, d in pairs( VehiclePrices ) do local carPrice = d guiGridListSetItemText ( Carshop.gridlist[1], carListRow, Carshop.column[2], carPrice , false, false ) end end end heres my table values VehiclesNames = { silvia_s15 = "Nissan Silvia S15", sx240 = "Nissan 240SX", supra_mk4 = "Toyota Supra MK4", skyline_r32 = "Nissan Skyline R32", skyline_r34 = "Nissan Skyline R34", } VehiclePrices = { silvia_s15 = "40000", sx240 = "12000", supra_mk4 = "60000", skyline_r32 = "35000", skyline_r34 = "60000", } and a screenshot to understand a bit where i am stuck: Link to comment
pa3ck Posted May 17, 2017 Share Posted May 17, 2017 That's because of the scope of the variable "carListRow". You declared it in the first loop as a local variable, that means it will only be available in that loop. When you add a new row, you can update the name and the price at the same time. Try this: VehiclesNames = { silvia_s15 = {"Nissan Silvia S15", 40000}, sx240 = {"Nissan 240SX", 12000}, supra_mk4 = {"Toyota Supra MK4", 60000}, skyline_r32 = {"Nissan Skyline R32", 35000}, skyline_r34 = {"Nissan Skyline R34", 60000}, } function createVehicleList () Carshop.gridlist[1] = guiCreateGridList(24, 37, 465, 397, false, Carshop.window) Carshop.column[1] = guiGridListAddColumn(Carshop.gridlist[1], "Vehicle", 0.5) Carshop.column[2] = guiGridListAddColumn(Carshop.gridlist[1], "Price", 0.2) if ( Carshop.column[1] ) then for a, b in pairs( VehiclesNames ) do local carName = b[1] -- the first value is the name local carPrice = b[2] -- the second value is the price local carListRow = guiGridListAddRow ( Carshop.gridlist[1] ) guiGridListSetItemText ( Carshop.gridlist[1], carListRow, Carshop.column[1], carName , false, false ) guiGridListSetItemText ( Carshop.gridlist[1], carListRow, Carshop.column[2], carPrice , false, false ) end end end Link to comment
DriFtyZ Posted May 17, 2017 Author Share Posted May 17, 2017 Thank you so much for your help i have another one problem for noobs like me how can i script the buy vehicle button to pop up only when i select something from the row i used the following function onClientGUIClick but still no sucess function buyVehicle (localPlayer) local carSelected = guiGridListGetSelectedItem ( Carshop.gridlist[1] ) if not carSelected == false then guiSetVisible (Confirmation.window, true) else return end end Link to comment
pa3ck Posted May 17, 2017 Share Posted May 17, 2017 (edited) You will need to attach your function to an event like onClientGUIClick like: addEventHandler("onClientGUIClick", resourceRoot, function(btn, state) if btn == "left" and state == "up" then local selRow, selInd = guiGridListGetSelectedItem(Carshop.gridlist[1]) if( selRow ~= -1 ) then guiSetEnabled( YOUR_GUI_BUTTON, true) else guiSetEnabled( YOUR_GUI_BUTTON, false) end end end) Make sure you disable the button when creating it so it will be enabled by the script when the user clicks a row Edited May 17, 2017 by pa3ck Link to comment
DriFtyZ Posted May 17, 2017 Author Share Posted May 17, 2017 Thank you, if you mind can you explain to me what are btn, state, sel, selind? Link to comment
pa3ck Posted May 17, 2017 Share Posted May 17, 2017 addEventHandler("onClientGUIClick", resourceRoot, function(btn, state) --[[ Variable btn, state | If you go to the wiki (wiki.multitheftauto.com), you can see the list of parameters being passed to this function You don't have to put them all in, but if you will need to use them, you will want to put them in You can give them any name you want, but the order cannot be changed, every single parameter is explained on the wiki page btn -> ' the name of the button which will be clicked , it can be left, right, middle ' --> basically which mouse button was pressed state -> ' the state of the mouse button, will be down if the mouse button was pushed, or up if it was released. ' --> state of the pressed key (only 'up' is functional ATM) If you look at this event on the wiki (https://wiki.multitheftauto.com/wiki/OnClientGUIClick), you will see there are more parameters, but we don't need them, therefore I didn't put them ]] if btn == "left" and state == "up" then -- so if the user clicked the left mouse button and released it --[[ Again, the wiki says (wiki.multitheftauto.com/wiki/GuiGridListGetSelectedItem) that this function has 2 return values ' Returns the row and column indexes of the selected item if the specified grid list is valid and has a selected item, (-1, -1) if no item is selected, false otherwise. ' (quoted from wiki ) ]] local selRow, selInd = guiGridListGetSelectedItem(Carshop.gridlist[1]) -- as explained above, if nothing is selected, this will return -1 if( selRow ~= -1 ) then -- check if it's not -1 (if it's not, it means something is selected) guiSetEnabled( YOUR_GUI_BUTTON, true) -- enable the button, we know for fact that something is selected else guiSetEnabled( YOUR_GUI_BUTTON, false) -- disable the button, we know for fact that nothing is selected end end end) Hope you understood everything, if not, don't be shy to ask for further help. Link to comment
DriFtyZ Posted May 17, 2017 Author Share Posted May 17, 2017 Understood sir one last ask-for-help here how can i trigger the onClientGUIClick button if its created inside an function? thanks for your help in advance lets say the events are addEventHandler ("onClientGUIClick", Confirmation.button.no, cancel, false ) addEventHandler ("onClientGUIClick", Confirmation.button.yes, buyVehicle, false ) I took your code to make a confirmation window appear with the buttons inside function confirmWindow(btn, state) if btn == "left" and state == "up" then local selRow, selInd = guiGridListGetSelectedItem(Carshop.gridlist[1]) local vehName = guiGridListGetItemText ( Carshop.gridlist[1], selRow, selInd ) local vehPrice = guiGridListGetItemText ( Carshop.gridlist[1], selRow, 2 ) if( selRow ~= -1 ) then Confirmation.window = guiCreateWindow(699, 412, 328, 164, "Car Shop", false) Confirmation.label = guiCreateLabel(54, 37, 226, 55, "Are you sure you want to buy "..vehName.." for $"..vehPrice..".", false, Confirmation.window) guiLabelSetHorizontalAlign(Confirmation.label, "left", true) Confirmation.button.no = guiCreateButton(195, 104, 85, 31, "No", false, Confirmation.window) -- (No Button) When i click it an event should be triggered? guiSetProperty(Confirmation.button.no, "NormalTextColour", "FFAAAAAA") Confirmation.button.yes = guiCreateButton(52, 104, 85, 31, "Yes", false, Confirmation.window) -- (Yes Button) same here when i click a whole other story script will run the vehicle in database, and blah blah (not written yet) guiSetProperty(Confirmation.button.yes, "NormalTextColour", "FFAAAAAA") else guiSetEnabled( Carshop.button.buyVehicle, false ) end end end Sorry if i ask a lot but this is my day 4 in scripting and the whole days im in front of the wikies looking for answer in what i do Link to comment
pa3ck Posted May 17, 2017 Share Posted May 17, 2017 You can create it anywhere as long as 1. you are sure when it triggers, the button exists 2. it has access to the variable for that given button Link to comment
DriFtyZ Posted May 17, 2017 Author Share Posted May 17, 2017 (edited) lets say the following script doesn't work Confirmation = { button = {}, } function button () Confirmation.window = guiCreateWindow(699, 412, 328, 164, "Car Shop", false) Confirmation.button.no = guiCreateButton(195, 104, 85, 31, "No", false, Confirmation.window) end function cancel () guiGetVisible (Confirmation.window, false) end addEventHandler ("onClientGUIClick", Confirmation.button.no, cancel, false ) its an example but still doesn't work when i set the button inside a function Edited May 17, 2017 by DriFtyZ Link to comment
pa3ck Posted May 17, 2017 Share Posted May 17, 2017 See the second argument of the event handler is a GUI element, it doesn't refer to a variable, but an element. In other words, when the addEventHandler executes and the button is not created yet, it will not work. Try this: Confirmation = { button = {}, } function button () Confirmation.window = guiCreateWindow(699, 412, 328, 164, "Car Shop", false) Confirmation.button.no = guiCreateButton(195, 104, 85, 31, "No", false, Confirmation.window) end function cancel () -- check which GUI was clicked if source == Confirmation.button.no then -- 'source' is a variable that is always present in this event and it is the GUI element that was clicked guiGetVisible (Confirmation.window, false) end end addEventHandler ("onClientGUIClick", resourceRoot, cancel, false ) -- every GUI that is being clicked in the resourceRoot Link to comment
DriFtyZ Posted May 17, 2017 Author Share Posted May 17, 2017 badargument @ eventhandler expected element at argument 2 and got nil but both of the 2 gui elements are active and the names of the vars are these what is wrong Link to comment
pa3ck Posted May 17, 2017 Share Posted May 17, 2017 (edited) Well I have no idea how you got that error, because the 2nd argument is clearly resourceRoot which is definitely not nil. You shouldn't set the 4th argument in addEventHandler to false and you never actually hide the window. guiGetVisible =/= guiSetVisible, guiGetVisible returns true/false, does not do anything. This one works: Confirmation = { button = {}, } function button () Confirmation.window = guiCreateWindow(699, 412, 328, 164, "Car Shop", false) Confirmation.button.no = guiCreateButton(195, 104, 85, 31, "No", false, Confirmation.window) end function cancel () if source == Confirmation.button.no then guiSetVisible (Confirmation.window, false) end end button() addEventHandler ("onClientGUIClick", root, cancel) Edited May 17, 2017 by pa3ck Link to comment
DriFtyZ Posted May 17, 2017 Author Share Posted May 17, 2017 Sorry i meant guiSetVisible which is still.. im gonna try your new handler and btw what does the button() in bottom of script? Yes, somehow im still getting the nil warnings but the button works now, not only that im getting nil warnings almost in every guiSetVisible parameter i do but it works like a charm Link to comment
pa3ck Posted May 17, 2017 Share Posted May 17, 2017 Button is the function that creates the GUI. If you don't call it, the GUI won't be created. You said the problem was at the eventHandler not with the guiSetVisible, what is the exact error you are getting and at which line? Link to comment
DriFtyZ Posted May 18, 2017 Author Share Posted May 18, 2017 Thanks for your reply i fixed the problem over the night should i ask you one more detail or make a new topic? cause i can't really understand how to make this thing work properly here, it has to do with the tables as that was my main problem Link to comment
pa3ck Posted May 18, 2017 Share Posted May 18, 2017 If its related to your main problem you can post it here, create a new one otherwise. Link to comment
DriFtyZ Posted May 18, 2017 Author Share Posted May 18, 2017 its related to first example you gave me addEventHandler("onClientGUIClick", resourceRoot, function(btn, state) if btn == "left" and state == "up" then local selRow, selInd = guiGridListGetSelectedItem(Carshop.gridlist[1]) if( selRow ~= -1 ) then guiSetEnabled( YOUR_GUI_BUTTON, true) else guiSetEnabled( YOUR_GUI_BUTTON, false) end end end) - if the left click is pressed and released - and if the gridlist (x) is selected - do this but if i click anywhere in a gui and the same row, index of the gridlist is selected it still excecutes the script i want somehow to end the loop if the user has the same thing selected already but i can't figure it out (lua tables thing got fixed over the rest of my day, sorry if i bother you too much) Link to comment
pa3ck Posted May 18, 2017 Share Posted May 18, 2017 Yes, it doesn't check if it's the same index or not. What you can do is check if the button is visible in the if statement with the selRow ~= -1 and only run it when it is not visible. Or you can also save the selInd into another variable (which is outside the onClientGUIClick handler) called something like lastIndex and compare the two indexes and only run the code when they are not the same. Link to comment
DriFtyZ Posted May 19, 2017 Author Share Posted May 19, 2017 okay, thanks for your help, appreciated Link to comment
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