Jump to content

Login GUI


MaVe

Recommended Posts

Posted (edited)

Hi, i read the tutorial on the MTA Wiki (http://development.mtasa.com/index.php? ... ipting_GUI)

I have the following problem:

When somebody connects and is registered, he can't login. Nothing happens when the Login button is pressed.

Code:

Server:

function joinHandler(username, password) 
  
        --local x,y,z 
        --x = 1959.55 
        --y = -1714.46 
        --z = 10 
        if (client) then 
              --spawnPlayer(client, x, y, z) 
              fadeCamera(client, true) 
              outputChatBox("Welcome to My Server", client) 
        end 
end 
  
addEvent("SubmitLogin", true) 
addEventHandler("SubmitLogin", getRootElement(), joinHandler) 

Client:

function CreateLoginWindow() 
  
        local X = 0.375 
        local Y = 0.375 
        local Width = 0.25 
        local Height = 0.25 
        wdwLogin = guiCreateWindow(X, Y, Width, Height, "Please Log In", true) 
        
        X = 0.0825 
        Y = 0.2 
        Width = 0.25 
        Height = 0.25 
        guiCreateLabel(X, Y, Width, Height, "Username", true, wdwLogin) 
        Y = 0.5 
        guiCreateLabel(X, Y, Width, Height, "Password", true, wdwLogin) 
        
        X = 0.415 
        Y = 0.2 
        Width = 0.5 
        Height = 0.15 
        edtUser = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin) 
        Y = 0.5 
        edtPass = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin) 
        guiEditSetMaxLength(edtUser, 50) 
        guiEditSetMaxLength(edtPass, 50) 
        
        X = 0.415 
        Y = 0.7 
        Width = 0.25 
        Height = 0.2 
        btnLogin = guiCreateButton(X, Y, Width, Height, "Log In", true, wdwLogin) 
        
        guiSetVisible(wdwLogin, false) 
end 
  
function initGUI( ) 
     CreateLoginWindow() 
  
     outputChatBox("Welcome to My MTA DM Server, please log in.") 
  
     if (wdwLogin ~= nil) then 
              guiSetVisible(wdwLogin, true) 
     end 
  
     showCursor(true) 
     guiSetInputEnabled(true) 
end 
addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource( ) ), initGUI ) 
  
addEventHandler("onClientGUIClick", btnLogin, function(button) 
        outputChatBox("blabla 1") 
        if button == "left" then 
                outputChatBox("blabla 2") 
                triggerServerEvent("SubmitLogin", getRootElement(), guiGetText(edtUser), guiGetText(edtPass)) 
                guiSetInputEnabled(false) 
                guiSetVisible(wdwLogin, false) 
                showCursor(false) 
        end 
end) 

What's wrong?

Edited by Guest
Posted

Umm.. your joinhandler function only does a fadeCamera and outputChatBox, nothing else. To make it login too, you have to code it as such too

Posted
Umm.. your joinhandler function only does a fadeCamera and outputChatBox, nothing else. To make it login too, you have to code it as such too

I think you dont understand my code. the joinhandler function gets executed when player clicked "login".. But my onClientGUIClick event doesnt get fired-.-

Posted
There is a typo in the wiki example. Change "client" to "source".

which "client"? in the server part, in the line with "if (client) then" ???

Posted

which "client"? in the server part, in the line with "if (client) then" ???

  
        if (source) then 
              --spawnPlayer(source, x, y, z) 
              fadeCamera(source, true) 
              outputChatBox("Welcome to My Server", source) 
        end 
  

Posted

