• Lua - Localise Table/Variable
    2 replies, posted
hi im building some entities and they use a table local table = {} table[1] data ect the issue is when a there is more than 1 of that entity they all affect the same table rather than it being independent to that entity, i was wondering how to do that as i can't find much on this topic its basically local table = {} table[1] = { entity = Vector(#entity)} table[2] = { entity = Vector(#entity)) function AddEntity() -- for i ect add entity if not already there and table is not full end function removeEntitys() --for i ect table count remove entity end remove entities removes them from all entities using the table, same copies of the entity or other entities that use it
Sounds like your table is potentially being declared at a global scope. Can you try declaring the table as a member of the entity objects? i.e. ENT.table = { } Ent.table[1] = "blah" Fill disclosure, I know Lua well but its been about a decade since I wrote code for Gmod...
I would avoid naming a variable as global-scope library name as you later access the new table in the local scope instead of the table library. Try avoiding that practice of just allocate a table inside inside your entity like I'm doing in my spinner SENT. Instead of: local table = {} Just do: local myKey = "my_fancy_custom_table_resides_here" function ENT:Initialize()   self[myKey] = {Size = 0} end local function AddEntity(eSelf,oEnt)   local iTop = eSelf[myKey].Size; iTop = iTop+1   eSelf[myKey][iTop] = { entity = Vector(#entity) }   eSelf[myKey].Size = iTop end
Sorry, you need to Log In to post a reply to this thread.