Cunning fox Posted January 21, 2019 Share Posted January 21, 2019 (edited) Hey everyone! I've created a library that sends data between the clients without server code. This is very useful when you want to create a client mod and send something to other players with your mod. First of all you need to download the library from here: Then place it into your mod folder(MOD/scripts/tools). local ClientData = require("tools/client_data")(modinfo.name) And now you can create a command for sending: ClientData:AddCommand(name, Fn) --Fn arguments: caller (guid, userid, name, prefab), ... And send your command with: ClientData:SendCommand(name, ...) For example there's my mod that prints something for other players that use this mod: local _G = GLOBAL local require = _G.require local ClientData = require("tools/client_data")(modinfo.name) ClientData:AddCommand("test", function(entity, str) print("TEST", str) end) _G.TheInput:AddKeyDownHandler(_G.KEY_F1, function() ClientData:SendCommand("test", math.random()) end) Edited January 21, 2019 by Cunning fox 1 Link to comment Share on other sites More sharing options...
CarlZalph Posted January 21, 2019 Share Posted January 21, 2019 I await for mods to induce yet even more chat spam. I'd recommend putting a rate limiter via a queue on the library since chat commands can only be sent every 0.1 seconds. Sending too many commands in a short time span would cause a loss of messages. Personally I think the better approach to client<->client communication is to have a third party server setup for use with get/post requests with TheSim:QueryServer. No chat spam, can use common web-based scripting solutions like PHP/perl/go/etc to handle things, and would allow for multiple third party servers setup for specific things if needed. Downside of course being the requirement of a third party server to handle the communication line, and the maintenance thereof. Link to comment Share on other sites More sharing options...
Developer zarklord_klei Posted January 23, 2019 Developer Share Posted January 23, 2019 (edited) Nicely done, uses code very similar to my own for client networking, got most of the things right. just fyi, unless all clients have the mod that "spams" the chat via this, your gonna see all these messages pop up as a client without this mod. Also a couple things I noted that your probably gonna want to clean up to improve performance with your code is as follows: local _Networking_Say = Networking_Say Networking_Say = function(guid, userid, name, prefab, message, ...) if string.sub(message, 1, 5) == self.prefix then --by splitting these up, you prevent weird edge cases where the prefix is correct, but somehow the other statements return false and a message gets to chat that shouldnt. if ThePlayer ~= nil and ThePlayer.userid ~= userid then self:HandleCommand(string.sub(message, 6), {guid, userid, name, prefab}) end return end return _Networking_Say(guid, userid, name, prefab, message, ...) end function ClientData:SendCommand(name, ...) --local sendstr = self.prefix.." "..name.."|"..json.encode({...}) --first things first, every character you add to the message makes the game send one more byte of data, so you want as few characters as possible, so we get rid of the space. --second thing, though this can to some extent be preference, but DataDumper({...},nil, true) compactly stores lua data as a string that can be loadstring-ed local sendstr = self.prefix..name.."|"..DataDumper({...}, nil, true) TheNet:Say(sendstr, false, true) end function ClientData:HandleCommand(str, entity) local formated = string.split(str, "|") --because that space is gone, we dont need to loop over the commands to find the correct one. local fn = self.commands[formated[1]] if fn then --another bug, you did a check for #formated[1] > 0, which could never be false. --use loadstring if you used DataDumper, but unless you did some tests and found that one was way larger than the other, its personal preference imo. local data = #formated[2] > 0 and loadstring(formated[2]) or {} fn(entity, unpack(data)) end end Edited January 23, 2019 by Zarklord Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now