How to use Shared.lua with non-entity?

So I only know how to use networked vars when they’re applied to an entity, like in DarkRP, but can’t get it to work on a global level.

For example, I’m making a day/night cycle script. When the server has reached a certain time it will notify the networked bool var that “IsNight” is true or false. A timer on the client side is constantly checking this shared var and will then redownload the light map when it is true.

The problem I’m having is that I can’t get it to work on a non-entity scope.

So in my shared.lua I have:



/*---------------------------------------------------------------------------
LIGHT MOD SHARED
---------------------------------------------------------------------------*/

function SetupDataTables()
	self:NetworkVar("Bool", 0, "IsNight")
end


GMOD doesn’t like this and complains “self” is a nil value. How is it done outside of an object’s scope?

I’ve also tried it without the self reference to no avail.

You can simply store a value serverside, then network it to the client using the net library?

Do you even need serverside? Are you making a day/night addon? You probably don’t even need to have a server side.

If I don’t make it server side won’t all clients have their own unsynched day/night cycle? It’s based on a timer so I thought the server would be best to tell all clients that every x minutes it turns day/night.

I’ve not used the net library, I expected a shared var would be the easiest way.

I’ve narrowed it down to just the client script errors now. Here’s my cl_myLightMod.lua



AddCSLuaFile("autorun/shared.lua")
include("autorun/shared.lua") -- Forgive these two, I added both to make sure it was included, and it is

local oldIsNight

function GameInitialize()
	timer.Create("lightTimer", 1, 0, function() Refresh() end)
end

hook.Add("Initialize", "gameInitClient", GameInitialize)

function Refresh()
	-- Check networked var to see if this is necessary
	if GetIsNight() != oldIsNight then
		oldIsNight = GetIsNight()
		render.RedownloadAllLightmaps()
	end
end

It complains that the getter GetIsNight() isn’t found anywhere, despite the included shared.lua

Bah the scope in this language is strange!

Hmm, well I would put the timer serverside then, and just network if it’s day or night.



-- Server
util.AddNetworkString("UpdateClientDaynNight")
local timePerDay = 10 * 60 -- 10 minutes
local isNight = false
timer.Create("DaynNight", timerPerDay, 0, function()
	isNight = !isNight
	net.Start("UpdateClientDaynNight")
		net.WriteBit( isNight )
	net.Broadcast()
end)
local function spawn( ply )
	net.Start("UpdateClientDaynNight")
		net.WriteBit( isNight )
	net.Send( ply )
end
hook.Add( "PlayerInitialSpawn", "GiveTime", spawn )

-- Client
local isNight = false
net.Receive("UpdateClientDaynNight", function()
	isNight = tobool(net.ReadBit())
	if isNight then
		-- Night shit
	else
		-- Day shit
	end
end)


This doesn’t seem like the trickiest thing to do.

Use something like this:


-- All this Server-Side
util.AddNetworkString( "NightNetworkString" )

function ToNight()  -- Call this to switch to night
	net.Start("NightNetworkString")
		net.WriteBit(true)
	net.Broadcast()
end

function ToDay()  -- Call this to switch to day
	net.Start("NightNetworkString")
		net.WriteBit(false)
	net.Broadcast()
end

-- All this Client-Side

net.Receive( "NightNetworkString", function( len )
	local night = util.tobool(net.ReadBit())
	if night then
		MakeNight()  -- Make Night
	else
		MakeDay()  -- Make Day
	end
end)

Cheers mate, that’s nice of you to bash that out for me. Should that code be in a single file or should I split it? If so, what should it be named?

Out of interest here’s my current server side script that includes spawning zombies when it’s dark:


AddCSLuaFile("autorun/client/cl_mylightmod.lua")
AddCSLuaFile("autorun/shared.lua")
include("autorun/shared.lua")

local isNight = false
local nightMins = 0.3
local dayMins = 0.3

local npcSpawns = {
	Vector(-719.68, 892, 70),
	Vector(-1968, 922, 70),
	Vector(-2112, -1242, 70),
	Vector(190, -1490, 75),
	Vector(879, -1138, 75),
	Vector(285, -1652, 270),
	Vector(-268, 330, 75),
	Vector(389, 920, -50),
	Vector(1422, 1005, 75),
	Vector(1360, 2305, 75),
	Vector(1383, 3871, 75),
	Vector(-1316, 3556, 75),
	Vector(985, 4212, 75),
	Vector(1540, 3533, 75)
}

local liveNpcs = {}

function GameInitialize()
	print("- - - - - - - - - - LIGHT MOD LOADED - - - - - - - - - - -")
	timer.Create("nightTimer", nightMins*60, 1, function() ToggleNight() end)
end

hook.Add("Initialize", "gameInitServer", GameInitialize)

function SpawnZombies()
	for key,value in pairs(npcSpawns) do
		npc = ents.Create("npc_fastzombie")
		npc:SetPos(value)
		npc:Spawn()
		npc.LoseTargetDist = 2000
		npc.SearchRadius = 2000
	
		table.insert(liveNpcs, npc)
	end
end

function RemoveZombies()
	for key,value in pairs(liveNpcs) do
		if value:IsValid() then
			value:Remove()
		end
	end
end

