• Print

Author Topic: chat.AddText does not work right  (Read 13729 times)

0 Members and 1 Guest are viewing this topic.

Offline DH1806

  • Newbie
  • *
  • Posts: 38
  • Karma: 0
chat.AddText does not work right
« on: January 19, 2015, 07:40:31 am »
Hey Guys, I've used many hours for the following thing and I just can't imagine what the problem could be. It's about the chat.AddText function. It seems like it's changing values that are given to it, even if it is a string. But here's the code:
Code: [Select]
function sendMessage(inumber, dnumber, x)
local inno = ""
local dete = ""
local lol = ""

inno = inumber
dete = dnumber
lol = x
if(CLIENT) then
chat.AddText(inno,dete,lol)
chat.AddText( color_white , "Diese Runde gibt es " , Color(0,235,0), inno , " Innocents, " , Color(0,0,235), dete, " Detectives", color_white, " und " ,  Color(255,7,0), lol, " Traitor.")
end
print ("i wrote "..lol)
end

The function gets the values inumber (Number of Innocents), dnumber (Number of Detectives) and x (Number of Traitors). The values are right, I did a print() function so I could check it. I've made them to a string ( local XYZ = tostring(ICount)) so they can be written down with chat.AddText. The "print ("i wrote "..lol)" should check if the lol value was given right to the function. It always writes down the right values that were send in the console. The only thing which does not work is the chat.Add() function. It says lol would be 1, even if it's 2 and the print function after the chat.Add() also says that lol is 2. Same with the other values. I just don't understand why it seems that only the chat.Add() changes the values, even if it's a string it changes the numbers.
Does anyone has an idea why or how to fix it? :)

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: chat.AddText does not work right
« Reply #1 on: January 19, 2015, 08:11:25 am »
which lua state are you calling sendMessage from?
Server or Client?

You can't just wrap if CLIENT then into a serverside call. For that, you'd need to send the values to the client using a net message and then use chat.AddText

Offline DH1806

  • Newbie
  • *
  • Posts: 38
  • Karma: 0
Re: chat.AddText does not work right
« Reply #2 on: January 19, 2015, 08:30:49 am »
Thanks for your answer. I wrote nothing there, but it should be serverside. It's a simple function in lua/autorun.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: chat.AddText does not work right
« Reply #3 on: January 19, 2015, 08:36:09 am »
http://wiki.garrysmod.com/page/chat/AddText

If the function is being called Serverside, then the if CLIENT bits won't be called correctly.
To do this properly what you should look into doing is sending a net message to the clients using net.Broadcast with the 3 variables you want to include in the chat.AddText then generating the chat message from a clientside net.Receive function.

http://wiki.garrysmod.com/page/net/Broadcast
http://wiki.garrysmod.com/page/net/Receive
http://wiki.garrysmod.com/page/net/WriteInt
http://wiki.garrysmod.com/page/net/ReadInt

Those links should get you started.

Offline DH1806

  • Newbie
  • *
  • Posts: 38
  • Karma: 0
Re: chat.AddText does not work right
« Reply #4 on: January 19, 2015, 08:51:12 am »
Thanks a lot, i will look into them later today.

Offline DH1806

  • Newbie
  • *
  • Posts: 38
  • Karma: 0
Re: chat.AddText does not work right
« Reply #5 on: January 19, 2015, 02:03:50 pm »
Is there a way to send more than one value per net.Start() ? The problem is that the net.ReadInt() function only deals with the bytes so I think there could be sent only one value per net.Start() . I'd like to get the message with 3 values in one line, so that's a problem here. Any ideas how to solve it? Can it even be solved? :D

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: chat.AddText does not work right
« Reply #6 on: January 19, 2015, 03:00:04 pm »
You can send more than one value per message. Just use multiple net.WriteInt.

Just keep in mind that on the client side you read them in the same order as they were written.

Later when i get home from work I'll write you an example.

Offline DH1806

  • Newbie
  • *
  • Posts: 38
  • Karma: 0
Re: chat.AddText does not work right
« Reply #7 on: January 19, 2015, 03:22:37 pm »
Thanks m8 for your help, i'll try it out tomorrow :)

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: chat.AddText does not work right
« Reply #8 on: January 19, 2015, 08:00:33 pm »
Try this.
This is what you need to make your code work but with using net messages.
Key points are commented so you can learn from it.

Just call your function sendMessage as you would have before.

