Entity-instance specific variables?

I noticed that code like this:


SomeVariable = 5

Is global for all weapons of that class.
(The file is for a brush entity)

Any way to make them different for each instance?

Yes, assign the variable to the entity object.

Entity.Variable = “shit”

I get this:


attempt to index global 'Entity'

The line number is the first line that has Entity. (Not ENT)

Full code:


ENT.Base = "base_brush"
ENT.Type = "brush"

Entity.keyValues = {}

Entity.dialogs = nil
Entity.dialogIndex = 1

function ENT:KeyValue(key, value)
	Entity.keyValues[key] = value
	if key == "dialog" then Entity.dialogs = nil end
	print(key .. " ... " .. value)
end

function ENT:StartTouch(entity)
	print("the thing is " .. Entity.keyValues["allAtOnce"])
	if Entity.keyValues["allAtOnce"] == "1" then
		for k, v in pairs(string.Split(Entity.keyValues["dialog"], "|")) do
			Astro.say(Entity.keyValues["who"], v)
		end
		print("hehe")
	elseif Entity.keyValues["allAtOnce"] == "0" then
		if Entity.dialogs == nil then
			Entity.dialogs = string.Split(Entity.keyValues["dialog"], "|")
		end
		Astro.say(Entity.keyValues["who"], Entity.dialogs[Entity.dialogIndex])
		Entity.dialogIndex = Entity.dialogIndex + 1
		if Entity.dialogIndex > #Entity.dialogs then Entity.dialogIndex = 1 end
	end
end

function ENT:EndTouch(entity)
end

function ENT:SetBounds( width, length, height )
	self:SetSolid( SOLID_BBOX )
	self:SetCollisionBounds( Vector(-width/2, -length/2, -height/2), Vector(width/2, length/2, height/2) )
	self:SetTrigger( true )
end 

Don’t use Entity at all- use ENT for everything, or you’ll probably be adding keys to the Entity FUNCTION instead (which returns an entity based on it’s entindex)

[editline]13th August 2016[/editline]

I haven’t tested it at all, but by replacing some stuff your code looks like this:



ENT.Base = "base_brush"
ENT.Type = "brush"

ENT.keyValues = {}

ENT.dialogs = nil
ENT.dialogIndex = 1

function ENT:KeyValue(key, value)
	self.keyValues[key] = value
	if key == "dialog" then self.dialogs = nil end
	print(key .. " ... " .. value)
end

function ENT:StartTouch(entity) -- I wasn't sure what you were exactly doing with this so I probably replaced some stuff wrong
	print("the thing is " .. entity.keyValues["allAtOnce"]) -- If you're trying to use the current entity and check that, then use 'self'
	if entity.keyValues["allAtOnce"] == "1" then
		for k, v in pairs(string.Split(entity.keyValues["dialog"], "|")) do
			Astro.say(entity.keyValues["who"], v)
		end
		print("hehe")
	elseif entity.keyValues["allAtOnce"] == "0" then
		if entity.dialogs == nil then
			entity.dialogs = string.Split(entity.keyValues["dialog"], "|")
		end
		Astro.say(entity.keyValues["who"], entity.dialogs[entity.dialogIndex])
		entity.dialogIndex = entity.dialogIndex + 1
		if entity.dialogIndex > #entity.dialogs then entity.dialogIndex = 1 end
	end
end

function ENT:EndTouch(entity)
end

function ENT:SetBounds( width, length, height )
	self:SetSolid( SOLID_BBOX )
	self:SetCollisionBounds( Vector(-width/2, -length/2, -height/2), Vector(width/2, length/2, height/2) )
	self:SetTrigger( true )
end


I replaced Entity with ENT outside of functions. Inside of functions I use self.
It still does not work. Both entities have the same properties even though I set different ones in Hammer.

Are you sure KeyValue is being ran? Have you put prints inside and tested?

There is already a print statement in KeyValue, it is ran correctly.

24 hoors so bump.

Post your current code, as well as the current problem


ENT.keyValues = {}

creates a reference to keyValues table in the entity metatable, which points to same table reference in all instances. You need to create one table per entity, which you can do by creating keyValues table inside ENT.Initialize