Entity Var cleared after clearing internal var *after* setvar
4 replies, posted
Hey so, I have a table, and when a player says something and a couple of if statements go off true, then I add the player to the table and set the table into Entity:SetVar, ( you can ask why I do it this way, but I am not very experienced and can't see a better way to do it and don't think datatables can hold a table, and the entity doesnt have a shared as the entity is spawned in hammer but coded in lua ) then AFTER I setvar I clear the table, but as soon as I clear the table the entity's var gets cleared too.
This piece of code is in 2 if statements and a playersay hook, so I dont know why the var would be "constantly" setting itself, well it seems to be doing so, I would think it would set the variable into the entity table and thats that, I can do what I want with the variable in the code and not have the entity var be affected, any ideas? All replies are appreciated. I can post "full" (most) code if needed, also I found this was the best way to get the entity according to a key inside the entity because of a bunch of things so yeah...
table.insert(ClaimedPlayersTble, ply:EntIndex())
table.KeyFromValue(ReservableSpotsEnts, string.sub(text, 8)):SetVar("ClaimedPlayers", ClaimedPlayersTble)
table.Empty(ClaimedPlayersTble)
Tables are passed around by reference, they aren't copied. If you could explain what you're trying to do a bit better there's probably a much better solution to what you're doing.
So I have an entity created, and spawned in hammer, I am trying to make that space that the entity is taking reservable by the player, I figured hooks would be good because you can add and remove stuff from them via code, I don't know how to do that via variables and I don't know if its possible, this is server side autorun code, the entity code shouldn't really be necessary, everything works up until the table clear as I need to clear it to allow someone else to claim another spot or "entity"
local ReservableSpots = {} -- Create a table for the RID keys
local ReservableSpotsEnts = {}
local ClaimedPlayersTble = {}
hook.Add( "EntityKeyValue", "reservableroomfind", function(ent, key, value)
-- Find and apply the RID keys and the ent to a table for future ref
if(ent:GetClass() == "reservableroom" && key == "RID") then
table.insert( ReservableSpots, value, value)
ReservableSpotsEnts[ent] = value
print(table.ToString(ReservableSpotsEnts, "ReservableSpotsEnts", true))
end
end)
hook.Add( "PlayerSay", "claimreservableroom", function( ply, text, team)
print(ply:GetName())
print("print")
print(ply:EntIndex())
if ( string.sub( string.lower( text ), 1, 6 ) == "!claim" ) then
if (table.HasValue( ReservableSpots ,string.sub( text, 8 ) ) ) then
table.insert(ClaimedPlayersTble, ply:EntIndex())
table.KeyFromValue(ReservableSpotsEnts, string.sub(text, 8)):SetVar("ClaimedPlayers", ClaimedPlayersTble)
table.Empty(ClaimedPlayersTble)
end
end
end)
I love the broken formatting, lol sorry, I tried fixing it, heres a picture of the code if you want to look at that instead rofl.
https://i.imgur.com/vRjIXek.png
I don't know if there's a better way of doing this but, a way of fixing this would be to just create a local table (or fetch the existing one from the entity) inside of the hook when you're going to insert the player, because I don't see you using the ClaimedPlayersTble table anywhere else in your code, making it a bit pointless to make it a root local.
I have the key as the entity because there is a table key from value, and those prints are going to stay in the final code, but I guess I could use those, and yeah that would be easier, sorry I'm not that good at GLua, so I wouldn't know but thanks for the help and tips, and a better way to do that.
Sorry, you need to Log In to post a reply to this thread.