Code: Lua
  1. if SERVER then
  2.  
  3.         util.AddNetworkString( "myNetMessage" ) --Every NetMessage has to have a unique identifier (like a hook) and it has to be initialized on the Server side before it can be used
  4.        
  5.         function sendMessage( inumber, dnumber, tnumber )
  6.                 net.Start( "myNetMessage" )
  7.                         net.Write( inumber, 8 ) --This function writes an 8bit integer to the netmessage. You can make the bit count whatever you want, but it's cheaper on the network to send as small of an integer as you need to.
  8.                         net.Write( dumber, 8 )
  9.                         net.Write( tnumber, 8 )
  10.                 net.Broadcast() --Broadcast sends it to every player connected
  11.         end
  12.        
  13. end
  14.  
  15. if CLIENT then
  16.  
  17.         net.Receive( "myNetMessage", function( len )
  18.                 local inno = net.ReadInt( 8 ) --Notice we'll read the net messages in the order they were sent.
  19.                 local dete = net.ReadInt( 8 )
  20.                 local terr = net.ReadInt( 8 )
  21.                
  22.                 chat.AddText( Color(255,255,255) , "Diese Runde gibt es " , Color(0,235,0), inno , " Innocents, ", Color(0,0,235), dete, " Detectives", Color(255,255,255), " und ", Color(255,7,0), terr, " Traitors.")
  23.         end )
  24.        
  25. end
  26.  

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: chat.AddText does not work right
« Reply #9 on: January 19, 2015, 08:02:35 pm »
Oh.. it's untested too.. so let me know if you have any questions or if it doesn't work.

It should work though... net code is very easy to write. :)

Offline DH1806

  • Newbie
  • *
  • Posts: 38
  • Karma: 0
Re: chat.AddText does not work right
« Reply #10 on: January 20, 2015, 07:39:08 am »
Thanks for your example, but sadly it does not yet work. :/ Here's my whole code:

No error message in the console, but there appears simply no message in the chat.
Any ideas why?

Edit: OK, there was only net.Write not net.WriteInt :D Finally after many, many hours I found a working solution :) Thanks a lot !

Code: Lua
  1. if (SERVER) then
  2.  
  3.         util.AddNetworkString( "sendMessage" )
  4.        
  5.     hook.Add( "TTTBeginRound", "NumberOfTypes", function()
  6.                 local TCount = 0
  7.                 local DCount = 0
  8.                 local ICount = 0
  9.    
  10.                 for k, v in pairs( player.GetAll() ) do
  11.                         if v:IsTraitor() and v:Alive() then
  12.                                 TCount=TCount+1
  13.                         elseif v:IsDetective() and v:Alive() then
  14.                                 DCount=DCount+1
  15.                         elseif v:Alive() then
  16.                                 ICount=ICount+1;
  17.                         end
  18.                 end
  19.    
  20.                 net.Start( "sendMessage" )
  21.                         net.WriteInt( TCount, 8 )
  22.                         net.WriteInt( DCount, 8 )
  23.                         net.WriteInt( ICount, 8 )
  24.                 net.Broadcast()
  25.                        
  26.     end )
  27. end
  28.  
  29. if CLIENT then
  30.  
  31.         net.Receive( "sendMessage", function( len )
  32.                 local terr = tostring(net.ReadInt( 8 ))
  33.                 local dete = tostring(net.ReadInt( 8 ))
  34.                 local inno = tostring(net.ReadInt( 8 ))
  35.  
  36.                 chat.AddText( color_white , "Diese Runde gibt es " , Color(235,0,0), terr, " Traitor, ", Color(0,0,235), dete, " Detectives", color_white, " und " , Color(0,235,0), inno , " Innocents." )
  37.         end )
  38.  
  39. end
« Last Edit: January 20, 2015, 08:03:01 am by DH1806 »

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: chat.AddText does not work right
« Reply #11 on: January 20, 2015, 07:42:20 am »
in your hook you're doing
return TCount, DCount, ICount

That will exit the function before the timer.Simple is run.

Just remove that line. It's unneeded.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: chat.AddText does not work right
« Reply #12 on: January 20, 2015, 07:50:17 am »
Also, put this at the top of the file.
AddCSLuaFile()

Offline DH1806

  • Newbie
  • *
  • Posts: 38
  • Karma: 0
Re: chat.AddText does not work right
« Reply #13 on: January 20, 2015, 08:18:34 am »
Thanks for your help, it worked fine, there is one last single problem. Spectators are counted as Innocents too, is there something like a player.IsSpectator() function? or could maybe player.GetHumans instead of players.GetAll() solve that problem?

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: chat.AddText does not work right
« Reply #14 on: January 20, 2015, 10:09:56 am »
That's more of a TTT thing and I have no experience with that.
GetHumans only filters out bots.


  • Print