net.WriteEntity?

Suppose I make custom variable server side on my player entity such as:
player._Money
player._Level
player._Points

and then I use net.WriteEntity(player) so the player can receive it…
does the player receive a copy of the object or just the client-side reference? In which case, he can’t access those server side variables.


(User was banned for this post ("undescriptive thread title" - Gran PC))

Just the clientside reference. If you want to network values from the server to the client, you should probably use data tables.

Here’s something to build on.
[lua]

– Serverside –

if ( SERVER ) then

util.AddNetworkString( "Stat" );

local meta = FindMetaTable( "Player" );

function meta:SetStat( sStat, Value )
	self[ sStat ] = Value;
	net.Start( "Stat" );
		net.WriteType( Value );
	net.Send( self );
end

function meta:GetStat( sStat )
	return self[ sStat ];
end

end


– Clientside –

if ( CLIENT ) then

stats = {};

function GetStat( sStat )
	return stats[ sStat ];
end

net.Receive( "Stat", function( iLen )
	stats[ net.ReadString() ] = net.ReadType( net.ReadUInt( 8 ) );
end );

end

–[[
Usage:

	GetStat( "Money" );
	
The function is global, so yeah..

]]–[/lua]

Thanks, but I didn’t ask for this purpose. :slight_smile:

In my experience, when you save properties to an entity and then send them with net, the properties go with the message.

However, the message, when it is received, doesn’t just point to the entity which it refers to, it’s a copy of the entity table. This means if you want it to save to the clientside version of the entity, you need to find the clientside entity which is the same as the net entity, which can be done with a loop through all players, and change the clientside player object’s properties to the net’s properties.

If that makes any sense to you…

net.Write/ReadEntity work as so:
[lua]
function net.WriteEntity( ent )

if ( !IsValid( ent ) ) then 
	net.WriteUInt( 0, 32 )
else
	net.WriteUInt( ent:EntIndex(), 32 )
end

end

function net.ReadEntity( ent )

local i = net.ReadUInt( 32 )
if ( !i ) then return  end

return Entity( i )

end
[/lua]

No server-side properties of an entity are sent within a call to net.WriteEntity()

@Willox, Thanks :slight_smile:

Did that actually happen to you? It doesn’t match the code given by willox.

0 evaluates to true in Lua, just saying.

This is Garry’s code, but Entity(0) is the expected output (being NULL), I suppose !i is there to make sure it receives data.

@Willox
Is it safe to override SetNetworkedInt with something like this:



function _R.Entity:SetNetworkedInt(id, value)
		net.Start("networkedint")
		net.WriteEntity(self)
		net.WriteString(id)
		net.WriteInt(value)
		net.Broadcast();
end

--Clientside
net.Receive("networkedint", function (len)

	local entity = net.ReadEntity()
	local id = net.ReadString()
	local value = net.ReadInt()
        entity.networkedInts[id] = value;

end )

--SHARED:
function _R.Entity:GetNetworkedInt(id)
	return self.networkedInts[id];
end


Ofcouse, new players will get updated when they join the servers.

I don’t know if you’ll get any real benefit out of doing something like that.

Really? why not?

I’m not sure of the specific disadvantages to the default SetNWInt. If you’re looking for an alternative you should probably use differently named functions.

Because that’s more or less how networked variables work already.

@_Killburn, where can I find the code of Setnetworked*?