• Table help
    7 replies, posted
First I know I spelled table wrong in the title, sorry. Hello. I'm trying to make a box to store things in. I got the basic functions working, I just need a way to remove the table after it have been used. My init.lua (Where the action happens) [lua] AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function ENT:Initialize() self.Entity:SetModel("models/props_junk/cardboard_box003a.mdl") self.Entity:PhysicsInit(SOLID_VPHYSICS) self.Entity:SetMoveType(MOVETYPE_VPHYSICS) self.Entity:SetSolid(SOLID_VPHYSICS) self.CanUse = true self.Entity.BoxFull = false local phys = self.Entity:GetPhysicsObject() if phys and phys:IsValid() then phys:Wake() end end function ENT:SpawnFunction( ply, tr ) if ( !tr.Hit ) then return end local ent = ents.Create("ent_box") ent:SetPos( tr.HitPos + tr.HitNormal * 16 ) ent:Spawn() ent:Activate() return ent end StoreBox = { } function AddItem( entity ) table.insert( StoreBox, { Entity = entity } ) end function ENT:Touch(ent) if ent:IsValid() and !ent:IsPlayer() then if self.Entity.BoxFull == false then AddItem( ent:GetClass() ) self.Entity.BoxFull = true ent:Remove() else return end end end function ENT:Use(activator, caller) if self.Entity.BoxFull == true then for k, v in pairs( StoreBox ) do local controller = ents.Create(v.Entity) controller:SetPos(self.Entity:GetPos() + Vector(0, 0, 3)) controller:Spawn() controller:Activate() table.remove( StoreBox, v.Entity ) end end self.Entity.BoxFull = false end function ENT:OnRemove() return false end [/lua] The problem is: table.remove( StoreBox, v.Entity ) The error I get: entities/ent_box/init.lua:53: bad argument #2 to 'remove' (number expected, got string) What I get from that and the wiki is that I need to use like table.remove( StoreBox, 1 ) to remove the first item that got added, but I want in a later version so it can hold more then one item and then I don't know how to remove it. So what I need help to is: How to make it so it can have lets say 10 items inside but it will only remove the ent that get spawned? (I'm going to make a derma menu so I can control what ent gets spawned) Thanks for you time/help :smile: Ps: Sorry if my English sucked :/
The 2nd argument requires a Integer (number) not a string and what you have given it is a string, Sorry if this seems uninformative but tahts just my 2 cents
I know that (Found it on wiki) but Is there a way to do what i asked for? (Make it store more then one item in a table and then remove it after it have been spawned)
Edited* [code]AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function ENT:Initialize() self.Entity:SetModel("models/props_junk/cardboard_box003a.mdl") self.Entity:PhysicsInit(SOLID_VPHYSICS) self.Entity:SetMoveType(MOVETYPE_VPHYSICS) self.Entity:SetSolid(SOLID_VPHYSICS) self.CanUse = true self.Entity.BoxFull = false local phys = self.Entity:GetPhysicsObject() if phys and phys:IsValid() then phys:Wake() end end function ENT:SpawnFunction( ply, tr ) if ( !tr.Hit ) then return end local ent = ents.Create("ent_box") ent:SetPos( tr.HitPos + tr.HitNormal * 16 ) ent:Spawn() ent:Activate() return ent end StoreBox = { } function AddItem( entity ) table.insert( StoreBox, { Entity = entity } ) end function ENT:Touch(ent) if ent:IsValid() and !ent:IsPlayer() then if self.Entity.BoxFull == false then AddItem( ent:GetClass() ) self.Entity.BoxFull = true ent:Remove() else return end end end function ENT:Use(activator, caller) if self.Entity.BoxFull == true then local numbers={} for k, v in pairs( StoreBox ) do local controller = ents.Create(v.Entity) controller:SetPos(self.Entity:GetPos() + Vector(0, 0, 3)) controller:Spawn() controller:Activate() table.insert(numbers,k) end for p,i in pairs(numbers)do table.remove( StoreBox, i ) end numbers={} end self.Entity.BoxFull = false end function ENT:OnRemove() return false end[/code]
[QUOTE=-TB-;19881729]Edited* [code]*CODE*[/code][/QUOTE] Thanks! will test it now! [editline]10:32PM[/editline] Doesn't seem to work, If I put more then one prop in it (It doesn't delete it)
[code] function ENT:Use(activator, caller) if self.Entity.BoxFull == true then for k, v in pairs( StoreBox ) do local controller = ents.Create(v.Entity) controller:SetPos(self.Entity:GetPos() + Vector(0, 0, 3)) controller:Spawn() controller:Activate() end table.Empty(StoreBox) end self.Entity.BoxFull = false end [/code] Try that. But if you don't want to totally empty your table and just keep some things in your table then this might work. [code] function ENT:Use(activator, caller) if self.Entity.BoxFull == true then local numbers={} for k, v in pairs( StoreBox ) do local controller = ents.Create(v.Entity) controller:SetPos(self.Entity:GetPos() + Vector(0, 0, 3)) controller:Spawn() controller:Activate() table.insert(numbers,k) end local subtractor=0 for p,i in pairs(numbers)do table.remove( StoreBox, (i-subtractor) ) subtractor=subtractor+1 end numbers={} end self.Entity.BoxFull = false end [/code]
Works :smile: Could you help me make a derma menu so you can pick what items you want to spawn, and what items you want to stay inside?
Yeah sure add me on steam.
Sorry, you need to Log In to post a reply to this thread.