• Print

Author Topic: Need help coding a simple file for TTT  (Read 7281 times)

0 Members and 1 Guest are viewing this topic.

Offline Larftus

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Need help coding a simple file for TTT
« on: July 20, 2016, 08:22:04 am »
Code: Lua
  1. if (SERVER) then
  2.         util.AddNetworkString( "SendClientSCount" ) //S stands for Special (T and D)
  3.         util.AddNetworkString( "SendClientICount" ) //I (obviously) stands for Innocent
  4.        
  5.         local TCount = 0
  6.         local DCount = 0
  7.         local ICount = 0
  8.         for k, v in pairs( player.GetAll() ) do
  9.             if v:IsTraitor() and v:Alive() then
  10.                 TCount=TCount+1
  11.             elseif v:IsDetective() and v:Alive() then
  12.                 DCount=DCount+1
  13.             elseif v:Alive() then
  14.                 ICount=ICount+1;
  15.             end
  16.         end
  17.         for k, v in pairs( player.GetAll() ) do
  18.             if ICount == 1 and TCount >= 1 then
  19.                 PrintMessage( HUD_PRINTCENTER, "Gents Rule")
  20.             elseif DCount == 1 and TCount >= 1 then
  21.                 PrintMessage( HUD_PRINTCENTER, "Gents Rule")
  22.             end
  23.         end
  24. end
  25.  

Basically what I am trying to go for is that when there is one innocent/detective left and one or more traitors left, the server displays a message in the center of the screen.  I am not used to the Lua language, so this is all confusing to me.  Also, where would I put this file in the server directory in order for it to work? Thank you.
            

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Need help coding a simple file for TTT
« Reply #1 on: July 20, 2016, 01:00:46 pm »
Most people on these forums have other commitments and thus have a limited amount of time to spend helping people. To make the most use of this time, it is very helpful if you specify what the issue you're having is: What you expect the code to do, what is happening instead, any pertinent errors, etc. Doing this is little extra work on your part, and greatly increases the chances of getting a response.

Since your code has no syntax errors and I do not know what you want this code to do (it seems to me that it will display a message if there is one innocent/detective and one or more traitors left), I cannot help you with that aspect.

As for where you should place this file: it depends. Sorry for the vague reply, but again without more information I can't really help. When do you want this code to run? This code doesn't seem like it would make sense to run once the map loads, as nobody is assigned any roles at that point.

Hopefully you will clarify your issue so we can assist you better.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Larftus

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: Need help coding a simple file for TTT
« Reply #2 on: July 20, 2016, 01:33:22 pm »
Most people on these forums have other commitments and thus have a limited amount of time to spend helping people. To make the most use of this time, it is very helpful if you specify what the issue you're having is: What you expect the code to do, what is happening instead, any pertinent errors, etc. Doing this is little extra work on your part, and greatly increases the chances of getting a response.

Since your code has no syntax errors and I do not know what you want this code to do (it seems to me that it will display a message if there is one innocent/detective and one or more traitors left), I cannot help you with that aspect.

As for where you should place this file: it depends. Sorry for the vague reply, but again without more information I can't really help. When do you want this code to run? This code doesn't seem like it would make sense to run once the map loads, as nobody is assigned any roles at that point.

Hopefully you will clarify your issue so we can assist you better.

Sorry I am new to this forum.

Question 1: What I expect the code to do?
Answer: I expect the code to show the phrase "Gents Rule" across the center of the screen when there is only one innocent/detective alive and one or more traitors alive, because some players on my server who are new do not use this rule. I wont waste your time explaining it.

Question 2: What is happening/errors?
Answer: I have not run this code yet, simply because I do not know where it goes.  Does it go in lua->autorun? Does it go in terrortown->gamemodes? I have no clue because Lua is like writing a paper in German to me. So I hope I can find out if this is a GMOD thing or a TTT thing, and then determine where I should place the file so that it executes.

Question 3: When do you want this code to run?
Answer: I want it to run when only one innocent or only one detective is alive and the rest of the player count is Traitors.

Here is example of how I want this code to work:

-3 Players alive
-2 are Innocents, 1 is Traitor
-1 Innocent dies
-"Gents Rule" appears in center of screen, possibly plays a sound

Again sorry for sounding too vague when I posted this.  I usually don't mess with lua, and anything that I do is easily searchable online.  But my player base is requesting stuff like this to get added, and because I am getting donations and such I feel obligated to fulfill their requests. Thank you.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Need help coding a simple file for TTT
« Reply #3 on: July 20, 2016, 03:57:42 pm »
Sorry I am new to this forum.

No worries, everyone was new at some point in time :)

Question 3: When do you want this code to run?
Answer: I want it to run when only one innocent or only one detective is alive and the rest of the player count is Traitors.

Here is example of how I want this code to work:

-3 Players alive
-2 are Innocents, 1 is Traitor
-1 Innocent dies
-"Gents Rule" appears in center of screen, possibly plays a sound

