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

0 Members and 2 Guests 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: 2728
  • 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: 2728
  • 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: 2728
  • 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 :)

An Error Has Occurred!

array_keys(): Argument #1 ($array) must be of type array, null given