Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared NetworkVarNotify callbacks #324

Open
Loonesbury opened this issue Aug 3, 2014 · 33 comments
Open

Shared NetworkVarNotify callbacks #324

Loonesbury opened this issue Aug 3, 2014 · 33 comments
Labels
Request The request seeks to add new functionality.

Comments

@Loonesbury
Copy link

Clientside callbacks added with NetworkVarNotify aren't called by the engine when the server changes the value, but as noted in includes/extensions/entity.lua it looks like they were meant to be:

-- Add a function that gets called when the variable changes
-- Note: this doesn't work on the client yet - which drastically reduces its usefulness.
@fruitwasp
Copy link

any update on this?

@kojidev
Copy link

kojidev commented Jul 17, 2016

C'mon 2 years have passed

@60percentCaffeine
Copy link

60percentCaffeine commented Jul 17, 2016

@kojidev You're wrong. 1 year 10 months and 18 days have passed.

@Kefta
Copy link

Kefta commented Jul 17, 2016

Github displays it as "Almost 2 years." No need to be pretentious over it.

@meepen
Copy link

meepen commented Sep 4, 2016

These should work with nw2 vars.

@robotboy655
Copy link
Contributor

http://wiki.garrysmod.com/page/Entity/SetNWVarProxy

This is for NWvars2 and should work already.

@WasabiThumb
Copy link

https://wiki.garrysmod.com/page/Entity/NetworkVarNotify should be updated then. Just seeing this as a regular forum user is all.

@mandrewpowers
Copy link

Christ, still not fixed?

@Kefta
Copy link

Kefta commented Feb 17, 2018

Use NW2Vars

@meepen
Copy link

meepen commented Dec 16, 2018

hello please fix

@WasabiThumb
Copy link

Thanks for the ping, @meepen. Is this an easy fix? It probabaly should be done. I forgot about the issues it caused me but im sure it will come around and bug me in the future. I do networking a lot.

@meepen
Copy link

meepen commented Jul 4, 2019

Can we pretty please get this fixed? ♥️

@robotboy655
Copy link
Contributor

Yeah I have the code for this ready but there's a big problem with this, and that is the callback is called too soon before the entity gets its Lua loaded on client, which you can understand is a problem.

@meepen
Copy link

meepen commented Aug 14, 2019

i'm sorry to keep bumping this, but is there any chance you resolved that issue?

@robotboy655
Copy link
Contributor

Nah, I keep finding issues with this, I have fixed a different issue for SWEPs already, but there's still similar problem where the callbacks would be randomly called for SENTs with wrong old value for no reason and the issue with callbacks being called to soon is really tricky to solve due to how networking is done in Source.

I am semi tempted to just push what I have, but I probably should fix these 2 problems it has.

@meepen
Copy link

meepen commented Aug 23, 2019

Completely understand, and it's just been a huge issue not having this by default, because NW2 still have issues when messing with dormancy that I haven't had time to replicate for a report yet and there's no other good options for callbacks.

It makes doing stuff properly (on-update) extremely hard.

Hope you can get them solved soon.

@meepen
Copy link

meepen commented Sep 23, 2019

I know it's looked down upon to bump issues, but this is VERY important for me. I hate doing stuff in Think to check if stuff has changed.
Is there anything I can do to help out?

@thegrb93
Copy link

Find reliable ways to reproduce the NW2 issues would be a good way to get them fixed faster

@meepen
Copy link

meepen commented Sep 23, 2019

I'm not talking about NW2 things in this request

@thegrb93
Copy link

I suggested it since NW2 has this functionality working already but needs fixing.

@robotboy655
Copy link
Contributor

I will put what I have onto Dev branch so you can test if there are any major issues with it, it should just work except for 2 things:

  1. When entering PVS of an entity with DTVars, the clientside float callbacks will be called from a seemingly random value to the correct one. I don't know why this is.
  2. The callbacks will not be run on entity spawn, and I am not sure this is fixable without making sort of queue and waiting for the entity Lua to load and firing callbacks then, but it feels like a very dirty hack.