function ToggleNight()
	if isNight == false then
		PrintMessage(HUD_PRINTCENTER, "Night has descended.. beware of the streets!")
		engine.LightStyle(0, "a")
		isNight = true
		SetIsNight(true)
		SpawnZombies()
		timer.Create("dayTimer", dayMins*60, 1, function() ToggleDay() end)
	end
end

function ToggleDay()
	if isNight == true then
		PrintMessage(HUD_PRINTCENTER, "Daylight has broken..")
		engine.LightStyle(0, "v")
		isNight = false
		SetIsNight(false)
		RemoveZombies()
		timer.Create("nightTimer", nightMins*60, 1, function() ToggleNight() end)
	end
end

Here, quickly made it, untested



-- Server
util.AddNetworkString("UpdateClientDaynNight")
local timePerDay = 10 -- 10 minutes
local isNight = false
local liveNpcs = {}
local npcSpawns = {
	Vector(-719.68, 892, 70),
	Vector(-1968, 922, 70),
	Vector(-2112, -1242, 70),
	Vector(190, -1490, 75),
	Vector(879, -1138, 75),
	Vector(285, -1652, 270),
	Vector(-268, 330, 75),
	Vector(389, 920, -50),
	Vector(1422, 1005, 75),
	Vector(1360, 2305, 75),
	Vector(1383, 3871, 75),
	Vector(-1316, 3556, 75),
	Vector(985, 4212, 75),
	Vector(1540, 3533, 75)
}
timer.Create("DaynNight", timerPerDay * 60, 0, function()
	isNight = !isNight
	net.Start("UpdateClientDaynNight")
		net.WriteBit( isNight )
	net.Broadcast()
	SpawnZombies( isNight )
end)

local function SpawnZombies( night )
	if night then
		PrintMessage(HUD_PRINTCENTER, "Night has descended.. beware of the streets!")
		engine.LightStyle(0, "a")
		for key,value in pairs(npcSpawns) do
			npc = ents.Create("npc_fastzombie")
			npc:SetPos(value)
			npc:Spawn()
			npc.LoseTargetDist = 2000
			npc.SearchRadius = 2000
			table.insert(liveNpcs, npc)
		end
	else
		PrintMessage(HUD_PRINTCENTER, "Daylight has broken..")
		engine.LightStyle(0, "v")
		for key,value in pairs(liveNpcs) do
			if value:IsValid() then
				value:Remove()
			end
		end
	end
end

local function spawn( ply )
	net.Start("UpdateClientDaynNight")
		net.WriteBit( isNight )
	net.Send( ply )
end
hook.Add( "PlayerInitialSpawn", "GiveTime", spawn )

-- Client
local isNight = false
net.Receive("UpdateClientDaynNight", function()
	isNight = tobool(net.ReadBit())  -- Is there is anything you wanna do here besides redownload lightmaps, does the client need this bool?
	if isNight then
        
	else

	end
        render.RedownloadAllLightmaps()
end)


Thanks so much for that. I’ve implemented and it works, there’s just one little problem - when it’s supposed to be night, it’s daytime and when it’s supposed to be day it’s night time. I’ve messed about the with booleans but it doesn’t help. Any ideas?



-- Client

net.Receive("UpdateClientDaynNight", function()
	render.RedownloadAllLightmaps()
end)



-- Server

AddCSLuaFile("autorun/client/cl_mylightmod.lua")

util.AddNetworkString("UpdateClientDaynNight")
local timePerDay = 0.1 -- 10 minutes
local isNight = false

local liveNpcs = {}

local npcSpawns = {
	Vector(-719.68, 892, 70),
	Vector(-1968, 922, 70),
	Vector(-2112, -1242, 70),
	Vector(190, -1490, 75),
	Vector(879, -1138, 75),
	Vector(285, -1652, 270),
	Vector(-268, 330, 75),
	Vector(389, 920, -50),
	Vector(1422, 1005, 75),
	Vector(1360, 2305, 75),
	Vector(1383, 3871, 75),
	Vector(-1316, 3556, 75),
	Vector(985, 4212, 75),
	Vector(1540, 3533, 75)
}

local function SpawnZombies( night )
	if night then
		PrintMessage(HUD_PRINTCENTER, "Night has descended.. beware of the streets!")
		engine.LightStyle(0, "a")
		for key,value in pairs(npcSpawns) do
			npc = ents.Create("npc_fastzombie")
			npc:SetPos(value)
			npc:Spawn()
			npc.LoseTargetDist = 2000
			npc.SearchRadius = 2000
			table.insert(liveNpcs, npc)
		end
	else
		PrintMessage(HUD_PRINTCENTER, "Daylight has broken..")
		engine.LightStyle(0, "v")
		for key,value in pairs(liveNpcs) do
			if value:IsValid() then
				value:Remove()
			end
		end
	end
end

local function spawn( ply )
	net.Start("UpdateClientDaynNight")
		net.WriteBit( isNight )
	net.Send( ply )
end

timer.Create("DaynNight", timePerDay * 60, 0, function()
	isNight = !isNight
	net.Start("UpdateClientDaynNight")
		net.WriteBit( isNight )
	net.Broadcast()
	SpawnZombies( isNight )
end)

hook.Add( "PlayerInitialSpawn", "GiveTime", spawn )

The engine light level is mixed, swap them around.

Of course! Thanks