net.Recieve not working...

Hi all,
I’m adding now an inventory to my RP gamemode. I already made the render ; the inventory rendering is correct. Now i want the server Player.inventory table to be sended and recieved by the client. The problem is that the client seams to never recieve any data from the server. I can send inventory 1000 times ore more (even in a for loop), nothing is recieved by the client, the net.Recieve that i put in my cl_init.lua won’t be called.

I tried sending at GM:PlayerSpawn but not working, i tried too on GM:PlayerInitialSpawn but same.

Here is the code :
Server side
InventoryManager:OnInventoryChanged(player) saves the data as file and sends data to the client
PlayerSpawn



	--Prepare inventory
	GameGlobal:PrintMessage("Saving/Reloading player's inventory...")
	InventoryManager:SaveInventory(ply)
	InventoryManager:LoadInventory(ply)
	InventoryManager:OnInventoryChanged(ply)
	GameGlobal:PrintMessage("Done !")


InitialPlayerSpawn



	--Prepare inventory
	GameGlobal:PrintMessage("Loading player's inventory...")
	InventoryManager:LoadInventory(ply)
	InventoryManager:OnInventoryChanged(ply)
	GameGlobal:PrintMessage("Done !")


InventoryManager:OnInventoryChanged



function InventoryManager:OnInventoryChanged(player)
	if (player.inventory == nil) then return false end
	--Save inventory
	InventoryManager:SaveInventory(player)
	
	--Send the complete inventory for update on client side
	net.Start("RealisticRoleplay_InventoryUpdater")
		print("test1")
		net.WriteTable(player.inventory)
	net.Send(player)
end


Client side



net.Receive("RealisticRoleplay_InventoryUpdate", function(len, ply)
	GameGlobal:PrintMessage("Updating player inventory...")
	LocalPlayer().inventory = net.ReadTable()
end)


The server outputs correctly “test1” and every Load/Reload messages.
That’s only the client that won’t recieve anything from server…

did you add this in server side



util.AddNetworkString("RealisticRoleplay_InventoryUpdate")


It should’ve thrown an error if that was the case, telling him about an attempt to use an unpooled net message.

Yuri, did you get any errors similar to this?

I don’t see where player.inventory is ever set, aside from clientside…

Networking example: https://dl.dropboxusercontent.com/u/26074909/tutoring/networking/networking_booleans.lua.html

Second: The player entity may be NULL in PlayerInitialSpawn for a few seconds; try using console commands to debug and ensure it is, or is not, being sent.

I did not get any errors ! I know how to use net.Recieve. It’s working when i put it in a HUDPaint that is forcing the server to tell what is the player.inventory. But this is decreasing so much server and client performences. The connection between client and server becomes realy crazy (about 200 ping, when i’m on localhost), so i can’t use that way.

I’ve noticed what’s wrong, you spelt the name wrong on the clientside;


// What you called it on the server
net.Start("RealisticRoleplay_InventoryUpdater")

// What you called it on the client
net.Receive("RealisticRoleplay_InventoryUpdate")

You removed an ‘r’ on the client.

Of curse the player inventory is set : InventoryManager:LoadInventory(player) function in two times :

  • first, create a new table at player.inventory
  • second load a file and use table.insert to insert entities that player has in his data file.

normaly, the client should recieve the table from the server (the table can be emty but can not be null) and sets it to LocalPlayer().inventory ! So inventory_renderer.lua is able to render every entities contained in the table.

[editline]5th July 2014[/editline]

I just seen your post ! Am i stupid ?? It seams that i coded that too quickly, so i just made mistake !!!
Will try that and after i will let you know…

[editline]5th July 2014[/editline]

Thank you man ! It’s working, client code is now called. But i got a new problem, realy strange this time.

So on server player.inventory is empty (because i’ve no items in my inventory). When i retrieve data with net.ReadTable on client side, i get nil instead of an empty table, why ? I also tried to add 2 default items so the table is no longer empty on server, but same problem : client recieves a nil value from net.ReadTable

EDIT : After some research, when use LocalPlayer().inventory = net.ReadTable, it’s not working but when i try to print net.ReadTable and removing the LocalPlayer().inventory set code, it’s working it ouputs a table… So it’s realy strange

You could write it like this:


local var = net.ReadTable( ) || { };

which assigns either a valid read table from net.ReadTable( ) to var, or assigns an empty table to var ( || { } )

That’s ok, i already solved the data recieve problem. Thanks for your help everyone.

Now client and server are correctly exchanging inventory table. The new problem (and i think the last one) is that the entities that are created by ents.Create are not valid. They are valids only if you spawn them. Only one problem : these entities are spawned only when the client requests to. So when the server creates entities from the entity class, i get an unusable entity. I can just spawn it and set it position. But i don’t want that until player requests it. I need client to render the not spawned entity model in the DModelPanel.