• Getting a value from a net.Receive to use in other functions.
    8 replies, posted
[B]Updated problem, scroll down to the 4th post.[/B] Hi, I'm using the net library to send the entity that's spawned serverside to clientside code. From there a message should be drawn on the player's screen varying on the prop's real-world coordinates. The problem is after I declare spawnedprop the net.ReadEntity(), the code doesn't recognize it anywhere else. I get: [CODE] [ERROR] lua/autorun/spawnedpropcl.lua:12: attempt to index global 'spawnedprop' (a nil value) 1. v - lua/autorun/spawnedpropcl.lua:12 2. unknown - lua/includes/modules/hook.lua:84 [/CODE] Here is my code. [CODE] net.Receive( "propdata", function (len, ply) spawnedprop = net.ReadEntity() end ) hook.Add( "HUDPaint", "textonscreen", function() proppos = spawnedprop:GetPos(); screenpos = proppos:ToScreen() if ( screenpos.x and screenpos.y ) then surface.SetFont( "ChatFont" ) surface.SetTextColor( 255, 255, 255, 255 ) surface.SetTextPos( screenpos.x, screenpos.y ) surface.DrawText( "Prop Spawned" ) end end )[/CODE]
Because your hook is running before the entity is received. You should be making the entity local to the whole file and checking if the entity is valid in the hook.
[code]local spawnedprop = nil net.Receive( "propdata", function (len, ply) spawnedprop = net.ReadEntity() end ) hook.Add( "HUDPaint", "textonscreen", function() if not spawnedprop or not spawnedprop:IsValid() then return end local proppos = spawnedprop:GetPos() local screenpos = proppos:ToScreen() if screenpos.x and screenpos.y then surface.SetFont( "ChatFont" ) surface.SetTextColor( 255, 255, 255, 255 ) surface.SetTextPos( screenpos.x, screenpos.y ) surface.DrawText( "Prop Spawned" ) end end )[/code]
Thank you. [editline]29th November 2015[/editline] I'm having a newer problem, if anyone can help me. The text would only appear on the latest prop spawned, any props spawned before it would have their text removed. To solve this, I made each prop entity a value in a table, and used a loop to draw the text on each value in that table. It worked, and I was able to see text on all props, until I removed one. Then, text on all props vanished and lua spewed out an error about it using a null entity. I figured it was because it was trying to draw text for an entity that wasn't there after I removed it. I made an attempt to fix this by running a think hook that made each value in the table either the value it was assigned or a string "unfilled". Then In the loop to draw text on all entities in the table, I made it check if v was "unfilled". This still didn't seem to work, though, when I remove a prop I get issues.
[code]local props = {} net.Receive("propdata", function (len, ply) table.insert(props, 1, {prop = net.ReadEntity(), killtime = CurTime() + 5}) end) hook.Add("HUDPaint", "textonscreen", function() if #props == 0 then return end for k, v in pairs(props) do if not v.prop:IsValid() or CurTime() > v.killtime then table.remove(props, k) continue end local proppos = v.prop:GetPos() local screenpos = proppos:ToScreen() if screenpos.x and screenpos.y then surface.SetFont("ChatFont") surface.SetTextColor(255, 255, 255, 255) surface.SetTextPos(screenpos.x, screenpos.y) surface.DrawText("Prop Spawned") end end end)[/code] Dunno, written on mobile.
[QUOTE=iJohnny;49213724][code]local props = {} net.Receive("propdata", function (len, ply) table.insert(props, 1, {prop = net.ReadEntity(), killtime = CurTime() + 5}) end) hook.Add("HUDPaint", "textonscreen", function() if #props == 0 then return end for k, v in pairs(props) do if not v.prop:IsValid() or CurTime() > v.killtime then table.remove(props, k) continue end local proppos = v.prop:GetPos() local screenpos = proppos:ToScreen() if screenpos.x and screenpos.y then surface.SetFont("ChatFont") surface.SetTextColor(255, 255, 255, 255) surface.SetTextPos(screenpos.x, screenpos.y) surface.DrawText("Prop Spawned") end end end)[/code] Dunno, written on mobile.[/QUOTE] Thanks, I'm glad you introduced me to that table.remove function. Unfortunately, [DEL]there is no 'continue' in lua[/DEL], - ([B]Update[/B]: There apparently is in glua!) however, I just used return in it's place to stop that iteration of the loop. Again, thanks a bunch!
[QUOTE=Sheeplie;49217435]Thanks, I'm glad you introduced me to that table.remove function. Unfortunately, there is no 'continue' in lua, however, I just used return in it's place to stop that iteration of the loop. Again, thanks a bunch![/QUOTE] there's continue in glua, but not lua
-ninja'd-
That's awesome! Glad there's an easier solution, thanks.
Sorry, you need to Log In to post a reply to this thread.