• [Fretta] Recording players usernames for use within local function.
    9 replies, posted
Hi all, Basically, the Prop Hunt gamemode that I'm running on a server doesn't indicate in chat when a player switches teams. As you can imagine, that is an issue, and so I've spent the past few hours digging in all of the source code to find where the actual team switch line was, and lo' and behold, I found it within Fretta; [CODE]local func = function() RunConsoleCommand( "changeteam", ID ) end[/CODE] The issue I face is that I'm not seeing anyway to turn this local function into something that can record the username of the switching player and display it in chat everytime the function is called. While getting the actual message to print is simple, I'm struggling to figure out how to record and insert the username of each player who switches within the gamemode into this chat message. The local function is apart of this larger function within the [B]cl_selectscreen.lua[/B] file: [CODE]function GM:ShowTeam() if ( !IsValid( TeamPanel ) ) then TeamPanel = vgui.CreateFromTable( vgui_Splash ) TeamPanel:SetHeaderText( "Choose Team" ) local AllTeams = team.GetAllTeams() for ID, TeamInfo in SortedPairs ( AllTeams ) do if ( ID != TEAM_CONNECTING && ID != TEAM_UNASSIGNED && ( ID != TEAM_SPECTATOR || GAMEMODE.AllowSpectating ) && team.Joinable(ID) ) then if ( ID == TEAM_SPECTATOR ) then TeamPanel:AddSpacer( 10 ) end local strName = TeamInfo.Name local func = function() RunConsoleCommand( "changeteam", ID ) ChatPrint( " have been swapped!") end local btn = TeamPanel:AddSelectButton( strName, func ) btn.m_colBackground = TeamInfo.Color btn.Think = function( self ) self:SetText( Format( "%s (%i)", strName, team.NumPlayers( ID ) )) self:SetDisabled( GAMEMODE:TeamHasEnoughPlayers( ID ) ) end if ( IsValid( LocalPlayer() ) && LocalPlayer():Team() == ID ) then btn:SetDisabled( true ) end end end[/CODE] Does anyone have any experience with this particular issue, or something similar? Any assistance would be incredible.
I believe [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/OnPlayerChangedTeam]GM:OnPlayerChangedTeam[/url] will be the hook that helps you out [editline]25th September 2016[/editline] Just go ahead and hook.Add a function that announces the change. You'll be just fine
Where should I place this? Inside the fretta code in OP or in the actual Prop Hunt code? Kinda new to GLua sorry :/
You can put it in as an "autorun/server/*.lua" and add it there. Or in the init.lua for prop hunt. Basically anywhere the server will load it. Since you're new make sure you're using hook.Add and not defining the function again.
Alright, so I simply just place something like: [CODE]function GM:OnPlayerChangedTeam( Player ply, number oldTeam, number newTeam ) ChatPrint( ply .. " has changed team!") [/CODE] And then place this in the init file on the gamemode: [CODE] hook.Add (OnPlayerChangedTeam)[/CODE] Everything should work? Thank you for the help so far!
No no, I suggest you read up on the [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/hook/Add]hook.Add[/url] function. It'll be your friend for adding things without overwriting any of the base. With what you posted it would be: [code]hook.Add( "OnPlayerChangedTeam", "WHATEVER IDENTIFIER YOU WANT", function( ply, oldteam, newteam ) PRINTING FUNCTION HERE end ) [/code] [editline]25th September 2016[/editline] ChatPrint is not going to work. I assume you want to useb[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/PrintMessage]PrintMessage[/url] though be weary of the character limit. In general the wiki will be very helpful for what works and what doesn't. If you're new, read up
Alright, so I simply drop something like this into the init file on the gamemode? [CODE]hook.Add( "OnPlayerChangedTeam", "ChangeNotif", function( ply, oldteam, newteam ) PrintMessage( HUD_PRINTTALK, ply .. " has changed team!" ) end )[/CODE]
[QUOTE=Pixelrain;51107680]Alright, so I simply drop something like this into the init file on the gamemode? [CODE]hook.Add( "OnPlayerChangedTeam", "ChangeNotif", function( ply, oldteam, newteam ) function ( Player ply, number oldTeam, number newTeam ) PrintMessage( HUD_PRINTTALK, ply .. " has changed team!" ) end )[/CODE][/QUOTE] That wont work. Try putting this in a file in autorun/server/ [CODE]hook.Add( "OnPlayerChangedTeam", "ChangeNotif", function( ply, oldteam, newteam ) PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has changed team!" ) end )[/CODE]
[QUOTE=Pixelrain;51107680]Alright, so I simply drop something like this into the init file on the gamemode? [CODE]hook.Add( "OnPlayerChangedTeam", "ChangeNotif", function( ply, oldteam, newteam ) function ( Player ply, number oldTeam, number newTeam ) PrintMessage( HUD_PRINTTALK, ply .. " has changed team!" ) end )[/CODE][/QUOTE] Please read the Wiki, instead of just putting something together and ask if it works. [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/hook/Add]hook.Add[/url] is explained in detail, and the examples show you how you should use the function. As for using PrintMessage, that would work. It is also ill-advised to edit gamemode files, just put that inside lua/autorun/server/anyfile.lua
[QUOTE=JasonMan34;51107696]Please read the Wiki, instead of just putting something together and ask if it works. [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/hook/Add]hook.Add[/url] is explained in detail, and the examples show you how you should use the function. As for using PrintMessage, that would work. It is also ill-advised to edit gamemode files, just put that inside lua/autorun/server/anyfile.lua[/QUOTE] Apologies, I read the wiki and was just struggling to wrap my head around it as the examples seemed pretty barebones in context of making it work with an existing gamemode. Thank you to all who helped so far, I will test it right now! [editline]26th September 2016[/editline] Thanks to all the help here guys, I re read the wiki and I finally figured out hooks properly with the first example. Appreciate all the support, for anyone in future reading this thread, this is the code that worked; [CODE]function teamChange( ply, oldteam, newteam ) PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has changed team!" ) end hook.Add( "OnPlayerChangedTeam", "ChangeNotif", teamChange )[/CODE] Didn't really understand until now you're adding the function above to that event occurring in game, and that the game handles the hook actually occurring. Knoxed code will also work, didn't when I first ran it but the small changes I made to init in the actual gamemode may have been the root cause as to why not. Regardless, thank you all!
Sorry, you need to Log In to post a reply to this thread.