Jump to content

onClientGUIClick


jkub

Recommended Posts

im working on a simple weapons gui and as far it works fine other then the buttons

i cant ever use the onClientGUIClick handler correctly even though I use it just like it says on the wiki

first thing I want to where i click the btnCls then it will close the window but it dont

function createWindow () 
    local x = 0.02 
    local y = 0.3 
    local Width = 0.2 
    local Height = 0.6 
        myWindow = guiCreateWindow ( x, y, Width, Height, "Test", true ) 
        guiSetVisible ( myWindow, false ) 
        guiWindowSetMovable ( myWindow, false ) 
        guiWindowSetSizable ( myWindow, false ) 
         
    x = 0.25 
    y = 0.9 
    Width = 0.5 
    Height = 0.05 
        btnCls = guiCreateButton ( x, y, Width, Height, "Close", true, myWindow ) 
  
    x = 0.1 
    y = 0.1 
    Width = 0.28 
    Height = 0.1 
        guiCreateLabel ( x, y, Width, Height, "colt45", true, myWindow ) 
end 
  
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), 
  
    function () 
        createWindow () 
            if ( myWindow ~= nil ) then 
                guiSetVisible ( myWindow, false ) 
            end 
    end 
  
  
) 
  
function hide ( btnCls ) 
    guiSetVisible ( myWindow, false ) 
    showCursor ( false ) 
end 
function show () 
    guiSetVisible ( myWindow, true ) 
    showCursor ( true ) 
end 
  
addCommandHandler ( "open", show ) 
addCommandHandler ( "close", hide ) 
  
addEventHandler ( "onClientGUIClick", btnCls, hide ) 

? what am i doing wrong...

Link to comment

jkub, I'd suggest using my GUI Classes which are easy to use and you can easily attach functions to "Click" event.

Your code would look like this:

function createWindow( ) 
    myWindow = Window:Create( .02, .3, .2, .6, "Test", true ); 
    myWindow:Visible( false ); 
    myWindow:Movable( false ); 
    myWindow:Sizable( false ); 
    myWindow:AddLabel( .1, .1, .28, .1, "colt45", true ); 
    --[[ 
   I made Add... functions for all the gui elements which can be children of the window 
   the names of the functions are easy to remeber, like, AddLabel, AddButton, AddGridList, etc. 
   ]] 
  
    btnCls = myWindow:AddButton( .25, .9, .5, .05, "Close", true ); -- you see here? simple 
    btnCls:AddOnClick( hide ); -- look how simple it is! 
end 
addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource() ), createWindow ); 
  
function hide( ) 
    myWindow:Visible( false ); 
    showCursor( false ); 
end 

Most of the GUI functions from wiki are accessible from my classes.

"guiSet" and "guiGet" are removed from the function name, so for instance if you wanted to see if a window is visible you'd use: myWindow:Visible(), if you'd want to set its visibility you'd use myWindow:Visible( true or false )

Link to comment

Oh I see. It would be so much easier and ( neater ) doing it like that and the way you do the Add thing for all the other gui elements for the parent window. You revised it in less then half the lines of code I did lol. Works wonders. Thanks for this amazing relief! Im gonna see what I can do as cuz I was pulling my hair out trying to get that to work correctly lol. :D

Link to comment

Maybe in the future I'll take an advantage of arc_'s animation class, so that you could have more functions, like Slide() (to slide gui on your screen), SlideAndFade() (to slide and fade in or fade out on the screen) or Fade() (to fade in or fade out)... But that's far from being done due to lack of time.

Anyway, remember, I replaced 2 functions with only 1, so if you call a method without passing any values, it will act like "Get", eg.

button = Button:Create( 10, 200, 50, 20 ); -- notice that I didn't pass "relative" param here, because I made it default to false 
button:Position( ); -- this will return it's position (non-relative values, pixels) 
button:Position( true ); -- should get position with relative values ( 0 - 1 ) 
button:Position( 100, 200 ); -- it says for itself, also lack of "relative" param 

