Jump to content

Superfluous work


DakiLLa

Recommended Posts

hi again.

Well, i have next question:

i have 12 buttons in my gui and ONE event that must be triggering after clicking on any button. But i dont want to write triggerServerEvent after each "click check"

I mean this:

there are the buttons:

z_one = guiCreateButton ( ... ) 
z_two = guiCreateButton ( ... ) 
z_three = guiCreateButton ( ... ) 
..... 
etc etc etc... 

and onClientGUIClick event:

addEventHandler ( "onClientGUIClick", gResRoot, 
    function () 
        if source == z_one then 
            triggerServerEvent ( "buyZone", gMe ) 
        elseif source == z_two then 
            triggerServerEvent ( "buyZone", gMe ) 
        elseif source == z_two then 
            triggerServerEvent ( "buyZone", gMe ) 
        elseif source == z_three then 
            triggerServerEvent ( "buyZone", gMe ) 
        elseif source == z_four then 
            triggerServerEvent ( "buyZone", gMe ) 
               .........etc etc etc.............. 
               ................................... 
        end 
) 

can you tell me an easy way to write this part of script in more compact form?

thx for replies :)

Link to comment

A compacter way is to use tables/arrays/whatever you call em. (you might want to read the whole post though)

z={} 
z[1] = guiCreateButton ( ... ) 
z[2] = guiCreateButton ( ... ) 
z[3] = guiCreateButton ( ... ) 
..... 
etc etc etc... 

addEventHandler ( "onClientGUIClick", gResRoot, 
    function () 
        if source == z[1] then 
             blahahahahaha 
        if source == z[2] then 
            triggerServerEvent ( "buyZone", gMe ) 
               .........etc etc etc.............. 
               ................................... 
        end 
) 

Even more compact might be...

z={} 
z[1] = {guiCreateButton ( ... ),"aVeryCoolEvent",getLocalPlayer(),"parameters"} 
z[2] = {guiCreateButton ( ... ),"anotherVeryCoolEvent",getLocalPlayer(),"parameters"} 
z[3] = {guiCreateButton ( ... ),"yetAnotherVeryCoolEvent",getLocalPlayer(),"parameters"} 
..... 
etc etc etc... 

And then...

addEventHandler ( "onClientGUIClick", gResRoot, 
    function () 
        for k,v in ipairs(z) do 
              if source==v[1] then 
                  triggerServerEvent(v[2],v[3],v[4],v[5],v[6]) 
              end 
         end 
    end 
) 

Probably not the easiest way, but atleast it should make it easier. ;) Also note that it's untested and made in crappy XFire in-game webbrowser... :D

My example will only work in the form:

z[ index ] = { element guiElement, string event, element theSource, var argument1... } 

Max number of arguments is 3 in my example.

Link to comment

I don't think that's what he wanted Gamesnert. Maybe you meant something like this?

  
-- somewhere 
z_one = guiCreateButton ( ... ) 
z_two = guiCreateButton ( ... ) 
z_three = guiCreateButton ( ... ) 
addEventHandler("onClientGUIClick", theParentOfButtons, triggerTheEvent) 
-- 
  
function triggerTheEvent() 
        local eventname, sourceroot 
    if source == z_one then 
        eventname = "myCustomEvent" -- it chooses what event to trigger here  
        sourceroot = getLocalPlayer() -- and chooses what will be the source 
                variable = "twillyg" 
    elseif source == z_two then 
         
    ......   
    end 
    triggerServerEvent(eventname, sourceroot, variable) 
end 
  

Only change the parts that vary inside the if statements, like if you need them to activate the same event, then don't declare it inside the if clause, but put it straight into triggerserverEvent, if you get what I mean

Only change things that you want to change

And you can

Link to comment

hmm, i think i will have more lines in script anyway with your example, LordAzamth, than in my version.. :D so it will more easyer to put again and again with copypast "triggerServer..."

Edit:

but anyway, thx for replies, guys, may be i will use your examples for other points in my next projects.. :wink:

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