Net messaging

Hello. Well I’ve pretty much learnt most of the fundamentals of glua and understand what things do what but I just can’t get my head around net messaging. I don’t understand it at all.
I guess if someone can explain they could use this example:
When I press a derma button it removes 50 points from the player (you could use pointshop if you like)

I just don’t understand how this all works :confused: it will be a mega help for anyone who explains this thanks anyway. Before anyone mentions I have checked the wiki about it (sorry for the bad grammar and stuff, a little tired)

I mainly use NetMessages to send important player-specific info straight to the server or execute a function on the client. Server to client: I sometimes will tell the client to open a derma menu or chat.AddText with net.WriteString. Client to server: I have a Loadout menu addon for TTT in which a player uses a derma menu to choose their weapons and click a button which uses net.WriteTable and net.SendToServer to tell the server to give those weapons. Your example would have a button that sends a generic netmessage to the server and when the Server recieves it, it would remove 50 points from the player.

Probably not the best explanation but its how I use them. Net messages are really useful for a large array of purposes. This is an amazing tutorial on it.

ok but where do i put the code to remove 50 points?


   function Example()

       print( "Removing Points....." )
       net.Start( "Example1" )
           --Remove points code here?



util.AddNetworkString( “some_fancyfunctionwithnovalidation” )

net.Receive( “some_fancyfunctionwithnovalidation”, function( len, ply )
local subtractAmount = net.ReadUInt(32)
ply.points = ply.points - subtractAmount


function TakeMyPointsDammit( )
net.Start( “some_fancyfunctionwithnovalidation” )
net.WriteUInt( 10, 32 )

Client Sends message to server which contains some info > Server gets the message and processes it.

If you’re making a system where players buy things, you send to server the itemID, and the server processes if the client can afford it / whether they have it / etc.
Lots of magic.

Example for networking booleans ( although there may be a new function set specifically for that now ):

I do have an “understanding realms by networking example” where there are 3 different receives; 1 client, 1 server, and 1 shared. It shows how executing code in different places with the shared receive will give different results ( running from client, or server, etc )… That will come with my basic networking system I’m set to release in the very near future.

The important thing to note is that client and server are separate. Shared code doesn’t necessarily mean that it’ll be executed on both the server AND the client at the same time. It may only execute on the client depending how it is written. Some shared code is executed primarily on the server ( certain logic ) and then a networked message is sent to the client letting the client know to call the hook; it happens so fast that it seems as though it is executed on the client and server at the exact same time, but usually it isn’t.

Here’s an example:

Simple console command will show you how things are executed…

Thanks for the help acecool also Paella what essentially does

 net.WriteUInt( 10, 32 ) 


Writes an Unsigned Integer. Signed integers are with - / + at the front. Unsigned are without
and can only represent positive numbers.

So, this is writing 10 in 32 bits which is overkill… to write the value 10, we’d only need 4 bits… 0000 1010 – The number is even, so the last digit it 0, it needs a 2 ( and the 8 ) to create 10 ( via addition ) so only the 5th and 7th bits are set to 1. We can omit the first 4 for the number.

With a signed integer, you need to allocate the bits used by + or -.

Bit counting is straight forward if you know what to look for.

8 bits to a byte: 0000 0000
If the last one is 1, then the number as a whole is odd. Each bit ( starting from the right )
is in power of 2.

From left to right ( where rightmost is 8th, and leftmost is 1st for sake of example.
They’re typically “read” from right to left )
8th is 2^0 which == 1.
7th is 2^1 == 2
6th is 2^2 == 4
5th is 2^3 == 8
4th is 2^4 == 16
3rd is 2^5 == 32
2nd is 2^6 == 64
1st is 2^7 == 128

How many bits are needed to write the number depends on signed / unsigned, and the total of the number.
If you add ALL 8 bits together: 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 == 255. So, if you send an unsigned
int that is 255, you’d need to use 8 bits to send it.

This is how I was taught in other languages, it should be the same here.

Hopefully this is straight-forward…

Obviously you would never be sending a constant value over net, it would clearly be a value that can change. Prices of items would, I assume, be able to go indeterminately high. This is especially true if you have a p2p trading system where nothing has a set price.

In that case you would clearly use a 32 unsigned bits, and then max out the value at 4 billion. It’s not like anyone’s network is going to choke and die when wasting 20 precious bits or whatever.

Agreed, I just wanted to help the user understand why the values were set the way they were; but never trust the client to give the price of anything. In a trade window obviously it would be between two clients with the server to verify.