Jump to content

Trying to understand the code, and need help


SuperM

Recommended Posts

Hello there, I'm trying to understand part of the code, can please someone help me and tell me what is what please. 

The part of the code is the following:

 

function updateItems()
	guiGridListClear(shop_gui.gridlist[2])
	local category = guiGridListGetItemText(shop_gui.gridlist[1],guiGridListGetSelectedItem(shop_gui.gridlist[1]))
		if (category ~= "") then
			for i,v in pairs(shop_items) do
				if (i == shop_type) then
					for i,v in pairs(v) do
						if (i == shop_marker) then
							for i,v in ipairs(v[category]) do
								local row = guiGridListAddRow(shop_gui.gridlist[2])
								guiGridListSetItemText(shop_gui.gridlist[2], row, 1, exports.text:getTextClient(v[1]), false, false)
								guiGridListSetItemText(shop_gui.gridlist[2], row, 2, v[3], false, false)
								guiGridListSetItemData(shop_gui.gridlist[2], row, 2, {v[2],v[3],v[1]})
							end
						end
					end	
				end
			end
		end
	end

Now the part that I can't understand is the following:

 

guiGridListSetItemText(shop_gui.gridlist[2], row, 1, exports.text:getTextClient(v[1]), false, false)
guiGridListSetItemText(shop_gui.gridlist[2], row, 2, v[3], false, false)
guiGridListSetItemData(shop_gui.gridlist[2], row, 2, {v[2],v[3],v[1]})

 

What are those v[3] and v[2] and v[1] ??

Link to comment

You have

for i,v in pairs(shop_items) do

on line 5. The 'v' letter or variable represents an element from the 'shop_items' list. Maybe that element is an array so you have to access its members by doing v[1], v[2], etc. Try to use

outputChatBox(v[2])

and see what appears in the chat. Same for v[1] and v[2].

 

Here's an example:
You have a list of PC parts called 'shop_items'. Each part from the list is marked with the letter 'v'. Every PC part has a model number, name, manufacturer, etc. So if the first information is the name of the part you simply write v[1] and you get the name of the PC part. If you want the model number, you type v[2] and if you want the manufacturer you type v[3] and so on.

Edited by koragg
  • Like 1
Link to comment

I got your point, now I have a Admin panel where I want to add custom functions in order to work completely with the mod and I have the following function:

 

addEventHandler("onClientGUIComboBoxAccepted", giveWindowCombobox, function()
		guiGridListClear(giveWindowGridlist)
		local text = guiComboBoxGetItemText(giveWindowCombobox,guiComboBoxGetSelected(giveWindowCombobox))
		outputDebugString(text)
		for i,item in ipairs(items[text]) do
			local row = guiGridListAddRow(giveWindowGridlist)
			Translated = exports.text:getLanguageTextClient(item)
			Original = item
			guiGridListSetItemText(giveWindowGridlist, row, 1, Translated, false, false)
			guiGridListSetItemText(giveWindowGridlist, row, 2, Original, false, false)
			outputChatBox("Translated Item"..Translated)
			outputChatBox("Original Item"..Original)
		end
	end)

And the "outpuChatBox" shows me the correct values, the one I need and the ones I want the "Translated" and the "Original"
I think the code above has no problem but I'm not sure.

 

	function give()
		if (guiGridListGetSelectedItem(giveWindowGridlist) ~= -1) then
			local Translated = guiGridListGetItemText(giveWindowGridlist,guiGridListGetSelectedItem(giveWindowGridlist),1)
			local Original = guiGridListGetItemText(giveWindowGridlist,guiGridListGetSelectedItem(giveWindowGridlist),2)
			outputDebugString("1-After T- "..Translated)
			outputDebugString("2-After O- "..Original)
			
			
			local quantity = guiGetText(giveWindowEditboxQuant)
			local selectedPlayer = getPlayerFromName(guiGridListGetItemText(gridlistPlayers1,Original,2))
			if selectedPlayer then
				triggerServerEvent("giveEvent",localPlayer,selectedPlayer,Original,quantity)
			end
		end
	end

 

On this part of code the only one that workd is the "Translated" at least from what I got from the debugstring the first debug shows me the correct value and the second debug it's just empy does not show any text after "2-After O-", do you know what I am missing here? Why can I get the translated one and can't get the original?

@koragg
 

Link to comment

Also if I add something like this:

 

guiGridListSetItemText(giveWindowGridlist, row, 1, Translated, false, false)
guiGridListSetItemText(giveWindowGridlist, row, 2, Original, false, false)
guiGridListSetItemData(giveWindowGridlist, row, 2, {Translated,Original})

and then on the other code something like this:

 

local Translated = guiGridListGetItemText(giveWindowGridlist,guiGridListGetSelectedItem(giveWindowGridlist),1)
local test1,test2 = unpack(guiGridListGetItemData(giveWindowGridlist, guiGridListGetSelectedItem(giveWindowGridlist),2))

I will get the following error:  Bad argument #1 'unpack' (table expected, got nil)

Edited by SuperM
Link to comment

Try like this maybe?

guiGridListGetItemText(giveWindowGridlist,guiGridListGetSelectedItem(giveWindowGridlist))

As guiGridListGetItemText accepts 3 arguments which are gridList, rowIndex and columnIndex but the function guiGridListGetSelectedItem returns two things according to MTA wiki: Returns the row and column indexes of the selected item

Not sure but play around with it as it's hard to guess what's wrong as I can't test it myself. Though if this is the case I'm not sure why the first one works fine for the Translated variable.

Link to comment
10 minutes ago, koragg said:

Try like this maybe?


guiGridListGetItemText(giveWindowGridlist,guiGridListGetSelectedItem(giveWindowGridlist))

As guiGridListGetItemText accepts 3 arguments which are gridList, rowIndex and columnIndex but the function guiGridListGetSelectedItem returns two things according to MTA wiki: Returns the row and column indexes of the selected item

Not sure but play around with it as it's hard to guess what's wrong as I can't test it myself. Though if this is the case I'm not sure why the first one works fine for the Translated variable.

By leaving it like that, in also only shows the Translated one. Already tested with a lot of ways but not sure why I'm not getting the Original value.

But thanks for trying to 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...