Okay, so you want the code to run when a certain criterion is met. Since the number of innocents, traitors, and detectives can only change when a player dies (in vanilla TTT that is; I'll cover possible edge cases in a bit), you can use the Hook Library.

That wiki page that I linked to contains a more in-depth explanation, but I'll summarize it for you: Most major things that happen in Garry's Mod will trigger an event. Developers can set code to run when these events are triggered. This is called "hooking" into the event, hence the name of the library.

Basically, you want to use hook.Add to hook into GM:PostPlayerDeath (this is just one of numerous death hooks; this one looked like the best fit to me but you can look at the others on the wiki). Put your current code in a function, and supply hook.Add with that function as the hook's callback. This file with the function should be put in lua/autorun, either by putting it in the actual garrysmod/lua/autorun folder or creating a new addon.

Let me know if you have any more questions.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need help coding a simple file for TTT
« Reply #4 on: July 21, 2016, 02:42:47 am »
You may wish to monitor Player disconnect hook too.
Death and Disconnect would cause same condition, a change in teams.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Need help coding a simple file for TTT
« Reply #5 on: July 21, 2016, 11:08:23 am »
You may wish to monitor Player disconnect hook too.
Death and Disconnect would cause same condition, a change in teams.

Thanks for mentioning it, I was a bit rushed for time and forgot about disconnecting.

Looking back on my post, I realize I also forgot to cover edge cases. Basically, if you have any custom features that allow players to change role mid round you're going to have issues. You'll either have to modify these addons to call an event and then hook the same check into those events or just live with the fact that it isn't perfect.
« Last Edit: July 21, 2016, 11:21:32 am by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Larftus

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: Need help coding a simple file for TTT
« Reply #6 on: July 21, 2016, 07:46:11 pm »
Thanks for mentioning it, I was a bit rushed for time and forgot about disconnecting.

Looking back on my post, I realize I also forgot to cover edge cases. Basically, if you have any custom features that allow players to change role mid round you're going to have issues. You'll either have to modify these addons to call an event and then hook the same check into those events or just live with the fact that it isn't perfect.

I rarely allow traitor changes during the round, so I wont worry about that.

Anyway, I tried out the code with my 3 friends and killed one of them so that it was 1 innocent and 1 traitor left alive.  Nothing appeared in chat.

Code: Lua
  1. function gents()
  2. if SERVER then
  3.     util.AddNetworkString( "SendClientSCount" ) //S stands for Special (T and D)
  4.     util.AddNetworkString( "SendClientICount" ) //I (obviously) stands for Innocent
  5.        
  6.     local TCount = 0
  7.     local DCount = 0
  8.     local ICount = 0
  9.                 for k, v in pairs( player.GetAll() ) do
  10.             if v:IsTraitor() and v:Alive() then
  11.                 TCount=TCount+1
  12.             elseif v:IsDetective() and v:Alive() then
  13.                 DCount=DCount+1
  14.             elseif v:Alive() then
  15.                 ICount=ICount+1;
  16.             end
  17.         end
  18.         for k, v in pairs( player.GetAll() ) do
  19.             if ICount == 1 and TCount >= 1 then
  20.                                 PrintMessage( HUD_PRINTTALK, "Gents Rule")
  21.                         elseif DCount == 1 and TCount >= 1 then
  22.                                 PrintMessage( HUD_PRINTTALK, "Gents Rule")
  23.             end
  24.         end
  25.         end
  26. end
  27.  
  28. hook.Add( "PostPlayerDeath", "gentsrule", gents( )
  29. )

That's the code, I might need help messing with it to get this to work. Thanks guys. I also decided it to show up in chat instead because of popular demand on my server.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Need help coding a simple file for TTT
« Reply #7 on: July 21, 2016, 11:45:59 pm »
Anyway, I tried out the code with my 3 friends and killed one of them so that it was 1 innocent and 1 traitor left alive.  Nothing appeared in chat.

Code: Lua
  1. hook.Add( "PostPlayerDeath", "gentsrule", gents( ) )

When you use the function calling notation (name of function followed by parentheses), you're not passing the function but rather its return value. Since your gents function returns nothing, you are effectively passing nil. Instead you should pass the function itself, like so:

Code: Lua
  1. hook.Add( "PostPlayerDeath", "gentsrule", gents )
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Larftus

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: Need help coding a simple file for TTT
« Reply #8 on: July 22, 2016, 01:03:02 pm »
When you use the function calling notation (name of function followed by parentheses), you're not passing the function but rather its return value. Since your gents function returns nothing, you are effectively passing nil. Instead you should pass the function itself, like so:

Code: Lua
  1. hook.Add( "PostPlayerDeath", "gentsrule", gents )

ok i will try this. everything else looks ok?

Offline Larftus

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: Need help coding a simple file for TTT
« Reply #9 on: July 23, 2016, 12:46:57 pm »
Ok so I added the thing and the message shows up.  But there are a few issues.

First off when its detective vs traitor it does not show up.  Secondly, the code works great until more than 4 people are on. Third, it shows up in the chat after the first person died.

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Need help coding a simple file for TTT
« Reply #10 on: July 24, 2016, 02:04:06 am »
I've created something like this before.

To make things easier on yourself, make the Detective and Innocent roles the same variable to check with.

Since they are playing on the same side, you don't need to specify both.
Out of the Garry's Mod business.

Offline [?] The Last Centurion

  • Newbie
  • *
  • Posts: 23
  • Karma: 2
Re: Need help coding a simple file for TTT
« Reply #11 on: July 29, 2016, 07:45:59 pm »
I've created something like this before.

To make things easier on yourself, make the Detective and Innocent roles the same variable to check with.

Since they are playing on the same side, you don't need to specify both.

I can't really be of any assistance to this because I is a scrub, but yes, making the Innocents and the Detectives one variable is easier, because there's only two "teams" and technically there are only two teams as Detectives are like "special" innocents.

  • Print