Jump to content

Redundant GUI Code


jkub

Recommended Posts

I really can't stand having to use redundant code anymore it takes long to do and it makes my scripts look like shit. Anyway in this instance I am having a major problem with a housing system I am making. I am currently working on the gui and in this "wizard" I created is like 19 gui edit fields.

I also have a create button in the lower corner. When I click the create button it should only pass the data onto the server IF ALL of the text fields are filled in. The big problem with that is the only way I know to check all of them is to individually do if guiGetText ( blahblahfield) == "" then.....

and that will end up being really long and stupid and pissing me off. I thought of a way myself to get all the children of the wizard GUI and then single out the edit fields. Only problem with that is there is no way to get the type of gui element. for example a button or edit field etc...

Please help me.

Link to comment
-- you can check gui elements' type, and editbox element type is "gui-edit": 
for i, element in ipairs(getElementChildren(someParentElement)) do 
  if getElementType(element) == "gui-edit" then 
     -- your check or whatever goes here 
  end 
end 
  
-------------------------------------------------------------------------------------  
 -- you can also put all your edits in a table like: 
myEdits = {} 
myEdits[1] = guiCreateEdit(blablabla) 
myEdits[2] = guiCreateEdit(blablabla) 
-- etc. 
-- and then just loop through them and check: 
for i, edit in ipairs(myEdits) do 
   -- your check or whatever goes here 
end 

Link to comment
-- you can check gui elements' type, and editbox element type is "gui-edit": 
function isEditsFull() 
for i, element in ipairs(getElementChildren(someParentElement)) do 
  if getElementType(element) == "gui-edit" then 
     if guiGetText(element) == "" then 
       return false 
     end 
  end 
  return true 
end 
end 
  
-------------------------------------------------------------------------------------  
 -- you can also put all your edits in a table like: 
myEdits = {} 
myEdits[1] = guiCreateEdit(blablabla) 
myEdits[2] = guiCreateEdit(blablabla) 
-- etc. 
-- and then just loop through them and check: 
function isEditsFull() 
for i, edit in ipairs(myEdits) do 
   if guiGetText(edit) == "" then return false end 
end 
return true 
end 

returns a true value IF HAS text

Link to comment

Isn't also possible to do something like this:

myEdit1 = guiCreateEdit(args...) 
myEdit2 = guiCreateEdit(args...) 
edits = {myEdit1, myEdit2} 
  
function checkEdits() 
for index, element in ipairs(edits) do 
if not isElement(element) then return end 
    if guiGetText(element) == "" then return false else return true end 
    end 
end 

Link to comment

Thanks again, I am happy to say that it works.

Now about something, I want to pass all this data serverside. I tried sticking all the gui edits in a table and creating another table with their text and sending it server side but for some reason every time it only adds the very first gui edit to the table.

elseif source == create_Btn then 
            for i, element in pairs(getElementChildren(addTab)) do 
                if getElementType(element) == "gui-edit" then 
                    if guiGetText(element) == "" or guiComboBoxGetSelected ( combobox_Class ) == -1 or guiComboBoxGetSelected ( combobox_Int ) == -1 or guiComboBoxGetSelected ( combobox_Dim ) == -1 then 
                        outputChatBox ( "ERROR, make sure all information is entered", 255, 0, 0 )  
                        ready = false 
                        return false 
                    else 
                        int = guiComboBoxGetItemText ( combobox_Class, guiComboBoxGetSelected ( combobox_Class ) ) 
                        dim = guiComboBoxGetItemText ( combobox_Dim, guiComboBoxGetSelected ( combobox_Dim ) ) 
                        class = guiComboBoxGetItemText ( combobox_Int, guiComboBoxGetSelected ( combobox_Int ) ) 
                        table.insert ( guiEditInputs, guiGetText(element) ) 
                        ready = true 
                        break 
                    end 
                end 
            end 
            if ready == true then 
                triggerServerEvent ( "addPropertyToDatabase", getLocalPlayer(), int, dim, class, guiEditInputs ) 
            end 
        end 

Link to comment

because you're setting ready=true at first iteration and then exiting the loop using break.

you dont really need break/ready stuff, it will stop and event wont be triggered after "return false"

        elseif source == create_Btn then 
            for i, element in pairs(getElementChildren(addTab)) do 
                if getElementType(element) == "gui-edit" then 
                    if guiGetText(element) == "" or guiComboBoxGetSelected ( combobox_Class ) == -1 or guiComboBoxGetSelected ( combobox_Int ) == -1 or guiComboBoxGetSelected ( combobox_Dim ) == -1 then 
                        outputChatBox ( "ERROR, make sure all information is entered", 255, 0, 0 ) 
                        return false 
                    else 
                        int = guiComboBoxGetItemText ( combobox_Class, guiComboBoxGetSelected ( combobox_Class ) ) 
                        dim = guiComboBoxGetItemText ( combobox_Dim, guiComboBoxGetSelected ( combobox_Dim ) ) 
                        class = guiComboBoxGetItemText ( combobox_Int, guiComboBoxGetSelected ( combobox_Int ) ) 
                        table.insert(guiEditInputs, guiGetText(element)) 
                    end 
                end 
            end 
            triggerServerEvent ( "addPropertyToDatabase", getLocalPlayer(), int, dim, class, guiEditInputs ) 
        end 

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