When I send an empty table({}) through net.WriteTable, it appears to be not be empty.
Here is my code:
SERVER:
[code]
net.Start("test_update")
net.WriteUInt(1, 16);
net.WriteTable({});
net.Send(self);
[/code]
CLIENT:
[code]
net.Receive("test_update", function()
local num = net.ReadUInt(16);
local tbl = net.ReadTable();
if (tbl == {}) then
print("TBL IS EMPTY")
end
LocalPlayer().MyTable[num] = tbl;
end)
[/code]
And the MyTable is just setup like LocalPlayer().MyTable = {};
Any ideas why it isn't recognizing that the table doesn't have any entries?
Thanks.
[B]EDIT:[/B]
Ok, I ran a test like so:
[code]
concommand.Add("test_emptytbl", function()
local tbl = {};
tbl[1] = {};
if (tbl[1] == {}) then
print("ok?")
else
print("no")
end
end)
[/code]
It printed out "no".
is there anyway to see if a table is created, but it isn't occupied by any variables?
Thanks.
Tables aren't compared by what they hold, they're compared by reference. tbl == {} will never be true. If your table is going to be like an array (using sequential keys starting from 1), then you can check if it's empty like this.
[lua]if (#tbl == 0) then
print("TBL IS EMPTY")
end[/lua]
Alternatively, for a more general approach
[lua]if (!next(tbl)) then
print("TBL IS EMPTY")
end[/lua]
If you don't understand anything I just said, just use the second option.
[QUOTE=sannys;51046987]If you don't understand anything I just said, just use the second option.[/QUOTE]
No, i completely understand. Thank you very much.
Sorry, you need to Log In to post a reply to this thread.