Entity variable seemigly being set across all instances of table.
1 replies, posted
Hello everyone. I've been creating a simulation room addon for a while now and now that I am creating my sim room builder, I have run into a few issue's that I did not see coming. I have created tables for each kind of object that can be spawned, which have their own size and position, angles and entities. The entity is what makes the objects function in the physical space, using PhysicsInitMultiConvex to create dynamic entities that can be changed to create an enviroment. However after spawning the entity and setting it up, when I set the entity variable to link the object to the entity, it seems as though all the objects entity variables are set to the same entity, meaning that when I want to remove all of the entities and objects, im left with only deleted one entity and all the objects. Here's some of my code (the relevant parts) so you can see what's going on.
SimRoom = {}
local SimObject = {}
SimRoom.Objects = {}
SimRoom.Objects.Wall = {}
SimRoom.Layouts = {}
SimRoom.ObjectCreation = {}
SimRoom.CurrentObjectSpawning = nil
SimRoom.SpawnedObjects = {}
SimRoom.RemovingObjects = false
function SimRoom:RemoveAllEntities()
self.RemovingObjects = true
for k,v in pairs(self.SpawnedObjects) do
if IsValid(v.Entity) then
v:Remove()
end
end
self.SpawnedObjects = {}
self.RemovingObjects = false
end
function SimRoom:CreateWall(layout, min, max, angle)
if not self.Layouts[layout] then return end
local lay = self.Layouts[layout]
local wall = self.Objects.Wall
local mesh = self.ObjectCreation:WorldToBoxMesh(min, max)
local meshTbl = {}
table.insert(meshTbl, mesh)
wall:SetMesh(meshTbl)
wall:SetPos(min)
wall:SetAngles(angle)
max2 = max - min
min2 = Vector(0,0,0)
wall:SetMinBox(min2)
wall:SetMaxBox(max2)
print("Created Wall at ".. tostring(min))
table.insert(lay.Objects, wall)
return wall
end
-- SimObject Funcs
AccessorFunc(SimObject, "mesh", "Mesh")
AccessorFunc(SimObject, "color", "Color")
AccessorFunc(SimObject, "position", "Pos")
AccessorFunc(SimObject, "angles", "Angles")
-- SimWall Funcs
SimRoom.Objects.Wall = SimObject
AccessorFunc(SimRoom.Objects.Wall, "minbox", "MinBox")
AccessorFunc(SimRoom.Objects.Wall, "maxbox", "MaxBox")
SimRoom.Objects.Wall.Type = SIM_WALL
function SimRoom.Objects.Wall:Spawn()
local ent = ents.Create("ent_simobject")
ent.Object = self
ent:SetPos(self:GetPos())
ent:SetAngles(self:GetAngles())
ent:Spawn()
ent:Activate()
self.Entity = ent
table.insert(SimRoom.SpawnedObjects, self)
end
function SimRoom.Objects.Wall:Remove()
self.Entity:Remove()
end
Here's a pastebin if the forum formmating makes it unreadable: [Lua] Annoying Entity Error
I am aware there may be some workarounds. However this should work! and I would like to keep my code as effecient, modifyable and easy to read as possible. Any help is greatly appreciated!
You're having this issue because all your objects are sharing the same SimObject table. Each object needs to have its own copy of the table.
Sorry, you need to Log In to post a reply to this thread.