Jump to content

Two GUI panels in one function?


Cronoss

Recommended Posts

Well, I'm trying to make two different GUI panels, one of them opens when the player is in "Jefferson" and types the command "buy", and the other one opens when the player is in "Rodeo". Inside this panels there is a gridlist that is using two different tables:

vehCS = {
{"Manana", 10000, 410},
}

vehGT = {
{"ZR-350",230000, 477},
}

local distancePickup = 3

function detectPanel()
	local x,y,z = getElementPosition(localPlayer)
	local px1, py1, pz1 = 2131.8557128906,-1150.8621826172,24.111211776733
	local px2, py2, pz2 = 562.91925048828,-1293.4617919922,17.248237609863
	local gps = getZoneName(x,y,z)
	if (gps=="Jefferson") then
		if getDistanceBetweenPoints3D ( px1, py1, pz1, x,y,z) <= distancePickup then
			CScns()
		end
	else
		if getDistanceBetweenPoints3D ( px2, py2, pz2, x,y,z) <= distancePickup then
			GTcns()
		end
	end
end
addCommandHandler("buy", detectPanel)
addCommandHandler("BUY", detectPanel)

function CScns()

	screenW, screenH = guiGetScreenSize()
	showCursor(true)
	setElementFrozen(localPlayer, true)
    windowCS = guiCreateWindow(10, (screenH - 527) / 2, 330, 527,"Test1", false) 
    guiWindowSetSizable(windowCS, false)

    list = guiCreateGridList(12, 28, 304, 437, false, windowCS)
    guiGridListAddColumn(list, "vehicle", 0.5)
    guiGridListAddColumn(list, "price", 0.5)
    for i=1,#vehCS do
        guiGridListAddRow(list, vehCS[i][1], vehCS[i][2])
    end
    buybtn = guiCreateButton(30, 474, 120, 39, "buy", false, windowCS)
    guiSetProperty(buybtn, "NormalTextColour", "FF0AF415")
    closebtn = guiCreateButton(174, 474, 120, 39, "close", false, windowCS)
    guiSetProperty(closebtn, "NormalTextColour", "FFFE0000")



 
    addEventHandler("onClientGUIClick", list, verVehiculo, false)
    addEventHandler("onClientGUIDoubleClick", list, buyVehCS, false)
    addEventHandler("onClientGUIClick", buybtn, buyVehCS, false)
    addEventHandler("onClientGUIClick", closebtn, closeGUIcs, false)


end

function GTcns()

	screenW, screenH = guiGetScreenSize()
	showCursor(true)
	setElementFrozen(localPlayer, true)
    windowGT = guiCreateWindow(10, (screenH - 527) / 2, 330, 527,"Test2", false) 
    guiWindowSetSizable(windowGT, false)

    listGT = guiCreateGridList(12, 28, 304, 437, false, windowGT)
    guiGridListAddColumn(listGT, "vehicle", 0.5)
    guiGridListAddColumn(listGT, "price", 0.5)
    for i=1,#vehGT do
        guiGridListAddRow(listGT, vehGT[i][1], vehGT[i][2])
    end
    buybtn = guiCreateButton(30, 474, 120, 39, "Buy", false, windowGT)
    guiSetProperty(buybtn, "NormalTextColour", "FF0AF415")
    closebtn = guiCreateButton(174, 474, 120, 39, "Close", false, windowGT)
    guiSetProperty(closebtn, "NormalTextColour", "FFFE0000")



 
    addEventHandler("onClientGUIClick", listGT, watchVehicleGT, false)
    addEventHandler("onClientGUIDoubleClick", listaGT, buyVehicleGT, false)
    addEventHandler("onClientGUIClick", buybtn, buyVehicleGT, false)
    addEventHandler("onClientGUIClick", closebtn, closeGUIGT, false)


end

This is not efficient, isn't there a better way to make something like this? I don't want to clone every function that the GUI calls with a certain button or action

Edited by Cronoss
Link to comment
  • Moderators
11 hours ago, Cronoss said:
addCommandHandler("buy", detectPanel)
addCommandHandler("BUY", detectPanel)

You can reduce this code btw, by disable caseSensitive.

bool addCommandHandler ( string commandName, function handlerFunction [, bool caseSensitive = true ] )
addCommandHandler("buy", detectPanel, false)

 

11 hours ago, Cronoss said:

Well, I'm trying to make two different GUI panels, one of them opens when the player is in "Jefferson" and types the command "buy", and the other one opens when the player is in "Rodeo". Inside this panels there is a gridlist that is using two different tables:

As for your initial question.

local zones_CScns = {["Jefferson"] = true}
local zones_GTcns = {["Rodeo"] = true}


--- ...

if zones_CScns[gps] then
    CScns()
elseif zones_GTcns[gps] then
    GTcns()
end

or

-- !Order matters

local zones

--- ...

local theFunc = zones[gps]
if theFunc then
    theFunc()
else
	-- do something else...
end

-- ... functions CScns and GTcns here

zones = {["Jefferson"] = CScns, ["Rodeo"] = GTcns}

 

 

 

  • Thanks 1
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...