• Trying to give and ID to an entity
    3 replies, posted
Hello guys, I'm trying to make a little script and i need to give an ID to an entity. I want the first entity toh ave the ID 1, the second entity the ID 2, so on and so forth. So i use a variable called VentLastId that I set to 0 when the entity initialize in init.lua function ENT:Initialize()     self:SetModel( "models/props_junk/vent001.mdl" )     self:PhysicsInit( SOLID_VPHYSICS )     self:SetMoveType( MOVETYPE_VPHYSICS   )     self:SetSolid( SOLID_VPHYSICS )     self:SetUseType(SIMPLE_USE)     VentLastId = 0     local phys = self:GetPhysicsObject()     if (phys:IsValid()) then         phys:Wake()     end end Then when the entity spawn I set his ID to VentLastID + 1 and set VentLastID to VentLastId + 1. Finally i used the ENT:Touch hook to print the entity's ID. But the only output I get is 1 even if I spawn a bunch of them. as you can see on the screenshot bellow https://files.facepunch.com/forum/upload/317004/4c31f8b7-e562-4fda-8830-bd3703c091a9/screenshot.png So here is my shared.lua ENT.Type = "anim" ENT.Base = "base_gmodentity" ENT.PrintName= "Vent" ENT.Spawnable = true ENT.AdminOnly = true ENT.Author  = "The French Spy" ENT.linked = 0 function ENT:SpawnFunction( ply, tr, ClassName )     if ( !tr.Hit ) then return end     local ent = ents.Create( ClassName )     ent:SetPos( tr.HitPos + tr.HitNormal * 16 )     ent:Spawn()     ent.id = VentLastId + 1     VentLastId = VentLastId + 1     return ent end function ENT:Touch( entity )     if self.linked == 0 then         print( entity.id )     end end If someone know what is wrong it would be very helpfull
You reset VentLastId to 0 because ENT.Initialize get's called too. Use this instead: VentLastId = VentLastId or 0
Thanks for your solution but I had to use an other method wich finally worked but i'm facing another problem with the variable VentLastId. So, when I put two entities each of them have their ID set, for the first one ID 1 and for the second ID 2, but when I start spawning more of them, like 3 or 4 they also get ID 1 and 2. Here is my new shared.lua ENT.Type = "anim" ENT.Base = "base_gmodentity" ENT.PrintName= "Vent" ENT.Spawnable = true ENT.AdminOnly = true ENT.Author  = "The French Spy" function ENT:Use( activator, caller )     if self.set == nil then         VentLastId = VentLastId + 1         self.id = VentLastId         self.set = 1         caller:ChatPrint( "Vent set to ID " ..self.id )     end     if IsValid( activator ) and activator:IsPlayer() then         if self.linked != nil then             caller:ChatPrint( "Linked to vent " ..self.LinkedVent )         end     end end function ENT:Touch( entity )     if self.linked == nil then         if entity.linked == nil then             if entity.id != nil then                 self.LinkedVent = entity.id                 if entity.LinkedVent != nil then                     entity.LinkedVent = self.id                     self.linked = 1                     if entity.linked != 1 then                         entity.linked = 1                     end                 end             end         end     end end And here is my init.lua AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') function ENT:Initialize()     self:SetModel( "models/props_junk/vent001.mdl" )     self:PhysicsInit( SOLID_VPHYSICS )     self:SetMoveType( MOVETYPE_VPHYSICS   )     self:SetSolid( SOLID_VPHYSICS )     self:SetUseType(SIMPLE_USE)     if VentLastID == nil then         VentLastId = 0     end     local phys = self:GetPhysicsObject()     if (phys:IsValid()) then         phys:Wake()     end end
Use Entity/NetworkVar to handle setting/getting the ID. Far easier and ensures the data is synced between the client and server. Only set the ID on the server and it will be updated on all clients.
Sorry, you need to Log In to post a reply to this thread.