• Entity.NetworkVar
    7 replies, posted
Entity.NetworkVar isn't overwriting functions if they already exist, is this meant to happen?
There's no reason why it shouldn't - are you sure? [img]http://puu.sh/2nfhY[/img]
My code : [lua] ENT.Base = "base_entity"; ENT.Type = "anim"; ENT.Author = "Oubliette"; ENT.Spawnable = false; ENT.AdminSpawnable = false; function ENT:SetupDataTables() self:NetworkVar( "Int", 0, "Health" ); self:NetworkVar( "Int", 1, "MaxHealth" ); self:NetworkVar( "Entity", 0, "Owner" ); self:NetworkVar( "Float", 0, "TestName" ); end [/lua] Proof : [code] > PrintTable( debug.getinfo( player.GetAll()[1]:GetEyeTrace().Entity.GetOwner ) )... linedefined = -1 currentline = -1 func = function: 0x284bed60 isvararg = true namewhat = lastlinedefined = -1 source = =[C] nups = 0 what = C nparams = 0 short_src = [C] > PrintTable( debug.getinfo( player.GetAll()[1]:GetEyeTrace().Entity.GetTestName ) )... linedefined = 241 currentline = -1 func = function: 0x28fd1b38 isvararg = false namewhat = lastlinedefined = 243 source = @lua/includes/extensions/entity.lua nups = 1 what = Lua nparams = 1 short_src = lua/includes/extensions/entity.lua > PrintTable( debug.getinfo( player.GetAll()[1]:GetEyeTrace().Entity.GetHealth ) )... linedefined = 57 currentline = -1 func = function: 0x28493c60 isvararg = false namewhat = lastlinedefined = 59 source = @gamemodes/torrent/gamemode/core/sh_entity_meta.lua nups = 0 what = Lua nparams = 1 short_src = gamemodes/torrent/gamemode/core/sh_entity_meta.lua ] [/code]
I think this is because of how metatables work. Basically each entity has a table associated with it, which is unique to that entity, each entity also has a metatable, but the metatable is just one table used for ALL entities. When you index an entity (i.e. print(ent.GetWhatever)) it calls a function which does something like this (abbreviated a lot but you get the idea): [lua] function index( ent, key ) if metatable[ key ] then return metatable[ key ] end if ent:GetTable()[ key ] then return ent:GetTable()[ key ] end end [/lua] So you can see when you index for say, GetHealth, first it searches the main Entity metatable, it finds the C function GetHealth and returns that. When you index for say, GetTestName, it searches the main Entity metatable, [b]doesn't[/b] find any function, so it then searches the Entitys individual table, and finds the function NetworkVar created. In short: just don't bother overwriting stuff.
You're right. Personally I think userdata elements should have priority over parent metatables, it should only fix problems like this. I'd probably just change the __index metamethod, unless Sublime has a built in file Find & Replace function ..
That's not how it works at all. It searches in the table you are indexing first, and if it doesn't find anything, [i]then[/i] it calls (or searches) __index.
[code] > player.GetAll()[1].Health = function() return 50000 end... > print( player.GetAll()[1]:Health() )... 100 > debug.getregistry()[ "Player" ].Health = function() return 5000 end... > print( player.GetAll()[1]:Health() )... 5000 > player.GetAll()[1].Potatoe = function() return 5000 end... > print( player.GetAll()[1]:Potatoe() )... 5000 [/code] Apparently not.
[QUOTE=initrd.gz;40027325]That's not how it works at all. It searches in the table you are indexing first, and if it doesn't find anything, [i]then[/i] it calls (or searches) __index.[/QUOTE] This is how I've always thought it worked, in the next update Garry moved the Entity index function to Lua, you can see it here: [url]https://github.com/garrynewman/garrysmod/blob/master/garrysmod/lua/includes/extensions/entity.lua#L10[/url]
Sorry, you need to Log In to post a reply to this thread.