@thegrb93
Copy link

Dirty but nessesary

@meepen
Copy link

meepen commented Sep 23, 2019

Is it only float callbacks? If so it shouldn't be too big of a deal.

The second one is completely fine and I can just do that in Lua if necessary

@robotboy655
Copy link
Contributor

It my tests it seems to be only floats that do the first thing. It's somewhere in the networking and I couldn't figure out what it is yet.

@meepen
Copy link

meepen commented Sep 23, 2019

I'll test this either tomorrow or tonight to provide useful feedback. Thank you so much.

@meepen
Copy link

meepen commented Sep 23, 2019

does the server have to be on dev? im running client dev and server main and i just get CallDTVarLuaProxy: No DTVar_ReceiveProxyGL printed whenever anything happens.

AddCSLuaFile()

ENT.Type = "point"
ENT.Base = "base_entity"
ENT.PrintName = "TTT Networking State"
ENT.Author = "Meepen"
ENT.Contact = "meepdarknessmeep@gmail.com"

function ENT:NetworkVarNotifyCallback(name, old, new)
	printf("%s::%s: %s -> %s", self:GetClass(), name, old, new)
	if (old == new) then
		return
	end
	hook.Run("On" .. name .. "Change", old, new)
end

function ENT:SetupDataTables()
	local types, vars = {}, hook.Run("InitializeNetworking") -- can just make this return {} and it should be fine

	self:NetworkVar("Int", 0, "Test")
	self:NetworkVarNotify("Test", self.NetworkVarNotifyCallback)
	for _, var in ipairs(vars) do
		-- blocked by issue https://github.com/Facepunch/garrysmod-requests/issues/324
		
		if (not types[var.Type]) then
			types[var.Type] = 1
		end
		
		printf("Registering variable %s (type %s)", var.Name, var.Type)
		self:NetworkVar(var.Type, types[var.Type], var.Name)
		self:NetworkVarNotify(var.Name, self.NetworkVarNotifyCallback)
		if (SERVER and var.Default) then
			self["Set" .. var.Name](self, var.Default)
		end

		types[var.Type] = types[var.Type] + 1
	end
end

function ENT:Initialize()
	self:SetPredictable(false)
end

function ENT:UpdateTransmitState()
	return TRANSMIT_ALWAYS
end

hook.Add("TTTAddPermanentEntities", "ttt_state", function(list)
	table.insert(list, "ttt_state")
end)
e = e or ents.Create "thatent"
e:SetTest(2)

image

@robotboy655
Copy link
Contributor

Yes, the callbacks are all defined in Lua so the c++ side has to call to a new Lua function.

@meepen
Copy link

meepen commented Sep 23, 2019

Looks to all be working perfectly, I don't have any things that hit floats hard that aren't always able to be seen so I will have to test that eventually. I'm really loving this!

@thegrb93
Copy link

thegrb93 commented Dec 7, 2019

Is this in the current version of gmod now?

@meepen
Copy link

meepen commented Dec 7, 2019

Yes

@thegrb93
Copy link

Setting the DTVar right after creating the entity doesn't trigger the clientside callback.

@thegrb93
Copy link

I found that invoking the callback in ENT:Initialize manually does the job

@robotboy655
Copy link
Contributor

robotboy655 commented Aug 31, 2020

Just to be clear and as a self note, this is still open because of the very first callbacks clientside (on entity spawn) are not being called due to a race condition in-engine.

Facepunch/garrysmod-issues#4639 is also affected by this.

There's also a nasty network related issue related to entity baselines that needs figuring out, which causes the callbacks clientside get called with seemingly garbage data for less than a frame and then get reset back to correct value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Request The request seeks to add new functionality.
Projects
None yet
Development

No branches or pull requests

11 participants