• Derma Recreation
    7 replies, posted
Heys, I'm quite new to GLua, though not Lua. I've browsed through quite a few addons/gamemodes to see the flow of code and was wondering why mostly everyone recreated their derma panels? Is there a performance issue to this? I know that for things such as inventories it would be more plausible that way as you don't have to worry about allocating variables, but what about static panels with non-changing elements?
Can you provide an example? In most cases it's all the same. I just do it because that's just how I've always done it, and unless I need it to be different, I keep it!
Both examples taken from the old wiki: [lua] function testpanel() -- Create the function test = vgui.Create("DFrame") -- Create the frame menu1 = vgui.Create("DButton") -- Create the button test:SetPos(50,50) -- set the frame's position on the screen test:SetSize(300, 300) -- set the frame size test:SetTitle( "Test" ) -- set the frame title test:SetVisible( true ) -- Make the frame visible test:MakePopup() -- make the frame popup menu1:SetParent( test ) -- parent the button to the frame menu1:SetText( "Menu >" ) -- set the button text menu1:SetPos(150, 150) -- set the button position in the frame menu1:SetSize( 100, 20 ) -- set the button size menu1.DoClick = function ( btn ) -- this will be called when the button is clicked local menu123 = DermaMenu() -- create a derma menu menu123:AddOption("hello", function() Msg("Hello") end ) -- adding options menu123:AddOption("how", function() Msg("How") end ) menu123:AddOption("are", function() Msg("Are") end ) menu123:AddOption("you", function() Msg("You") end ) menu123:AddOption("?", function() Msg("?") end ) menu123:Open() end -- ending the doclick function end -- ending the function concommand.Add("menutest", testpanel) -- adding the console command [/lua] versus [lua] function frame_create() --Note that I changed the function name. You can change the fucntion name to anything you want frame = vgui.Create("DFrame") frame:SetPos(50,50) frame:SetSize(500,350) frame:SetTitle("Testing Derma Controls") frame:SetVisible(false) --This is important to set false or your frame will pop-up every time you join to server or a single player game. frame:SetDraggable(true) frame:ShowCloseButton(true) frame:SetBackgroundBlur(false) frame:MakePopup() button = vgui.Create("DButton",frame) --Note that I have changed the variables to button and frame. This is important to you. button:SetText("Suicide") button:SetPos(5,27) button:SetSize(150,50) button.DoClick = function() error("Derma Is Da Best! (Left Click)") end button.DoRightClick = function() error("Woohoo! (Right Click)") end end function frame_open() frame:SetVisible(true) end function frame_close() frame:SetVisible(false) end concommand.Add("-our_concommand",frame_close) concommand.Add("+our_concommand",frame_open) hook.Add("Initialize","frame_create",frame_create) [/lua] I'm beginning to think that it's just a GLua paradigm...
A better approach is to register a derma panel with [URL="http://wiki.garrysmod.com/page/vgui/Register"]vgui.Register[/URL]. This is exactly what all base derma controls in gmod do: [URL]https://github.com/garrynewman/garrysmod/tree/master/garrysmod/lua/vgui[/URL] An example of registering a custom panel: [lua]local PANEL = {} function PANEL:Init() ... end vgui.Register("CustomControl", PANEL, "DPanel")[/lua] To create an instance of the control: [lua]local custom = vgui.Create("CustomControl")[/lua] Additional, slightly out-dated, documentation can be found here: [URL]https://github.com/garrynewman/garrysmod/blob/ff51a59d5a821dec3c8f524631c86150b3e4744d/garrysmod/lua/includes/extensions/client/panel/scriptedpanels.lua[/URL]
Er.. that's not quite what I'm asking. I'm wondering if there is a specific reason why people recreate their derma panels using vgui.Create() instead of just caching it in a global variable and using Panel:SetVisible().
just bad code, it's better to hide and show than delete and recreate
I recreate sometimes when I'm making the code, that way I can change it and reopen it to refresh it rather than reconnect.
just consider this when trying to decide if you should hide/show or destroy and recreate: do i change anything? If everything stays the same (or at least more than some percentage lets say 60%) stays the same then you should probably be hideing and showing and simply updating the layout if the screen resolution has changed (if you really care, if you plan on selling it you should) If most of the content changes each time then it's usually easier to simply recreate since it's simpler coding and you're updating alot of values anyway so there is little point in caching. but in answer to your question it's primarily a function of how lazy the coder is feeling when they write it.
Sorry, you need to Log In to post a reply to this thread.