I've made over 5000 entities in my life and all worked fine while networking them, but now there is one that is trying to mess with me.
So I send data from servers side to client and apply it to entity, then I print it on both ends and I get this as an output:
[IMG]http://puu.sh/cTfBK/1abb6b9835.png[/IMG]
So as you can see, it works properly... but when I am trying to print the table inside ENT:Draw(), it returns nil.
I am clueless at this point.
sv_core.lua -- Entity creation and item setting.
[code]function coffeeInventory.dropStack( ply, itemPos )
local x, y = itemPos[1], itemPos[2]
if ply.coffeeInventory[ x ][ y ] == false then
return
end
local pack = ents.Create( "coffeeinventory_package" )
pack:SetPos( LocalToWorld( Vector( 50, 0, 35 ), Angle( 0, 0, 0 ), ply:GetPos(), ply:GetAngles() ) )
pack:Spawn()
pack.item = ply.coffeeInventory[ x ][ y ]
pack:sendData()
ply.coffeeInventory[ x ][ y ] = false
coffeeInventory.sendPlayerData( ply )
coffeeInventory.database.savePlayerData( ply )
end[/code]
init.lua -- Entity and sending data
[code]AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
function ENT:Initialize()
self:SetModel( "models/props_junk/cardboard_box003a.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:SetUseType( SIMPLE_USE )
self:GetPhysicsObject():Wake()
self.item = {}
end
function ENT:Use( activator, caller )
local i = ents.Create( self.item.class )
i:SetPos( self:GetPos() + Vector( 0, 0, 35 ) )
i:SetModel( self.item.mdl )
i:Spawn()
for var, value in pairs ( self.item.data ) do
i[ var ] = value
i.dt[ var ] = value
end
self.item.amount = self.item.amount - 1
if self.item.amount <= 0 then
self:Remove()
end
end
util.AddNetworkString( "coffeeInv_itemPackageData" )
function ENT:sendData()
net.Start( "coffeeInv_itemPackageData" )
net.WriteEntity( self )
net.WriteTable( self.item )
net.Broadcast()
end[/code]
cl_init.lua -- Entity data receiving
[code]include("shared.lua")
net.Receive( "coffeeInv_itemPackageData", function()
local ent = net.ReadEntity()
ent.item = net.ReadTable()
end )
function ENT:Draw()
print( self.item )
self:DrawModel()
if self:GetPos():Distance( LocalPlayer():GetPos() ) < 250 then
cam.Start3D2D( self:GetPos() + Vector( 0, 0, 15 ), Angle( 0, LocalPlayer():EyeAngles().yaw - 90, 90 ), 0.02 )
surface.SetDrawColor( Color( 235, 189, 99, 50 ) )
surface.DrawRect( -400, 0 + math.sin( CurTime() ) * 50, 800, 100 )
draw.SimpleText( "Item Pack ", "coffeeinventory_medium3d", 0, 50 + math.sin( CurTime() ) * 50, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
cam.End3D2D()
end
end[/code]
does it always error? networking is asynchronous so it's probably calling draw before it has a chance to set self.item
[QUOTE=PortalGod;46503745]does it always error? networking is asynchronous so it's probably calling draw before it has a chance to set self.item[/QUOTE]
It doesn't error at all, it just prints nil.
I've kept printing for like 10 seconds and it didn't change.
I have the same problem with GMS, I have no idea why it does that, but here's the workaround I use:
[code]
local PendingDrops = PendingDrops or {}
net.Receive( "gms_container", function()
local t = net.ReadTable()
local ent = t.Container
PendingDrops[ t.EntIndex ] = t
/*if ( !IsValid( ent ) ) then
print( "This should not be happening!", ent )
//PendingDrops[ t.EntIndex ] = t
else
//ent:Update( t )
end*/
end )
function ENT:Think()
if ( PendingDrops[ self:EntIndex() ] ) then
//print("updated", self)
self:Update( PendingDrops[ self:EntIndex() ] )
PendingDrops[ self:EntIndex() ] = nil
end
end[/code]
Sorry, you need to Log In to post a reply to this thread.