Facepunch / garrysmod-requests

Feature requests for Garry's Mod
86 stars 24 forks source link

Shared NetworkVarNotify callbacks #324

Open Loonesbury opened 10 years ago

Loonesbury commented 10 years ago

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 commented 8 years ago

any update on this?

kojidev commented 8 years ago

C'mon 2 years have passed

60percentCaffeine commented 8 years ago

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

Kefta commented 8 years ago

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

meepen commented 8 years ago

These should work with nw2 vars.

robotboy655 commented 8 years ago

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

This is for NWvars2 and should work already.

WasabiThumb commented 6 years ago

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

mandrewpowers commented 6 years ago

Christ, still not fixed?

Kefta commented 6 years ago

Use NW2Vars

meepen commented 5 years ago

hello please fix

WasabiThumb commented 5 years ago

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 commented 5 years ago

Can we pretty please get this fixed? ♥️

robotboy655 commented 5 years ago

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 commented 5 years ago

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

robotboy655 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

meepen commented 5 years ago

I'm not talking about NW2 things in this request

thegrb93 commented 5 years ago

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

robotboy655 commented 5 years ago

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 commented 5 years ago

Dirty but nessesary

meepen commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

meepen commented 5 years ago

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 commented 5 years ago

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

meepen commented 5 years ago

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 commented 4 years ago

Is this in the current version of gmod now?

meepen commented 4 years ago

Yes

thegrb93 commented 4 years ago

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

thegrb93 commented 4 years ago

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

robotboy655 commented 4 years ago

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.

https://github.com/Facepunch/garrysmod-issues/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.

A1steaksa commented 1 month ago

I'm not 100% sure my issue is the same as the ones in this thread, but it's close enough that I don't want to create another issue for it.

When I spawn a new Entity on the Server, then use an "Entity" type ENT:NetworkVar on an old Entity to refer to that newly created Entity, the NetworkVarNotify callback is never called on the Client. I tried switching to sending the ENT:EntIndex instead which revealed that at the moment that the callback is triggered, the new Entity does not yet exist on the Client.

I'm going to be relying on timer.Simple( 0, MyCoolFunction ) which does not feel good at all