Absence2 Posted March 2, 2012 Share Posted March 2, 2012 Hello, I just found this wonderful resource, https://community.multitheftauto.com/index.php?p= ... ails&id=30 but I would like to expand it's uses by adding a few GUIs instead of commands. So can I somehow add a GUI button and onClientGUIclick it triggers a commandhandler? ( /snake ) I've tried by adding so on onClientGUIclick it opens gameWindow but that failed and did not work at all. Client: size = 0.02 direction = "right" lastdir = "right" pause = false started = false firstStart = false checkbox = {} positions = {} positions[1] = 0.1 positions[2] = 10 positions[3] = 20 positions[4] = 30 positions[5] = 40 positions[6] = 50 positions[7] = 60 positions[8] = 70 positions[9] = 80 positions[10] = 90 function mainWindow () if not gameWindow then firstStart = true gameWindow = guiCreateWindow (0.2, 0.2, 0.6, 0.6, "Snake!", true) gameArea = guiCreateGridList (0.3, 0.06, 0.7, 0.95, true, gameWindow) score = guiCreateLabel(0.05, 0.1, 0, 0, "Score: 0", true, gameWindow) speed1 = guiCreateLabel(0.1, 0.53, 0, 0, "Speed", true, gameWindow) guiSetFont(speed1, "default-bold-small") guiSetSize ( speed1, guiLabelGetTextExtent ( speed1 ), guiLabelGetFontHeight ( speed1 ), false ) local sizeD = guiLabelGetFontHeight ( speed1 ) difficulty = guiCreateProgressBar (0.05, 0.55, 0.20, 0.10, true, gameWindow) local x, y = guiGetPosition(difficulty, false) local sx, sy = guiGetSize(difficulty, false) guiSetPosition(difficulty, x, y + sizeD, false) guiProgressBarSetProgress(difficulty, 1) addEventHandler("onClientGUIClick", difficulty, function (button, state, posX, posY) local cX = guiGetScreenSize() * getCursorPosition" class="kw4">getCursorPosition" class="kw4">getCursorPosition() local pX, pY = guiGetPosition(difficulty, false) local x = cX / pX local x = x - 8 local x = x * 28 local progress = x if progress <= 1 then progress = 1 elseif progress >= 100 then progress = 100 end guiProgressBarSetProgress(difficulty, math.floor(progress)) end, false) guiSetFont (score, "default-bold-small") startPause = guiCreateButton(0.05, 0.7, 0.20, 0.10, "Start!", true, gameWindow) addEventHandler("onClientGUIClick", startPause, startOrPauseSnake, false) guiSetSize ( score, guiLabelGetTextExtent ( score ), guiLabelGetFontHeight ( score ), false ) currentScore = 0 showCursor(true) else guiSetVisible(gameWindow, true) showCursor(true) end bindKey ("arrow_u", "down", function () direction = "up" end) bindKey ("arrow_r", "down", function () direction = "right" end) bindKey ("arrow_d", "down", function () direction = "down" end) bindKey ("arrow_l", "down", function () direction = "left" end) bindKey ("space", "down", startOrPauseSnake) end addCommandHandler("snake", mainWindow) --progress == - 10 per progress function startOrPauseSnake () local progress = guiProgressBarGetProgress(difficulty) time = 450 - ( progress * 4.5 ) + 50 if started == false then started = true if gameOver then destroyElement(gameOver) end guiSetText(startPause, "Pause!") checkbox = {} direction = "right" lastdir = "right" currentScore = 0 guiSetText (score, "Score: "..currentScore) checkbox[1] = guiCreateCheckBox(0.4,0.4, size, size, "", true, true, gameArea) checkbox[2] = guiCreateCheckBox(0.4-size,0.4, size, size, "", false, true, gameArea) checkbox[3] = guiCreateCheckBox(0.4-size-size,0.4, size, size, "", false, true, gameArea) checkbox[4] = guiCreateCheckBox(0.4-size-size-size,0.4, size, size, "", false, true, gameArea) x = positions[math.random(1, 10)] / 100 y = positions[math.random(1, 10)] / 100 fruit = guiCreateCheckBox (x, y, size, size, "", true, true, gameArea) movement = setTimer(moveit, time, 0) last = #checkbox else if pause == false then guiSetText(startPause, "Start!") killTimer(movement) pause = true else guiSetText(startPause, "Pause!") movement = setTimer(moveit, time, 0) pause = false end end end function stopTheSnake () if ( wannaStart ) then guiSetVisible(wannaStart, false) showCursor(false) end if firstStart == false then return end if started == true and pause == false then killTimer(movement) pause = true guiSetText(startPause, "Start!") guiSetVisible(gameWindow, false) showCursor(false) elseif started == true and pause == true then guiSetVisible(gameWindow, false) showCursor(false) elseif started == false then guiSetVisible(gameWindow, false) showCursor(false) end unbindKey ("arrow_u", "down") unbindKey ("arrow_r", "down") unbindKey ("arrow_d", "down") unbindKey ("arrow_l", "down") unbindKey ("space", "down", startOrPauseSnake) end addEvent("stopSnakeC", true) addEventHandler("stopSnakeC", getRootElement(), stopTheSnake) addCommandHandler("stopsnake", stopTheSnake) addEvent("startSnakeC", true) addEventHandler("startSnakeC", getRootElement(), function () if not wannaStart then showCursor(true) wannaStart = guiCreateWindow (0.4,0.3, 0, 0, "Play Snake?", true) guiSetSize(wannaStart, 250, 200, false) playText = guiCreateLabel(0.1, 0.3, 0.9,0.7, "Would you like to play some snake while you wait?", true, wannaStart) guiSetFont(playText, "default-bold-small") guiLabelSetHorizontalAlign(playText, "left", true) --guiSetSize ( playText, guiLabelGetTextExtent ( playText ), guiLabelGetFontHeight ( playText ), false ) yeaplay = guiCreateButton(0.1, 0.8, 0.3, 0.2, "Yes", true, wannaStart) addEventHandler("onClientGUIClick", yeaplay, function () guiSetVisible(wannaStart, false) mainWindow () end, false) noplay = guiCreateButton(0.6, 0.8, 0.3, 0.2, "No", true, wannaStart) addEventHandler("onClientGUIClick", noplay, function () guiSetVisible(wannaStart, false) showCursor(false) end, false) else guiSetVisible(wannaStart, true) showCursor(true) end end) posX = {} posY = {} function moveit () --posX[1], posY[1] = guiGetPosition(checkbox[1], true) i = 0 if direction == "right" then if lastdir == "left" then direction = "left" else lastdir = "right" for k,v in ipairs(checkbox) do i = i + 1 if v == checkbox[1] then posX[i], posY[i] = guiGetPosition(v, true) guiSetPosition(v, posX[i] + (size ), posY[i], true) --posX, posY = guiGetPosition(v, true) else posX[i], posY[i] = guiGetPosition(v, true) guiSetPosition(v, posX[i-1], posY[i-1], true) end end end elseif direction == "down" then if lastdir == "up" then direction = "up" else lastdir = "down" for k,v in ipairs(checkbox) do i = i + 1 if v == checkbox[1] then posX[i], posY[i] = guiGetPosition(v, true) guiSetPosition(v, posX[i], posY[i] + (size ), true) --posX, posY = guiGetPosition(v, true) else posX[i], posY[i] = guiGetPosition(v, true) guiSetPosition(v, posX[i-1], posY[i-1], true) end end end elseif direction == "left" then if lastdir == "right" then direction = "right" else lastdir = "left" for k,v in ipairs(checkbox) do i = i + 1 if v == checkbox[1] then posX[i], posY[i] = guiGetPosition(v, true) guiSetPosition(v, posX[i] - (size), posY[i], true) --posX, posY = guiGetPosition(v, true) else posX[i], posY[i] = guiGetPosition(v, true) guiSetPosition(v, posX[i-1], posY[i-1], true) end end end elseif direction == "up" then if lastdir == "down" then direction = "down" else lastdir = "up" for k,v in ipairs(checkbox) do i = i + 1 if v == checkbox[1] then posX[i], posY[i] = guiGetPosition(v, true) guiSetPosition(v, posX[i], posY[i] - (size ), true) --posX, posY = guiGetPosition(v, true) else posX[i], posY[i] = guiGetPosition(v, true) guiSetPosition(v, posX[i-1], posY[i-1], true) end end end end headX, headY = guiGetPosition(checkbox[1], true) if headX < -0.01 or headX >= 0.99 or headY < -0.01 or headY >= 0.99 then killTimer(movement) --outputChatBox ("Game Over") gameOver = guiCreateLabel (0.3, 0.5, 0, 0, "Game over!", true, gameArea) guiSetFont (gameOver, "default-bold-small") guiSetSize ( gameOver, guiLabelGetTextExtent ( gameOver ), guiLabelGetFontHeight ( gameOver ), false ) last = #checkbox blinks = 0 snakeVisible = true gameover1 = setTimer (destroySnake, 400, 10) end for k,v in ipairs(checkbox) do if v ~= checkbox[1] then local x, y = guiGetPosition(v, true) if x == headX and y == headY then killTimer(movement) --outputChatBox ("Game Over") gameOver = guiCreateLabel (0.3, 0.5, 0, 0, "Game over!", true, gameArea) guiSetSize ( gameOver, guiLabelGetTextExtent ( gameOver ), guiLabelGetFontHeight ( gameOver ), false ) last = #checkbox guiSetFont (gameOver, "default-bold-small") blinks = 0 snakeVisible = true gameover1 = setTimer (destroySnake, 400, 10) Link to comment
Ludo Posted March 2, 2012 Share Posted March 2, 2012 You may use executeCommandHandler(commandName, thePlayer, [other arguments]) Link to comment
Absence2 Posted March 2, 2012 Author Share Posted March 2, 2012 You may use executeCommandHandler(commandName, thePlayer, [other arguments]) I don't get it, how do I attach that^ to a onClientGuiClick / guibutton ? Link to comment
Castillo Posted March 2, 2012 Share Posted March 2, 2012 function clickHandler() executeCommandHandler("snake") end addEventHandler("onClientGUIClick",myButton,clickHandler,false) Link to comment
Absence2 Posted March 2, 2012 Author Share Posted March 2, 2012 (edited) function clickHandler() executeCommandHandler("snake") end addEventHandler("onClientGUIClick",myButton,clickHandler,false) That did work on stopsnake but not /snake, but I suppose that is because they're in two different .lua files (same folder due), any suggestions? >.< also I get this error: bad argument @ 'addEventHandler' [expected element at arguement 2, got nil] something to worry about? playSnake = guiCreateButton(0.05, 0.85, 0.20, 0.10, "Close", true, gameWindow) function clickHandler() executeCommandHandler("snake") end addEventHandler("onClientGUIClick",playSnake,clickHandler,false) Edited March 2, 2012 by Guest Link to comment
Castillo Posted March 2, 2012 Share Posted March 2, 2012 That was an example, you can't use it like that, you must change "myButton" to your button argument. Link to comment
Absence2 Posted March 2, 2012 Author Share Posted March 2, 2012 That was an example, you can't use it like that, you must change "myButton" to your button argument. Edited my post and I know, I'm not that big of a noob EDIT: Wait a minute.. I fixed it, I had added the onguiclick outside the eventhandler. Link to comment
Absence2 Posted March 2, 2012 Author Share Posted March 2, 2012 So it's working now? Yup, it's working, thanks for the help, both of you I'm just worried about this one: bad argument @ 'addEventHandler' [expected element at arguement 2, got nil] Link to comment
Castillo Posted March 2, 2012 Share Posted March 2, 2012 If the button is inside a function then you must add the event handler inside as well. Link to comment
Kenix Posted March 2, 2012 Share Posted March 2, 2012 RainyLawyer,Learn https://wiki.multitheftauto.com/wiki/Scr ... troduction https://wiki.multitheftauto.com/wiki/Int ... ng_the_GUI Link to comment
Absence2 Posted March 2, 2012 Author Share Posted March 2, 2012 RainyLawyer,Learnhttps://wiki.multitheftauto.com/wiki/Scr ... troduction https://wiki.multitheftauto.com/wiki/Int ... ng_the_GUI I am working on it, I guess I'm just slow.. If the button is inside a function then you must add the event handler inside as well. And yes, you're right, sorry, I didn't realize that the error had dissapeared after correcting the first issue. Link to comment
Castillo Posted March 2, 2012 Share Posted March 2, 2012 There's nothing wrong on being slow. You're welcome btw. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now