Shared.Message() ?

TinCanTinCan Join Date: 2006-12-11 Member: 59010Members
I'm trying to get the server to print something to a client's console. I found Shared.Message() in ConsoleCommands_Server so that looked hopeful to me but now in testing I cannot seem to get the existing list_players command to print anything. Is that supposed to work? Maybe a game needs to be in progress or other players exist or something?

More importantly How can I get the server to print something to the client's console?

Comments

  • BeigeAlertBeigeAlert Texas Join Date: 2013-08-08 Member: 186657Members, Super Administrators, Forum Admins, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, NS2 Map Tester, Reinforced - Diamond, Reinforced - Shadow, Subnautica Playtester, Pistachionauts
    You'll need to hook up a network message for that, and use the Client only command "ChatUI_AddSystemMessage(string)"

    So your code might look like this:
    Shared.RegisterNetworkMessage("MyMessageName", { text = "string (256)" })
    if Server then
        function ServerSendSystemMessage(text)
            Server.SendNetworkMessage("MyMessageName", { text = text }, true)
        end
    end
    if Client then
        function OnClientReceiveSystemMessage(message)
            ChatUI_AddSystemMessage(message.text)
        end
        Client.HookNetworkMessage("MyMessageName", OnClientReceiveSystemMessage)
    end
    
  • GhoulofGSG9GhoulofGSG9 Join Date: 2013-03-31 Member: 184566Members, Super Administrators, Forum Admins, Forum Moderators, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, Reinforced - Supporter, WC 2013 - Supporter, Pistachionauts
    edited January 2017
    BeigeAlert wrote: »
    You'll need to hook up a network message for that, and use the Client only command "ChatUI_AddSystemMessage(string)"

    So your code might look like this:
    Shared.RegisterNetworkMessage("MyMessageName", { text = "string (256)" })
    if Server then
        function ServerSendSystemMessage(text)
            Server.SendNetworkMessage("MyMessageName", { text = text }, true)
        end
    end
    if Client then
        function OnClientReceiveSystemMessage(message)
            ChatUI_AddSystemMessage(message.text)
        end
        Client.HookNetworkMessage("MyMessageName", OnClientReceiveSystemMessage)
    end
    

    In case you don't want to construct this yourself you can also just use
    ServerAdminPrint(client, message)
    
    ;)
  • TinCanTinCan Join Date: 2006-12-11 Member: 59010Members
    edited January 2017
    Still struggling here...
    I think I'm close but no joy. It could just be I'm doing something wrong but the trouble with troubleshooting is my mod acts differently when testing locally launching from launchpad to a published mod loaded on a dedicated server.

    @BeigeAlert
    When I use your method:
    Using Launchpad: Text shows up on screen for a bit (kind of cool) but nothing in the console.
    Real Server: Nothing on screen or in console but the text does show up in the log. (close)

    @GhoulofGSG9
    When I use your method:
    Using Launchpad: Text shows up in the console (hooray!) but it shows up twice (meh).
    Real Server: Nothing in log or console. (boo)


  • BeigeAlertBeigeAlert Texas Join Date: 2013-08-08 Member: 186657Members, Super Administrators, Forum Admins, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, NS2 Map Tester, Reinforced - Diamond, Reinforced - Shadow, Subnautica Playtester, Pistachionauts
    edited January 2017
    To have it show up in console, have it print the same text using the Log() command. It takes a format string and any number of parameters. If you run it on server, it will show up in the server console only, so you'll need to have it on the client as well.

    What other mods are on the server? Might be one of them is overriding the text behavior.
  • TinCanTinCan Join Date: 2006-12-11 Member: 59010Members
    edited January 2017
    Still having trouble and I'm sure it's my confusion. I guess I'm not clear how to get the message to the client on demand. Should it look something like this?

    Shared.RegisterNetworkMessage("MyMessageName", { text = "string (256)" })
    if Server then
        function ServerSendSystemMessage(text)
            Server.SendNetworkMessage("MyMessageName", { text = text }, true)
        end
    end
    if Client then
        function OnClientReceiveSystemMessage(message)
            ChatUI_AddSystemMessage(message.text)
        end
        Client.HookNetworkMessage("MyMessageName", OnClientReceiveSystemMessage)
    end
    
    function printit(client)
        if Server then
    -- Here is where the server will open a file and read the text 
    -- that I want to send to the client's console, but for example
    -- I'll just do this:  
        MyMessageName = "testing"
        end
        if Client then
            log(MyMessageName)
        end
    end
    
    Event.Hook("Console_printit", printit)
    
  • GhoulofGSG9GhoulofGSG9 Join Date: 2013-03-31 Member: 184566Members, Super Administrators, Forum Admins, Forum Moderators, NS2 Developer, NS2 Playtester, Squad Five Blue, Squad Five Silver, Reinforced - Supporter, WC 2013 - Supporter, Pistachionauts
    edited January 2017
    TinCan wrote: »
    Still having trouble and I'm sure it's my confusion. I guess I'm not clear how to get the message to the client on demand. Should it look something like this?

    Shared.RegisterNetworkMessage("MyMessageName", { text = "string (256)" })
    if Server then
        function ServerSendSystemMessage(text)
            Server.SendNetworkMessage("MyMessageName", { text = text }, true)
        end
    end
    if Client then
        function OnClientReceiveSystemMessage(message)
            ChatUI_AddSystemMessage(message.text)
        end
        Client.HookNetworkMessage("MyMessageName", OnClientReceiveSystemMessage)
    end
    
    function printit(client)
        if Server then
    -- Here is where the server will open a file and read the text 
    -- that I want to send to the client's console, but for example
    -- I'll just do this:  
        MyMessageName = "testing"
        end
        if Client then
            log(MyMessageName)
        end
    end
    
    Event.Hook("Console_printit", printit)
    

    I would suggest to have a look at the Network Message part of my modding guide draft at https://goo.gl/128GvS .

    That ServerAdminPrint(client, message) shows the message twice in the console of a local server (assuming you send it to the hosting client) is an expected behavior as the given method prints both into server's and target client's console. At a local server both console are the same so the message shows up twice ;)

    Also feel free to join the #modding channel at our discord if you have further project specific questions.
  • TinCanTinCan Join Date: 2006-12-11 Member: 59010Members
    Finally got it sorted out.

    I had not set my mod.settings file to Read-only and (arg!) the publish_id had changed so when I thought I was testing new code on the server, I wasn't. That explains a lot, So frustrating.

    I ended up going with
    local function NotifyPlayer(player, message)
        Server.SendNetworkMessage(player, "ServerAdminPrint", { message = message }, true)
    end
    
    
    NotifyPlayer(player, Message)
    


    I would suggest to have a look at the Network Message part of my modding guide draft at https://goo.gl/128GvS .
    Great stuff. +1



Sign In or Register to comment.