Send an array to client


I need to send an array of data which can be very long, from my server init.lua to my cl_init.lua.
Should I use net.Send()? Split it up?

net.WriteTable, but it has limit of 64KB. What’s the data you are trying to send?

I will send an array like { “weapon_crowbar” } and it can be 100 of those.

Why don’t you just make the table shared if you’re going to be using it on the server and on the client?

It’s not net.WriteTable that has a 64KB data limit, it’s net packets as a whole.

I’m quite new to LUA; how would I do that?

You do that by creating a file that is ran on the server and on the client while being sent to the player. Just like a shared.lua file within a gamemode.

[editline]11th September 2013[/editline]

What is contained in your table?

So I put my file in lua/autorun/?
I want to send an array containing { “weapon_crowbar”, “weapon_glock” }, it can contain alot more than that.

Post your table content ( Using pastebin or

 tags ) So we can estimate what needs sending and what not.

By "making the table shared" is meant that you should put the table into your shared.lua of your gamemode. The shared.lua then must be included from init.lua and cl_init.lua as well as have AddCSLuaFile() on its first line, but I assume you already have it anyway.

@EvacX, I know, the context of my post wasn't the best though, my bad.

I don’t have anything in it currently, its for an inventory.
When the player joins, his stuff should be loaded from the database, put in the array, sent to client for derma menu, save it in client side and let player use items or add items to the inventory, and on player disconnect, save the array to the database.

Okay, then you should use net library, but keep in mind the 64KB limit of one message.

Also, you shouldn’t send ALL of the data to the client, some base values ( Name, model and stuff like that ) can be loaded from client ( Assuming your items are stored in Lua files ) so you then only send the item ids and some custom values.

You don’t need to send any of the table. Make the table shared and just send the index in the table that corresponds to that item.

I only send the entity name.

Can you show me what you mean?

He just said it’s an inventory, how is he going to share it?
Just use net.WriteTable, this is one of the legitimate use cases.

Considering you won’t hit the limit of a net message with just entity class names, you can safely use net.WriteTable.

Thank you, but what happens if it reaches the 64kb limit?

If you are sending more than 64kb of inventory data you are doing something dramatically wrong.

For reference, this is 64KB of text. 64K is small in terms of a mp3 but it’s still a shitload of data.

You can compress it using common text-based compression methods.

I have solved it now, I just have one last issue.
Unless my table has two items in it (clearly is two when I do PrintTable( table )), this is false:

if Weapons && type( Weapons ) == "table" && table.Count( Weapons ) > 0 then

If Weapons table contains only 1 item, this is false.
If I add one more item to it, this is true.

Is that some common issue or am I doing something bad?

[editline]12th September 2013[/editline]

More code:

net.Receive( "Weapons", 
function ( len )
Weapons = net.ReadTable();
Received Weapons send

if Weapons && type( Weapons ) == "table" && table.Count( Weapons ) > 0 then
	WeaponsCount = table.Count ( Weapons );

	WeaponsCount = 0;

I add an item to the Weapons table, I do get this print, only this:
Received Weapons send

I add another item to the Weapons table, I get this:
Received Weapons send
1 = weapon_mad_aug
2 = weapon_mad_usp_match

[editline]12th September 2013[/editline]

Oh my mistake, I only ran the table.insert function if the table.Count > 0.