It still doesn't work. As i said, the onClientGUIClick event doesnt get fired. I can't see my debug messages (btw: i have already removed the ";"s at the end of the row. :S

Posted

Ace_Gambit: The client variable is allowed too.. It's passed with triggerServerEvent() as the local player. You don't always pass localPlayer as source, now do you?

I think you dont understand my code. the joinhandler function gets executed when player clicked "login".. But my onClientGUIClick event doesnt get fired-.-

I agree that I didn't look too much into that code, BUT you didn't mention what kind of problems you were having too, and my crystal ball seems to be out of order atm.

  • 3 months later...
Posted

Omg i couldnt get it working yet... Can somebody help me and tell me how to make the onClientGUIClicked event?

Posted

you should move the addEventHandler ("onClientGUIClick" ...) line into the createLoginWindow as at the position where it get's called atm btnLogin doesn't exist.

Posted
If there's a mistake on the wiki, please fix it!

I think i just understood it wrong, because the wiki didnt mention good enough where i should add the event handler...

Posted

The addEventHandler can be anywhere in your script, example:

  
function pEnter(player) 
print(tostring(player) .. " entered vehicle: " .. tostring(source)); 
end 
  
function rStart() 
-- this can here 
addEventHandler("onPlayerEnterVehicle", getRootElement(), pEnter); 
end 
  
-- and here 
addEventHandler("onPlayerEnterVehicle", getRootElement(), pEnter); 
addEventHandler("onResourceStart", getRootElement(), rStart); 
  

Posted

I'll try to explain how all this works. The thing is that when resource starts, it first executes all the code in the scripts (from top left to the bottom right of the file, from left to right) after that the server (or client) triggers on(Client)ResourceStart.

So, follow these steps (in order from 1-6, don't read 6 if you haven't read 5! That way you will understand better):

-- 1st, this function is defined but is not called, why should it be called now? 
function resourceStarted( ) 
    -- 6th, finally onClientResourceStart is triggered and calls this function 
    -- which makes a button! Now you should addEventHandler with this button because it's created 
    -- and ready to be used 
    button = guiCreateButton( 10, 200, 100, 20, "1337 button", false ) 
end 
-- 2nd, you call this addEventHandler function which attaches a function to an event 
addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource() ), resourceStarted ) 
  
-- 3rd, this function is defined but is not called, why should it be called now? 
function buttonClicked( ) 
    outputChatBox( "Wow! You clicked a button! Do it again!" ); 
end 
-- 4th, you call addEventHandler function which attaches a function to an event 
-- but what is button? it wasn't created yet! keep going  --> 
addEventHandler( "onClientGUIClick", button, buttonClicked, false ) 
  
-- 5th, there is nothing here, right? so, onClientResourceStart is triggered, lets do it now! (go to 6th) 

Posted
The addEventHandler can be anywhere in your script, example:
  
function pEnter(player) 
print(tostring(player) .. " entered vehicle: " .. tostring(source)); 
end 
  
function rStart() 
-- this can here 
addEventHandler("onPlayerEnterVehicle", getRootElement(), pEnter); 
end 
  
-- and here 
addEventHandler("onPlayerEnterVehicle", getRootElement(), pEnter); 
addEventHandler("onResourceStart", getRootElement(), rStart); 
  

Yes its possible for callbacks like that, but OnClientGUIClick(ed?) you need to pass the button element, and that element is defined in onClientResourceStart, so i have to add the eventhandler-adder there.

Posted

50p shows also a very good example.

Example for buttons:

  
function buildMyWindow() 
local window = guiCreateWindow(x, y, w, h, title, relative); 
local btn = guiCreateButton(x, y, w, h, title, relative); 
  
addEventHandler("onClientGUIClick", btn, btnClick, false); 
-- See i don't use getRootElement but i use the button element. 
end 
  
function btnClick(button) 
outputChatBox("my button is clicked YEEHAA", getLocalPlayer()); 
end 
  

Posted

Interesting why do you pass the red colour value as a local player?

Hint: clientside outputChatBox doesn't have visibleTo argument

Posted
Interesting why do you pass the red colour value as a local player?

Hint: clientside outputChatBox doesn't have visibleTo argument

Rofl, owned.

Posted
Interesting why do you pass the red colour value as a local player?

Hint: clientside outputChatBox doesn't have visibleTo argument

Rofl, owned.

If you want to post something on this forum use your brain sometimes and think about what you're going to post... This post was useless ¬¬

Alexander, I did show a good example of how scripts work and why it didn't work for him. If you want to be a scripter you should know how things work not just how to script because one day you may get a problem and you won't even know what causes it.

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