Jump to content

GUI


StefanAlmighty

Recommended Posts

I'm adding more GUI code to the client file, which will be shown when the player clicks the button "Create" which I've added to the main GUI. The code is this, but according to my server, there's a problem with it because the whole script doesn't work.

Here is the code:

  
local status = "Your current position will be used for this marker." 
local gui.newLocation = guiCreateWindow(0, 0, w, h, "Create Teleport © Almighty", false) 
centerWindow(gui.newLocation) 
guiSetVisible(gui.newLocation, false) 
guiWindowSetSizable(gui.newLocation, false) 
local gui.valueLabel = guiCreateLabel(10, 53, 54, 15, "Value", false, gui.newLocation) 
local gui.valueInput = guiCreateEdit(10, 28, 150, 25, "", false, gui.newLocation) 
local gui.descLabel = guiCreateLabel(164, 53, 118, 15, "Location Description", false, gui.newLocation) 
local gui.descInput = guiCreateEdit(164, 28, 226, 25, "", false, gui.newLocation) 
local gui.localeMessage = guiCreateLabel(10, 78, 380, 22, status, false, gui.newLocation) 
local gui.localeStatusColor = guiLabelSetColor(gui.localeMessage, 255, 255, 255) 
local gui.newCreate = guiCreateButton(240, 110, 150, 30, "Create", false, gui.newLocation) 
  

I added that directly beneath the main 'window' GUI on the client file.

Link to comment
  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

  • Moderators

You are creating the new window but you hide it immediately, is that really what you wanted to do at this point of the code ? Are you showing it later ?

local gui.newLocation = guiCreateWindow(0, 0, w, h, "Create Teleport © Almighty", false) 
... 
guiSetVisible(gui.newLocation, false) 

Also, I just read the whole topic, and KariiM did a pretty nice job (with you of course) but you guys are using:

guiGridListGetSelectedItem(grid)+1 

to know which row has been clicked and get the right row in the table. However this is working only if the gridlist hasn't been reordered. The player can reorder the gridlist by clicking on the column headers. By doing that the 1st row in the gridlist might not match the 1st row in the data table anymore.

To fix that you can ask a row (well actually a cell of a row) to store a value for you which will be the corresponding row's number in the data table:

function viewGUI (aTable) 
    if ( source == getLocalPlayer() ) then 
        guiSetVisible (window, true) 
        showCursor (true) 
        guiGridListClear(grid) 
        for index ,val in ipairs (aTable) do 
            local row = guiGridListAddRow (grid) 
            guiGridListSetItemText (grid, row, col1, val[1], false, true) 
            guiGridListSetItemText (grid, row, col2, val[2], false, false) 
            guiGridListSetItemText (grid, row, col3, val[3], false, false) 
            guiGridListSetItemData (grid, row, 1, index ) -- storing the row's number (or index) of the [b]data [/b]table 
        end 
    end 
end 

and then get it back this way:

addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false) 
            showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then 
            outputChatBox ("You must select the destination !", getLocalPlayer(), 255, 0, 0) 
            return false 
        end 
        guiSetVisible(window, false) 
        showCursor(false) 
        local rowSelected = guiGridListGetSelectedItem(grid) -- getting the gridlist row's number as you did 
        triggerServerEvent("warp", getLocalPlayer(), guiGridListGetItemData (grid, rowSelected, 1)) -- but getting the [b]data [/b]table index from the row instead of doing a +1 
    end 
end) 

So now the gridlist can be reordered as much as it wants to, our item data we set won't be modified.

@KariiiM: Nice to see you now helping others (I remember I was helping you not so much time ago). Keep doing that, you are helping the community and it's great.

However if I can say just one thing, try to indent your code properly: 1 tab or 4 spaces is the recommended size for 1 level of indentation. The code is more readable and you can instantly see if you missed for example an end to close an opened if or a for loop or whatever. Thanks and keep up the good work.

Link to comment

I'm working on the GUI for create and delete buttons, so I'll get back to you if I get issues.

But for now I'm getting this issue, the script doesn't work because I've added the "elseif (source == create)" onwards. I don't see what I did wrong there?

  
addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
        guiSetVisible (window, false) 
        showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then outputChatBox ("You must selecte the destination",getLocalPlayer(),255,0,0) return false end 
        guiSetVisible(window,false) 
        showCursor(false) 
        triggerServerEvent("warp",getLocalPlayer(),guiGridListGetSelectedItem(grid)+1) 
    elseif (source == create) 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false)           -- closes current gui 
            showCursor(false) 
        end 
    end 
