Multiple same entity but with different vars & Use a single entity

Hi i’m trying to learn lua by making a gamemode. I have some problems and i don’t find any solution :

I have an entity_A and an entity_B, i need when this entity_A touch the entity_B to change a vars from the entity B.

1)How can i tell to the entity_A to change the vars from the entity_B -> i’m going to have multiple entity_B on my map and i won’t it(the entity_A) to affect all my entity_B, only the one that the entity_A is touching?

2)For my project i need the gamemode to get the vars from a specific entity_B on my map. How can i do that ?

Greeting Alex S

use the touch hooks for entities, eg when entity B touches entity A, you literally got entity B’s object, so you could do like ent.var = “meh” etc, and for “specific entities” begin with a loop, and get the class etc.

i did it in the init.lua (on my entity_B)



function ENT:touch( process ) // proces is my entity_A   and tour is my entity_B
tour.Myvariable = "1"
    return
end
 

The problem is that i can’t use this variable in any other files/function. is there an another way to do it? or did i forgot something?
also it set this var to all my entity_B on the map

Can you give me an example on how to do it?


 function() for k,v in pairs ( entity_B.GetAll() ) do

?

its ENT:Touch, not touch.

also you have to set ENTITY SPECIFIC vars, as written in my recent threads, which you can set in an ENTITY function, eg ENT:Draw is one, so you could do like in the touch function

self.var = process.var

for the loop:

ents.FindByClass

Lua noob lvl. over 9000

entity_B init.lua



function ENT:Use( activator, caller )
print(self.gotproces)
end
 
function ENT:Touch(proces)
self.gotproces = proces.gotproces
end


entity_B cl_init.lua



function ENT:Draw()
	self:DrawModel()
	self.gotproces = "0"
end


entity_A init.lua


function ENT:Touch( tour )
for k, v in pairs( ents.FindByClass( "tour" ) ) do
	if ( v:GetPos() == "335.748444, -240.061188, 99.55") then
proces.gotproces = "1"
self:Remove()
end end end

There is no error but the tour.gotproces won’t change(and is still nil).


(User was banned for this post ("Image macro / Meme reply" - Craptasket))

ENT:Draw was an example function…

OKKKKKKKKKKKKKK thanks a lot this problems seems fixed !
And i will try tomorow to use the loop .

You can set entity specific variables by storing it in the entity the same way you would a table. Say your variable is a number, you could do [entity].MyNumber = 1, where [entity] is your entity object. You can then at any time access and change these individually on each entity by doing [entity].MyNumber again. It should be noted that you don’t have to define these in ENT: functions, you can do [entity].variable anywhere you have its entity object, and this goes for all entities, even players.

E.g.
[lua]-- in Entity B
function ENT:Initialize()
self.MyNumber = 2
end

– Somewhere else in Entity B
function ENT:Use(ent, caller)
self.MyNumber = self.MyNumber + 1
– Count 1 up every time someone uses it
end

– Then to get the information on Entity A (This is in Entity A’s code)
function ENT:Touch(ent)
if IsValid(ent) and ent:GetClass() == “YourEntityName” then – Make sure it is indeed one of Entity B
self.LastTouchedEntsNumber = ent.MyNumber
– self = Entity A, and ent = Entity B in this case
end
end[/lua]
The above example shows how you could set a variable on each individual Entity Bs, which will then increase by 1 each time they’re used (press E), resulting in them having different numbers. Later, when Entity A touches Entity B, you can get that variable from that entity since you have got its entity object (basically the entity itself in the code, here it’s “ent”). It then sets Entity A’s variable to what that particular Entity B had.