Multiple of Same Entity Causing Confliction?

Hello Facepunch, so I’m having a bit of an issue with an entity I’ve created.
The entity uses a variable to determine whether the entity’s animation should be sped up, or slowed down.
The problem is that when I spawn the same entity multiple times, it seems like the variable of one controls all 3 of their animation speeds, whereas I just want it so each of the single entity listens to its own variable.

Basically, I want the entity to be independent from its duplicates. Any tips?

Do not use a single global variable, but assign the variable to the entity.

See how the MinSize and MaxSize variable are defined here and how are they used inside ENT: functions.

As you have described it im guessing that the variable you’ve defined would look something like this right?


local Velocity = 0 -- just an example
function ENT:Think()
     Velocity = Velocity + 0.2 -- still an example
end

If you want the variable to be unique per entity, you shouldnt define it like that, because that will be the same variable for EACH of them.
So what you should do is create a variable INSIDE the entity. How do you do this you ask?
Just concatenating the Entity on Initialize and setting a variable inside it or as robotboy said out of it indexing the ENT being created. Example:


function ENT:Initialize()
     self.Velocity = 0 -- Variable is defined here for each different entity created from your entity base (or just entity)
end

-- OR

ENT.Velocity = 0

So basically now the variable you have to use in hooks like think is instead of “Velocity” would be “ENT.Velocity”, being ENT the entity and therefore it being replaced by “self” in most of the entity hooks. So the think hook for it would finish something like this:


function ENT:Think()
     self.Velocity = self.Velocity + 0.2
end

This is exactly the same as before, but being a unique velocity per each entity created.

[editline]28th October 2016[/editline]

Ninja’d!

Thanks for the quick response, I looked at it and was able to understand.

Thank you for the detailed response, just seeing what you were describing was definitely helpful.
This post has been solved, thanks to the both of you. :happy: