Messing around with a gamemode:
I want to create a menu that will pop up when a player presses a button (for now, "Q").
Currently, this is on my cl_init.lua:
[CODE]
function mMenu()
local ply = LocalPlayer()
local BackGround = vgui.Create( "DFrame" )
BackGround:SetSize( 200, 90 )
BackGround:SetPos( (ScrW()/2)-BackGround:GetWide(), (ScrH()/2)-BackGround:GetTall() )
BackGround:SetTitle( "Menu" )
BackGround:SetVisible( true )
BackGround:SetDraggable( true )
BackGround:ShowCloseButton( true )
BackGround:MakePopup()
local AButton = vgui.Create( "DButton", BackGround )
AButton:SetSize( 160, 40 )
AButton:SetText( "Button" )
AButton:SetPos( 20, 30 )
end
end
function KeyPressed (ply, key)
if (key == 1024)
then
mMenu()
end
Msg (ply:GetName().." pressed "..key.."\n")
end
hook.Add( "KeyPress", "KeyPressedHook", KeyPressed )
[/CODE]The menu will pop up when the player presses "d", as that is key "1024" apparently. However I can't seem to find the key code for "q". Looking through the gmod wiki, it seems like "KeyPressed" may not be the right function to use. Maybe "Player.KeyDown"? But I have no idea how to implement that. Before I start going down the wrong road, could someone give some input?
Thanks,
Jeremy
[B][URL="http://wiki.garrysmod.com/?title=Gamemode.OnSpawnMenuOpen"]Gamemode.OnSpawnMenuOpen [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B]
[B][URL="http://wiki.garrysmod.com/?title=Gamemode.OnSpawnMenuClose"]Gamemode.OnSpawnMenuClose [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B]
Please use wiki next time.
Also a example if you want it to be like the spawn menu.
[LUA]local Models = {
"models/props/CS_militia/newspaperstack01.mdl",
"models/props/cs_office/coffee_mug.mdl",
"models/props/cs_office/coffee_mug2.mdl",
"models/props/cs_office/coffee_mug3.mdl",
"models/props/cs_office/Chair_office.mdl",
"models/props/cs_office/computer.mdl",
"models/props/cs_office/computer_caseB.mdl",
"models/props/cs_office/computer_mouse.mdl",
"models/props/cs_office/file_cabinet1_group.mdl",
"models/props/cs_office/Shelves_metal.mdl",
"models/props/cs_office/sofa.mdl",
"models/props/cs_office/sofa_chair.mdl",
"models/props/cs_office/Table_coffee.mdl",
"models/props_junk/PlasticCrate01a.mdl",
"models/props_junk/PushCart01a.mdl",
"models/props_junk/TrafficCone001a.mdl",
"models/props_junk/wood_crate001a.mdl",
"models/props_junk/TrashBin01a.mdl"
}
local frame = vgui.Create("DFrame")
local IconList = vgui.Create( "DPanelList", frame )
frame:SetPos((ScrW() / 2) - 175, (ScrH() / 2) - 200)
frame:SetSize(350,400)
frame:SetTitle("Spawn Menu")
frame:SetDraggable(false)
frame:ShowCloseButton(false)
frame:SetVisible(false)
IconList:EnableVerticalScrollbar( true )
IconList:EnableHorizontal( true )
IconList:SetPadding( 4 )
IconList:SetPos(10,30)
IconList:SetSize(330, 360)
for k,v in pairs(Models) do
local icon = vgui.Create( "SpawnIcon", IconList )
icon:SetModel( v )
IconList:AddItem( icon )
icon.DoClick = function( icon ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gm_spawn", v) end
end
function GM:OnSpawnMenuOpen()
frame:SetVisible(true)
gui.EnableScreenClicker(true)
end
function GM:OnSpawnMenuClose()
frame:SetVisible(false)
gui.EnableScreenClicker(false)
end
[/LUA]
[QUOTE=yuriman;22426635][B][URL="http://wiki.garrysmod.com/?title=Gamemode.OnSpawnMenuOpen"]Gamemode.OnSpawnMenuOpen [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B]
[B][URL="http://wiki.garrysmod.com/?title=Gamemode.OnSpawnMenuClose"]Gamemode.OnSpawnMenuClose [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B]
Please use wiki next time.
[/QUOTE]
[QUOTE=JTG2003;22425797]
[B] Looking through the gmod wiki[/B], it seems like "KeyPressed" may not be the right function to use. [/QUOTE]
I did. But as this is not Sandbox nor am I looking to create a spawn menu, I didn't think to search for such things.
Thanks, though
i see.
no problem
EDIT:
When i said like the spawn menu i ment it to be you need to hold down Q to have it open.
That works great. However I've run into another roadblock. I want to run functions that need to be called by the server (Ex: SetMaxHealth() ). Is there a way to call a server function (in init.lua) from the client (cl_init.lua)?
Since shared is included in cl_init, can I put the SetMaxHealth in shared and reference shared.lua from cl_init.lua? I know this is getting off topic now, but I hate making a new topic for every question.
Basically, when I press a button in the derma frame defined in cl_init, I want to be able to change the max health (and other things, but if I can understand this, I can do the rest I think)
Usermessages :eng101:
To send data from cl_init to be used in init.lua
No concommand is only way for client->server transfer :pseudo:
Ok, maybe we take a different approach. Maybe I setup a variable on the client that the server continually looks for.
So the server is constantly updating MaxHealth by getting that variable from the player.
Is that possible? Seems like it would bog down the server though..
I'm attempting concommand now...
EDIT:
By using SetVar, GetVar, and concommand, I am able to accomplish what I want. By pressing a button in the frame, it executes a concommand which is picked up by shared. Shared checks the variable and executes a server command if appropriate.
The only issue I have right now is I can't reference the player in cl_init. I tried saying, for example, ply = LocalPlayer() but it didn't seem to work. Any thoughts? The idea is using GetVar in cl_init to check variables...
After using ply = LocalPlayer(), this is what I get in the console for an error:
gamemodes\test\gamemode\cl_init.lua:56: attempt to index global 'ply' (a nil value)
This is line 56:
stat1:SetText( ply:GetVar("points") )
EDIT2:
Starting a new topic as this has gotten far from the original post. Thanks for the help!
[QUOTE=HeavyMtl123;22458499]Usermessages :eng101:
To send data from cl_init to be used in init.lua[/QUOTE]
[QUOTE=Tobba;22458678]No concommand is only way for client->server transfer :pseudo:[/QUOTE]
Do people really have to start this every time someone says the word usermessages or the word concommand?
Sorry, you need to Log In to post a reply to this thread.