end) 
  

Link to comment

I'm going to bump this because I don't think my question was clear enough. My code is crashing because I added an additional elseif for when the button "Create" is clicked. Here is the code in question:

  
addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
        guiSetVisible (window, false) 
        showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then outputChatBox ("You must selecte the destination",getLocalPlayer(),255,0,0) return false end 
        guiSetVisible(window,false) 
        showCursor(false) 
        triggerServerEvent("warp",getLocalPlayer(),guiGridListGetSelectedItem(grid)+1) 
    elseif (source == create) 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false)           -- closes current gui 
            showCursor(false) 
        end 
    end 
end) 

I'll narrow down the specific part which is causing the script to not work. Here it is:

  
elseif (source == create) 
    if guiGetVisible(window) ~= nil then 
        guiSetVisible (window, false)           -- closes current gui 
        showCursor(false) 
    end 
end 

Link to comment
addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
        guiSetVisible (window, false) 
        showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then outputChatBox ("You must selecte the destination",getLocalPlayer(),255,0,0) return false end 
        guiSetVisible(window,false) 
        showCursor(false) 
        triggerServerEvent("warp",getLocalPlayer(),guiGridListGetSelectedItem(grid)+1) 
    elseif (source == create) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false)            
            showCursor(false) 
        end 
    end 
end) 
  

Link to comment

When I keep adding more click responses to that function, it messes up, what am I doing wrong?

  
addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false) 
            showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then outputChatBox ("You must selecte the destination",getLocalPlayer(),255,0,0) return false end 
        guiSetVisible(window,false) 
        showCursor(false) 
        triggerServerEvent("warp",getLocalPlayer(),guiGridListGetSelectedItem(grid)+1) 
    elseif (source == create) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false)           
            showCursor(false) 
            guiSetVisible (newLocation, true) 
            showCursor (true) 
        end 
    elseif (source == newLocationCancel) 
        if guiGetVisible(newLocation) ~= nil then 
            guiSetVisible (newLocation, false) 
            guiSetVisible (window, true) 
        end 
    elseif (source == newLocationCreate) 
        if guiGetVisible(newLocation) ~= nil then 
            guiSetVisible (newLocation, false) 
            showCursor (false) 
        end 
    end 
end) 

I added more to it, but now the script doesn't work again :(

Link to comment

I added more to it, but now the script doesn't work again :(

You're using elseif without adding then so it will return false with ERRORs

elseif (source == newLocationCreate) 

Must be like that

elseif (source == newLocationCreate) then 

Full code

  
addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false) 
            showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then outputChatBox ("You must selecte the destination",getLocalPlayer(),255,0,0) return false end 
        guiSetVisible(window,false) 
        showCursor(false) 
        triggerServerEvent("warp",getLocalPlayer(),guiGridListGetSelectedItem(grid)+1) 
    elseif (source == create) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false)           
            showCursor(false) 
            guiSetVisible (newLocation, true) 
            showCursor (true) 
        end 
    elseif (source == newLocationCancel) then 
        if guiGetVisible(newLocation) ~= nil then 
            guiSetVisible (newLocation, false) 
            guiSetVisible (window, true) 
        end 
    elseif (source == newLocationCreate) then 
        if guiGetVisible(newLocation) ~= nil then 
            guiSetVisible (newLocation, false) 
            showCursor (false) 
        end 
    end 
end) 

Link to comment

Okay so now I'm trying to create GUI text if a player doesn't fill out the name box or the description box. However, the text for some reason does not show up.

  
addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false) 
            showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then outputChatBox ("You must selecte the destination",getLocalPlayer(),255,0,0) return false end 
        guiSetVisible(window,false) 
        showCursor(false) 
        triggerServerEvent("warp",getLocalPlayer(),guiGridListGetSelectedItem(grid)+1) 
    elseif (source == create) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false)           
            showCursor(false) 
            guiSetVisible (newLocation, true) 
            showCursor (true) 
        end 
    elseif (source == newLocationCancel) then 
        if guiGetVisible(newLocation) ~= nil then 
            guiSetVisible (newLocation, false) 
            guiSetVisible (window, true) 
        end 
    elseif (source == newLocationCreate) then 
        if (nameInput == "") then 
            guiSetText (errorMessage, "You have not entered a name for the teleport.") 
        elseif (descInput == "") then 
            guiSetText (errorMessage, "You have not entered a description for the teleport.") 
        else 
            -- ill continue this after 
        end 
    end 