"GUI made easy!"

Link to comment

omg lol I am foolish. I got that now and it now works but my next prob is a button I click to buy a gun

I use cash = getPlayerMoney ( thePlayer )

to get the cash but It says that getPlayerMoney is a nil value

function colt () 
local cash = getPlayerMoney ( thePlayer ) 
        if ( cash < 350 ) then 
            outputChatBox ( "You need $350 for this weapon", thePlayer ) 
        else 
            giveWeapon ( thePlayer, 22, 35, true ) 
            takePlayerMoney ( thePlayer, 350 ) 
        end 
end 
  

Link to comment
wait... I think i caught myself

is it because I am using "getPlayerMoney" which is a server function on a clientside script> is that it

if it is how would I link this correctly to the server script?

Yes, getPlayerMoney is server-side only. You must triggerServerEvent which will deal with the money and the weapons because you can't have a gun that only you can see it.. other players wouldn't get any damage. Read a bit more about triggerServerEvent.

Link to comment

client triggered serverside event onElementClicked but event is not marked as remotly triggerable

addEvent ( "onButtonClicked", true )--make it remotley triggerable 
  
function onButtonClickedHandler ( event ) 
    triggerServerEvent ( "onPlayerClick" ) 
end 
  
function coltBuy ( thePlayer ) 
    giveWeapon ( thePlayer, 22, 35 ) 
end 
  
addEventHandler ( "onButtonClicked", getRootElement(), coltBuy ) 

Link to comment
onElementClicked
"onButtonClicked" 

I think that has to be a little mistake lol...

Besides, onElementClicked event already exists. So you can't add it again. Neither you can trigger a predefined event with a piece of script. :)

Solution? Just call the onButtonClicked event or rename it to something cool like onClientPushedTheBigBlackButtonThingyWhichShouldGiveHimAWeaponButThatDependsOnIfThisEventWorksBecauseIfItDoesntTheServerCantGiveItAnyway. Although that might be a little TOO much waste of pretty much anything in your life. :P

Link to comment

Server-side:

addEvent( "clientRequestsWeapon", true ); 
addEventHandler( "clientRequestsWeapon", getRootElement(), 
    function( ) 
        local money = getPlayerMoney( client ); 
        if money >= 350 then 
            takePlayerMoney( client, 350 ); 
            giveWeapon( client, 22, 35, true ); 
        else 
            outputChatBox( "Get some money before you buy this shit!!!", client ); 
        end 
    end 
) 

NOTE: Some people don't know that server events triggered by clients have another "hidden" variable which is "client". This variable tells the server which client triggered the event, so you don't have to pass getLocalPlayer() to triggerServerEvent and add that to list of arguments in a function attached to the event. In this case "source" isn't even needed.

Client-side:

-- add this to a function added to AddOnClick() 
triggerServerEvent( "clientRequestsWeapon", getRootElement() ); 

This one line will trigger server event (which is added in the server-side code above). The event has a function attached which will be called by the event. That function will deal with the money and the weapon too.

I hope you'll understand now how server and client can "talk" with each other. If you still don't understand.. then I wish you luck in reading wiki.

Peace and Love!

Link to comment

I use command to open the gui but I eventually wanted to do it with a marker located at ammunation... I looked at your bank script to see how a hit marker would trigger the gui to come up for a client I seemed to do that right. Its pretty solid now but How would I extract this gui script to its own resource???

Link to comment
I use command to open the gui but I eventually wanted to do it with a marker located at ammunation... I looked at your bank script to see how a hit marker would trigger the gui to come up for a client I seemed to do that right. Its pretty solid now but How would I extract this gui script to its own resource???

You just have to copy "/classes/" directory with all the file in it into your new resource and and copy specified lines from meta.xml into your new resource meta.xml. I stated which lines you should copy in the meta.xml itself.

It says:

...

The magic is that you just copy my GUI Classes files (/classes/ dir) to your resource and include them in your new meta.xml so that client knows that the files are used and they will be used... then you can just start scripting with my classes.

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