How could I have handled this network notification system better?

I have a question about how I could have solved this problem better especially because my solution is not scalable in terms of Big-O. So on gmod there is this pointshop, It has a function that sends a notifications to the player:

simplified version for example purpose
SERVER

function Player:PS_Notify(…)
net.Start(‘PS_SendNotification’)
end

CLIENT

net.Receive(‘PS_SendNotification’, function(length)
displayNotification()
end

Now this works for 1 notification, but If i have different Icons or different styled notifications. I don’t want to have to create this

util.AddNetworkString(‘PS_SendNotification1 - version 1’)
util.AddNetworkString(‘PS_SendNotification2 - version 2’)
util.AddNetworkString(‘PS_SendNotification3 version 3’)

Different scenarios require different notifications. Is using this many network strings bad networking? Lucky I only had 3 notification styles because this solved my problem. I just feel kinda dirty. How could I have solved this better? Maybe I should have created a notification handler that activates the notification sequence but has logic that chooses which notification to launch? what would that involve, Local Booleans I’m guessing ?

:cake:

Why not send “version” to the client as an integer and then call the notification that you need based on that “version”?

Well, if I understand it right, if you pass less data to a message it will be more faster. So, different network string for each notification is more preferable if performance is important to you.

But still, if you want to customize your notification from server you can do this.

Shared:

ICON_TYPE_NONE = 0
ICON_TYPE_WARN = 1
ICON_TYPE_ERROR = 2

Server:

util.AddNetworkString("PS_SendCustomizableNotification")
function PS_SendCustomizableNotification(ply, iconType)
    net.Start("PS_SendCustomizableNotification")
    net.WriteUInt(iconType, 2)
    net.Send(ply)
end
PS_SendCustomizableNotification(ICON_TYPE_WARN)

Client:

net.Receive("PS_SendCustomizableNotification", function(len)
    local iconType = net.ReadUInt(2) // 2 bits = [0..3]
    displayNotification(iconType)
end)

Same with messages, but you have to do this:

local messages = {
    [MSG_TYPE_HELLO] = "Hello, World",
    [MSG_TYPE_TEST] = "Test message",
}
net.Receive("PS_SendCustomizableNotification", function(len)
    local msgType = net.ReadUInt(1) // 1 bit = [0..1]
    local msgString = messages[msgType]
    displayNotification(msgString)
end)
1 Like

We are talking here about 2 Bits when WriteUInt is used with a max value of 3

thats alot less than 3 entire netstrings
here why:

  • you can only have 2048 netstrings in total (may be increased in the future)
  • the netstring itself needs to be networked to client too

sending 2 bits more in a net message that may get sent sometimes is not worth the extra 2 netstring slot usage when you use different names instead (its basicly like creating a global variable: not good)

1 Like