end) 

That's the function for when the button is clicked. Here is the code which creates the label which can be found at the top of my script.

  
local errorMessage = guiCreateLabel(10, 78, 380, 22, "", false, newLocation) 
  

Link to comment

Try this and tell me the result

addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false) 
            showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then outputChatBox ("You must selecte the destination",getLocalPlayer(),255,0,0) return false end 
        guiSetVisible(window,false) 
        showCursor(false) 
        triggerServerEvent("warp",getLocalPlayer(),guiGridListGetSelectedItem(grid)+1) 
    elseif (source == create) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false)           
            showCursor(false) 
            guiSetVisible (newLocation, true) 
            showCursor (true) 
        end 
    elseif (source == newLocationCancel) then 
        if guiGetVisible(newLocation) ~= nil then 
            guiSetVisible (newLocation, false) 
            guiSetVisible (window, true) 
        end 
    elseif (source == newLocationCreate) then 
        if (nameInput == "") then guiSetText (errorMessage, "You have not entered a name for the teleport.") return end 
        if (descInput == "") then guiSetText (errorMessage, "You have not entered a description for the teleport.") return end 
        -- ill continue this after 
    end 
end) 

Link to comment

Try that,

addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false) 
            showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then outputChatBox ("You must selecte the destination",getLocalPlayer(),255,0,0) return false end 
        guiSetVisible(window,false) 
        showCursor(false) 
        triggerServerEvent("warp",getLocalPlayer(),guiGridListGetSelectedItem(grid)+1) 
    elseif (source == create) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false)           
            showCursor(false) 
            guiSetVisible (newLocation, true) 
            showCursor (true) 
        end 
    elseif (source == newLocationCancel) then 
        if guiGetVisible(newLocation) ~= nil then 
            guiSetVisible (newLocation, false) 
            guiSetVisible (window, true) 
        end 
    elseif (source == newLocationCreate) then 
        local name = guiGetText(nameInput) 
        local desc = guiGetText(descInput) 
        if (name == "") then guiSetText (errorMessage, "You have not entered a name for the teleport.") return end 
        if (desc == "") then guiSetText (errorMessage, "You have not entered a description for the teleport.") return end 
  
        -- ill continue this after 
    end 
end) 

Link to comment

Okay so now for the hard part. I'm not sure how to use table.insert. When the player types in the name and description, it should add a row to the data table which is in server.lua. It's at the bottom of this code.

  
addEventHandler ("onClientGUIClick", root, 
function () 
    if (source == cancel) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false) 
            showCursor(false) 
        end 
    elseif (source == transport) then 
        local row = guiGridListGetSelectedItem(grid) 
        if row and row == -1 then outputChatBox ("You must selecte the destination",getLocalPlayer(),255,0,0) return false end 
        guiSetVisible(window,false) 
        showCursor(false) 
        triggerServerEvent("warp",getLocalPlayer(),guiGridListGetSelectedItem(grid)+1) 
    elseif (source == create) then 
        if guiGetVisible(window) ~= nil then 
            guiSetVisible (window, false)           
            showCursor(false) 
            guiSetVisible (newLocation, true) 
            showCursor (true) 
        end 
    elseif (source == newLocationCancel) then 
        if guiGetVisible(newLocation) ~= nil then 
            guiSetVisible (newLocation, false) 
            guiSetVisible (window, true) 
        end 
    elseif (source == newLocationCreate) then 
        local name = guiGetText(nameInput) 
        local desc = guiGetText(descInput) 
        if (name == "") then guiSetText (errorMessage, "You have not entered a name for the teleport.") return end 
        if (desc == "") then guiSetText (errorMessage, "You have not entered a description for the teleport.") return end 
        -- How do I create the function which adds a row to my data table (which is located in the server.lua) 
    end 
end) 
  

Here is the data table:

  
local data = { 
[1] = {"1", "Los Santos", "Pershing Square", 1479.9873046875, -1710.9453125, 13.36874961853,    0,  0,  0}, 
[2] = {"2", "San Fierro", "Doherty", -1988.5693359375, 507.0029296875, 35.171875,   0,  0,  90}, 
[3] = {"3", "Las Venturas", "Las Venturas Airport", 1691.6801757813,    1449.1293945313,    10.765375,  0,  0,  268}, 
} 
  

Also, I want it so for the IDs, it grabs the next available ID. So for example, if there was 1, 2 and 4 it would create a teleport as 3.

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