Hey there, I was trying to make an SWEP that showes up an DPanel when used and removes it when you use it again.
I ran into some problems, so I tryed to remove the DPanel with the Secondary Attack. But since I dId this, they automaticly spamm the function so the DPanel is always open and the console get spammed. I would be glad if someone could give me some advise what I did wrong.
function SWEP:PrimaryAttack()
self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
print( "sucess" )
local panel = vgui.Create( "DPanel")
panel:SetSize( 314, 445 )
panel:SetPos( 20, 120 )
function panel:Paint( w, h )
surface.SetDrawColor(255, 255, 255, 255)
surface.SetMaterial(Material("hud/nineliner.jpg"))
surface.DrawTexturedRect(0, 0, self:GetWide(), self:GetTall())
print("create")
end
end
function SWEP:SecondaryAttack()
local panel = vgui.Create( "DPanel")
function panel:Paint( w, h )
panel:Remove()
end
end
before I tryed this, my Code looks something like this:
local isactive = false
function SWEP:PrimaryAttack()
self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
print( "sucess" )
if isactive == false
local panel = vgui.Create( "DPanel")
panel:SetSize( 314, 445 )
panel:SetPos( 20, 120 )
function panel:Paint( w, h )
surface.SetDrawColor(255, 255, 255, 255)
surface.SetMaterial(Material("hud/nineliner.jpg"))
surface.DrawTexturedRect(0, 0, self:GetWide(), self:GetTall())
print("create")
isactive = true
end
else
local panel = vgui.Create( "DPanel")
function panel:Paint( w, h )
panel:Remove()
isactive = false
end
end
pls dont hit me if the second one misses sone "end" or something, I just made it up here really quick.
Add
panel:SetVisible(true)
Then try it
https://files.facepunch.com/forum/upload/469093/9cc32179-64e9-4f82-9f75-89925cdbc5b5/image.png
vgui works like this:
when vgui.Create is called it returns a table with panel functions etc.
so you cant expect vgui to get the panel you created previously by calling vgui.Create again.
What i mean is just make something like this
local panel
function SWEP:PrimaryAttack()
self:SetNextPrimaryFire( CurTime() + 3 )
print( "sucess" )
panel = vgui.Create( "DPanel", panel)
panel:SetSize( 314, 445 )
panel:SetPos( 20, 120 )
function panel:Paint( w, h )
surface.SetDrawColor(255, 255, 255, 255)
surface.SetMaterial(Material("hud/nineliner.jpg"))
surface.DrawTexturedRect(0, 0, self:GetWide(), self:GetTall())
panel:SetVisible(true)
end
end
function SWEP:SecondaryAttack()
self:SetNextSecondaryFire( CurTime() + 3 )
function panel:Paint( w, h )
panel:SetVisible(false)
print("remove")
end
end
This seems to work, but somehow only works for the first time after I load the game, after that it only prints "sucess" in the console but neither do anything about showing the panel again, nor print "remove" a second time.
https://files.facepunch.com/forum/upload/469093/ab31ef83-2636-4ad7-a42a-41babaea3407/Screenshot_2019-05-21-23-03-20-325_com.android.chrome.png
?
What is that?
Why is it here?
when I create the Panel in the beginning of the script, how do I make it visible when not by using setVisible inside the paint function?
Just put it before or after the function.Thats it.
I got it to work, thank you very much @PalaBeaveR m <3
Sorry, you need to Log In to post a reply to this thread.