Sending Vector of an entity via net.WriteVector?

Hiya my dudes,

I have created a simple Derma menu that opens when an entity is used. Basically, I would like a weapon to spawn above the entity when I click a Derma button, but as you can see below, I get an error.


-- The Error

[ERROR] addons/gunorder/lua/entities/orderbox/init.lua:24: Tried to use a NULL entity!
  1. Send - [C]:-1
   2. unknown - addons/gunorder/lua/entities/orderbox/init.lua:24

I can get the weapon to spawn on the player by using ply:GetPos() instead of the vector of the entity but cannot seem to pass the entity vector position over to the server for the wep:SetPos()

I have tried sending the vector split up into float values before I noticed the error. As I am very new to GLua, I can’t seem to figure out how in gods name I am supposed to fix this NULL entity issue.
Am I supposed to do net.WriteEntity() or something instead? Or will that just write the entity classname much like my cl_init.lua file does using the net.WriteString(“weapon_357”)?

I added in an entity validity check in there as well as a print to the server console for dubugging


--Clientsided stuff, located in the entity

-- init.lua
	--This is all when the entity is used
	
function ENT:Use(ply, caller)
	if (IsValid(self)) then
		print("YES I AM A VALID ENTITY")			--Just to check the if statement is working
		net.Start("SwepPos")
			net.WriteVector(self:GetPos())
		net.Send()
		
		umsg.Start( "OrderMenu", ply )
			umsg.Short( "1" )
		umsg.End()
	end
end


--cl_init.lua
	--This is when the button is clicked in the Derma Menu
	
	Button.DoClick = function()
		net.Start("SwepSpawn")				--Start spawn message
			net.WriteString("weapon_357")	--Write entity classname to server
		net.SendToServer()					--Send classname to server
	end


-----------------------------------------------------------------------

--All Serverside, located in servers autorun.

--Adding Network Strings for net.Reveive("string", function())
util.AddNetworkString("SwepSpawn")

net.Receive("SwepSpawn", function(len,ply)
	wep= ents.Create(net.ReadString())										--Get the classname written earlier
	local SpawnPos = net.ReadVector()
	wep:SetPos(Vector(SpawnPos.x, SpawnPos.y, SpawnPos.z + 20)				--Spawn at position of entity that was used, add 20 onto Z axis to spawn above entity
	wep:Spawn()																--Spawn the weapon
end)


A second question also,

I understand that I must read things in the same order I write them when I send them to the server. Does this rule apply to the individual types of data? As in, if I write a string, a vector and a string, will it read in that order or will the ReadString skip the vector and read the next string in the written order?

Like I said, I am very new to this stuff, so any help would be GREATLY appreciated. Thanks guys :smiley:

You didn’t specify what player to send the net message to. Read every error you get very carefully. It says it right there that it errored at net.Send and on line 24.

You read and write data in the same order. Period. There is nothing special going on. Same order.

You have a syntax issue with your parenthesis in SwepSpawn, and you’re not inputting an entity for net.Send to send to; try this:


--- Clientside
-- cl_init.lua
Button.DoClick = function()
	net.Start("SwepSpawn")
		net.WriteString("weapon_357") -- It might just be for testing, but try to avoid sending hardcoded strings into net.Write*. Cache them by index, instead
	net.SendToServer()
end

--- Serverside
-- init.lua
function ENT:Use(ply, caller)
	net.Start("SwepPos")
		net.WriteVector(self:GetPos())
	net.Send(ply)
	
	-- Use a netmessage or convar for OrderMenu, don't use a umsg
end

-- Some file
util.AddNetworkString("SwepSpawn")

net.Receive("SwepSpawn", function(len,ply)
	local wep = ents.Create(net.ReadString())
	
	if (wep ~= NULL) then
		local SpawnPos = net.ReadVector()
		SpawnPos.z = SpawnPos.z + 20
		wep:SetPos(SpawnPos)
		wep:Spawn()
	end
end)

And yes, you have to read them in type order. The type is not networked.

Now that makes a heap of sense… Thank you so much for answering that for me! You actually just eliminated my frustration. Damn I’m a bloody idiot :stuck_out_tongue:

I suppose I just have to work on the spawning location now. Thanks a heap again, seriously helped a heap.