Jump to content

Hide button


RandomRambo

Recommended Posts

Hello guys.
I need to create a button in front of my created image and on button K (for example) I need to hide the image and the button (and also cursor,but i did it).

So,I can hide cursor and an image but I can't hide the button idk why.

Code:

local x,y = guiGetScreenSize() 
local elementd = true
local screenW,screenH = guiGetScreenSize()
local resW,resH = 1280,720
local sW,sH =  (screenW/resW), (screenH/resH)

function elementshowd ()
if elementd == false then
elementd = true
showCursor(false)
guiSetVisible(event, false)
else
elementd = false
end
end
bindKey( "K", "down", elementshowd)

function eventshows ()
if elementd == false then
    showCursor ( true )	
    event = guiCreateButton(16, 20, 250, 40, "Take", false)
addEventHandler ( "onClientGUIClick", event, Tokens )

---------

dxDrawImage(500*sW, 120*sH, 370*sW, 470*sH, "img/event.png", 0, 0, 0, tocolor(255, 255, 255, 255), true)
end
end
addEventHandler ( "onClientRender", getRootElement(), eventshows )

 

Help me please to hide the button :/

Edited by RandomRambo
Link to comment

guiCreateButton is a persistent button - it creates a button element, not renders a button for a frame -- you should never call it in onClientRender except in if-statements which you know will only execute once when necessary (i.e. not every frame, and only if the previously created button was destroyed).

Setting elementd to false in your code merely stops creating new buttons -- it does not delete the previously created buttons (yes, buttons, plural. You're creating hundreds of buttons every couple seconds depending on the FPS of the client).

This code

event = guiCreateButton(16, 20, 250, 40, "Take", false)
addEventHandler ( "onClientGUIClick", event, Tokens )

and this code

function eventshows ()
  if elementd == false then
    dxDrawImage(500*sW, 120*sH, 370*sW, 470*sH, "img/event.png", 0, 0, 0, tocolor(255, 255, 255, 255), true)
  end
end
addEventHandler ( "onClientRender", getRootElement(), eventshows )

should generally never exist in the same function

Link to comment
6 hours ago, Addlibs said:

guiCreateButton is a persistent button - it creates a button element, not renders a button for a frame -- you should never call it in onClientRender except in if-statements which you know will only execute once when necessary (i.e. not every frame, and only if the previously created button was destroyed).

Setting elementd to false in your code merely stops creating new buttons -- it does not delete the previously created buttons (yes, buttons, plural. You're creating hundreds of buttons every couple seconds depending on the FPS of the client).

This code

event = guiCreateButton(16, 20, 250, 40, "Take", false)
addEventHandler ( "onClientGUIClick", event, Tokens )

and this code

function eventshows ()
  if elementd == false then
    dxDrawImage(500*sW, 120*sH, 370*sW, 470*sH, "img/event.png", 0, 0, 0, tocolor(255, 255, 255, 255), true)
  end
end
addEventHandler ( "onClientRender", getRootElement(), eventshows )

should generally never exist in the same function

So can you show me an example how I can create a button in front of this image and then hide it with it.

Link to comment
4 hours ago, RandomRambo said:

Create  an image and then create a button in front of image and on for example "K" hide it or show it

You mean like that?

function AttachDxToGuiElement( GUIElement , img )
if ( guiGetVisible ( GUIElement ) ) then 
local x, y = guiGetPosition ( GUIElement , false )
local width , Height = guiGetSize ( GUIElement , false )
dxDrawImage(x, y, width  , Height ,img, 0, 0, 0,SetImagecolor or tocolor(255, 0, 0, 255), true)
dxDrawText(guiGetText ( GUIElement ) or tostring '',x, y, width + x , Height + y,SetTextcolor or tocolor(55,55,55,255),1,"default-bold","center","center",false,false,true)
guiBringToFront ( GUIElement )
guiSetAlpha ( GUIElement , 0)
end
end
------------------
event = guiCreateButton(16, 20, 250, 40, "Take", false)

addEventHandler ("onClientRender", root,
    function ()
         AttachDxToGuiElement( event ,'img/event.png' )
    end)

bindKey( "k", "down",function( )
		guiSetVisible( event , not  guiGetVisible( event ))
		showCursor( guiGetVisible( event ))
	end
)

 

  • Thanks 1
Link to comment
On 16/08/2022 at 00:48, #\_oskar_/# said:

You mean like that?

function AttachDxToGuiElement( GUIElement , img )
if ( guiGetVisible ( GUIElement ) ) then 
local x, y = guiGetPosition ( GUIElement , false )
local width , Height = guiGetSize ( GUIElement , false )
dxDrawImage(x, y, width  , Height ,img, 0, 0, 0,SetImagecolor or tocolor(255, 0, 0, 255), true)
dxDrawText(guiGetText ( GUIElement ) or tostring '',x, y, width + x , Height + y,SetTextcolor or tocolor(55,55,55,255),1,"default-bold","center","center",false,false,true)
guiBringToFront ( GUIElement )
guiSetAlpha ( GUIElement , 0)
end
end
------------------
event = guiCreateButton(16, 20, 250, 40, "Take", false)

addEventHandler ("onClientRender", root,
    function ()
         AttachDxToGuiElement( event ,'img/event.png' )
    end)

bindKey( "k", "down",function( )
		guiSetVisible( event , not  guiGetVisible( event ))
		showCursor( guiGetVisible( event ))
	end
)

 

Ye,but it shows when I start resource,but It should show if someone jujst press 'K' (not when resource started).
And the button doesn't work,it is just as not a button it is just like a text

 

Upd.Button works,but can I just make a button with my image?

spacer.png

for example this (and text from button will be on it.Is it possible?

Edited by RandomRambo
Link to comment

When I try to add one more button it shows only 1 button anyway.What am I doing wrong?:\

function AttachDxToGuiElement( GUIElement , img )
if ( guiGetVisible ( GUIElement ) ) then 
 showCursor ( true )	
local x, y = guiGetPosition ( GUIElement , false )
local width , Height = guiGetSize ( GUIElement , false )
dxDrawImage(500*sW, 120*sH, 370*sW, 470*sH, "img/event.png", 0, 0, 0, tocolor(255, 255, 255, 255), true)
dxDrawText(guiGetText ( GUIElement ) or tostring '',x, y, width + x , Height + y,SetTextcolor or tocolor(223,247,0,255),1,"default-bold","center","center",false,false,true)
guiBringToFront ( GUIElement )
guiSetAlpha ( GUIElement , 0)
end
end
------------------
event = guiCreateButton(740, 800, 250, 40, "Take", false)
event1 = guiCreateButton(890, 700, 250, 40, "Take", false)

addEventHandler ("onClientRender", root,
    function ()
         AttachDxToGuiElement( event ,'img/event.png' )
         AttachDxToGuiElement( event1 ,'img/event.png' )
    end)

bindKey( "K", "down",function( )
		guiSetVisible( event , not  guiGetVisible( event ))
		guiSetVisible( event1 , not  guiGetVisible( event1 ))
		showCursor( guiGetVisible( event ))
		 showCursor ( false )
	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...