

// Do we even need this anymore? 17:14 14/06/2006
//   It's useful for sending strings easily 0:13 29/9/2006

local gmod = gmod
local type = type
local string = string
local tostring = tostring
local pairs = pairs
local SERVER = SERVER
local CLIENT = CLIENT
local Msg = Msg

module("shared")

local shared_vars = {}

if (SERVER) then

	local function SendSingleValue(n,p)
	
		local qn = string.format("%q", n)
		local qv = tostring(shared_vars[n])
		if (type(shared_vars[n]) == "string") then
			qv = string.format("%q", shared_vars[n])
		end

		if (p) then
			p:SendLua( "shared.RecieveVar(" .. qn .. "," .. qv .. ")" )
		else
			gmod.BroadcastLua( "shared.RecieveVar(" .. qn .. "," .. qv .. ")" )
		end		
		
	end

	function SendVarsToClient( p )

		for k,v in pairs(shared_vars) do
			
			SendSingleValue(k,p)

		end	
	end

	function SetVar(name, value)
		if (type(name) ~= "string") then
			return
		end
		
		if (type(value) == "nil" or type(value) == "number" or type(value) == "string") then
			
			shared_vars[name] = value
			
			SendSingleValue(name)

		end
	end
	
	
end

if (CLIENT) then

	function RecieveVar(name, value)
	
		shared_vars[name] = value
		
	end

end

function GetVar(name)
	if (type(name) ~= "string") then
		return nil
	end

	return shared_vars[name]
end

