• Derma menu doesn't open til I reupload the file
    29 replies, posted
I have a derma menu, located in lua/autorun/client, that only wants to open after I reupload the file (after I rejoin) [lua]local w = ( ( ScrW() / 2 ) + ( ScrW() / 4 ) ); local h = ( ( ScrH() / 2 ) + ( ScrH() / 4 ) ); local p = 10; Frame = vgui.Create( "DFrame" ); Frame:SetTitle( "Radio" ); Frame:SetSize( w + (p * 2), h + (p * 6) ); Frame:SetDraggable( true ); Frame:ShowCloseButton( false ); Frame:SetVisible( false ); Panel = vgui.Create( "DPanel", Frame ); Panel:SetPos( 8, 32 ); Panel:SetSize( w, h ); HTML = vgui.Create( "HTML", Panel ); HTML:SetPos( 8, 8 ); HTML:SetSize( w - (p * 2), h - (p * 2) ); HTML:OpenURL( "http://www.pandora.com/" ); local closeButton = vgui.Create( "Button", Frame ); closeButton:SetSize( 150, 20 ); closeButton:SetPos( 8, ( ( ScrH() / 2 ) + ( ScrH() / 4 ) ) + (p*3.5) ); closeButton:SetVisible( true ); closeButton:SetText( "Close" ); function closeButton:OnMousePressed() Frame:SetVisible( false ) end function open() Frame:SetVisible( true ) Frame:MakePopup() Frame:Center() end hook.Add( "OnPlayerChat", "radioCommand", function( ePlayer, sText ) if ( ePlayer ~= LocalPlayer() ) then return; end if ( string.StartWith( string.lower( sText ), "!radio" ) ) then open(); return; end end );[/lua] It's a radio, so that's why I have the open / close things set up that way.
What do you mean you have to reupload the file after you rejoin? I'm not following you here. Also, use local functions (the open() one).
When I join, I type !radio, but it doesn't work. I then reupload the same file to my server, and it begins working magically.
Are you doing "AddCSLuaFile" on the server?
Just added "AddCSLuaFile( "autorun/client/radio.lua" )" to the top of that code. Didn't work... but I'm most likely not doing it right.
You need to call "AddCSLuaFile" [I]serverside[/I], so the server can know that it should send that file in the OP to the clients. If you do "AddCSLuaFile" on the client, then you are basically telling the client to send its own file to itself which it initially never has any knowledge of (technically incorrect, but logically a good example).
Tried adding "AddCSLuaFile( "autorun/client/radio.lua" )" to my gamemode's init.lua and my resource.lua file (in lua/autorun/server); still didn't work, until I reupload the file while I'm in the server.
I believe the reason it works only after you upload it while you're on the server is because of Garry's weird-as-fuck auto refresh system. Also, do a "print( "something" )" on the top of radio.lua to see if/when the file is actually running.
Yea, it only prints "something" when I reupload the file, but not when I join the server.
You need to do the AddCSLuaFile first, then [url=http://wiki.garrysmod.com/page/Global/include]include it.[/url] Including it runs the file.
Alright, at the top of radio.lua put this: [lua] AddCSLuaFile() -- Tell the client to download this file if not CLIENT then return end -- Stop further execution if we are serverside [/lua] And put it in the base "autorun/" directory.
Nope still not working. Just realised I get this error when joining the server [code][ERROR] lua/autorun/radio.lua:9: attempt to index global 'Frame' (a nil value) 1. unknown - lua/autorun/radio.lua:9[/code] That's the "Frame:SetTitle( "Radio" );" line.
Well first of all localize "Frame", "Panel" and whatever else you have in there. [B]Always[/B] use locals unless you have a good reason [I]not[/I] to. At least it's not running serverside else it would error on the first "vgui" line. After creating the "DFrame", try do "print( Frame.SetTitle )" and give me the results, maybe Garry removed it/changed it? [editline]9th August 2013[/editline] Also why are you using semicolons? It's not needed, and it ruins consistency because you're not doing it everywhere, only in some places.
Now I get [code] [ERROR] lua/autorun/radio.lua:9: attempt to index local 'Frame' (a nil value) 1. unknown - lua/autorun/radio.lua:9[/code] Also, after reuploading, this prints to console: function: 0x311f1a28 Then nothing else prints to console when I type !radio
That's odd, Frame is clearly a valid panel object (because the "print( Frame.SetTitle )" printed a function), but the error is saying that it's nil. Can you give me the entire script as it is right now?
[lua]AddCSLuaFile() -- Tell the client to download this file if not CLIENT then return end -- Stop further execution if we are serverside local w = ( ( ScrW() / 2 ) + ( ScrW() / 4 ) ) local h = ( ( ScrH() / 2 ) + ( ScrH() / 4 ) ) local p = 10 local Frame = vgui.Create( "DFrame" ) Frame:SetTitle( "Radio" ) Frame:SetSize( w + (p * 2), h + (p * 6) ) Frame:SetDraggable( true ) Frame:ShowCloseButton( false ) Frame:SetVisible( false ) print( Frame.SetTitle ) local Panel = vgui.Create( "DPanel", Frame ) Panel:SetPos( 8, 32 ) Panel:SetSize( w, h ) local HTML = vgui.Create( "HTML", Panel ) HTML:SetPos( 8, 8 ) HTML:SetSize( w - (p * 2), h - (p * 2) ) HTML:OpenURL( "http://www.pandora.com/" ) local closeButton = vgui.Create( "Button", Frame ) closeButton:SetSize( 150, 20 ) closeButton:SetPos( 8, ( ( ScrH() / 2 ) + ( ScrH() / 4 ) ) + (p*3.5) ) closeButton:SetVisible( true ) closeButton:SetText( "Close" ) function closeButton:OnMousePressed() Frame:SetVisible( false ) end local function open() Frame:SetVisible( true ) Frame:MakePopup() Frame:Center() end hook.Add( "OnPlayerChat", "radioCommand", function( ePlayer, sText ) if ( string.StartWith( string.lower( sText ), "!radio" ) ) then open() return end end )[/lua] In lua/autorun
Try putting that in [I]your[/I] (not the server's) "lua/" folder. And then go to singleplayer and run "lua_openscript_cl <nameoffile>.lua"
Nothing happens
You're supposed to type "!radio" to test it
Yes I know
According to the [b][url=http://gmodwiki.net/Lua/Hooks/Base/OnPlayerChat]Lua/Hooks/Base/OnPlayerChat[img]http://gmodwiki.net/favicon.ico[/img][/url][/b], "OnPlayerChat" is only called when another player types a message, I don't know if that's correct or not though. Try doing a "print( "test" )" in the hook. See if it prints "test" in your console when you type something in chat.
It doesn't print anything (before or after I upload)
I don't really know what you're doing wrong then right now. I'll help you again when I come home.
If you want to open it when you type something in chat use this : [url=http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index8519.html]Link Here[/url]
Alright, I just started again, reverse engineering some other random scripts. lua/autorun/client/radio.lua [lua]function OpenRadio() local MenuFrame = vgui.Create( "DFrame" ) MenuFrame:SetSize( ScrW() * 0.95, ScrH() * 0.95 ) MenuFrame:SetPos( (ScrW() - MenuFrame:GetWide()) / 2, (ScrH() - MenuFrame:GetTall()) / 2 ) MenuFrame:SetTitle( "Radio" ) MenuFrame:SetVisible( true ) MenuFrame:SetDraggable( true ) MenuFrame:ShowCloseButton( false ) MenuFrame:SetBackgroundBlur( true ) MenuFrame.Paint = function() local backdrop = surface.GetTextureID( "incognito/backdrop.vtf" ) surface.SetTexture( backdrop ) surface.SetDrawColor( 255, 255, 255, 255 ) surface.DrawTexturedRect( 0, 0, MenuFrame:GetWide(), MenuFrame:GetTall() + 1 ) end MenuFrame:MakePopup() local RADIO = vgui.Create( "HTML", MenuFrame ) RADIO:SetPos( 25, 30 ) RADIO:SetSize( ScrW() * 0.9 + 25, ScrH() * 0.9 - 25 ) RADIO:OpenURL( "http://pandora.com" ) local CloseButton = vgui.Create( "DButton", MenuFrame ) CloseButton:SetSize( 150, 35 ) CloseButton:SetPos( 25, MenuFrame:GetTall() - 25 - CloseButton:GetTall()/2 ) CloseButton:SetText( "Close" ) CloseButton.DoClick = function( button ) MenuFrame:SetVisible( false ) end end concommand.Add("openradio", OpenRadio)[/lua] lua/autorun/server/sv_radio.lua [lua]AddCSLuaFile( "autorun/client/radio.lua" ) function ChatRADIOMenuPopup( ply, text ) local prefixs = { "!radio", "/radio", "radio" } local command = string.Explode(" ", text)[1] for _, v in pairs( prefixs ) do if string.lower( command ) == v then ply:ConCommand( "openradio" ) end end end hook.Add( "PlayerSay", "ChatRADIO", ChatRADIOMenuPopup )[/lua] Problem now is, when I close and type !radio again, the song lags (because the page refreshes). Any way around this?
Did you tried a single command ? I tried once to add multiple and I added them wrong and after I added only one it worked.
What?
[QUOTE=incognitocob;41780934] Problem now is, when I close and type !radio again, the song lags (because the page refreshes). Any way around this?[/QUOTE] Store a local refrence to the frame in that lua file outside of the function and then you will be able to check if it exists. Then you can do something like [code] if IsValid(MenuFrame) then MenuFrame:SetVisible(true) else -- Rest of code end[/code]
I have no idea how to do that... quite new to lua.
[QUOTE=incognitocob;41783902]I have no idea how to do that... quite new to lua.[/QUOTE] The simplest (but not really 100% the best) way would be to do [lua] local MenuFrame = nil function OpenRadio() if IsValid(MenuFrame) then MenuFrame:SetVisible(true) else MenuFrame = vgui.Create( "DFrame" ) -- Rest of code goes here, don't forget the extra "end" here [/lua] Alternatively, you might want to use gui.OpenURL instead, which would be a simple: [code] function OpenRadio() gui.OpenURL( "http://pandora.com" ) end[/code] This has the advantage of staying open while you do other stuff and even if you leave the server.
Sorry, you need to Log In to post a reply to this thread.