Lua Entities and Hammer

Now, I know if you create an entity for Garrysmod you can put it into a map by just adding an entity with its name, and I know how to make FGD files and use them and whatnot. But my real question is communication between them. To explain this I think its best to do an example.

So, I make an entity called “material_barrel” and this barrel has different types that are calculated when it’s spawned. It spawns a different kind of barrel depending on what weapon your holding.

Now, I want to make a map that has these barrels, but you can choose what material it will be.
This is what the FGD would be:

[…] = material_barrel:
“Its just an example, people”
[
barrel_types(choices) : “Type” : 0 =
[
0 : “Oil”
2 : “Gas”
3 : “Crack”
]
]

So, I spawn an entity, and pick “Crack” (what did you think I’d pick?). Now, I what I expect to happen is to have the barrel spawn as “Crack” when themap loads. How do I make the Lua script get what is set in the map?

you need to use the ENT:KeyValue( key, value) on the entity, this gets called before the initialize and it receives the values from the map. I am working on a trigger_teleport entity and had to use this to get the trigger info. here’s the wiki http://wiki.garrysmod.com/?title=ENT.KeyValue

example:
[lua]

function ENT:KeyValue( key, value )

            -- Find out what's being passed
	print(tostring(self) .. " with -- Key : " .. key .. "     Value : " .. value)
	
	if key == "ScriptName" then 
		self.ScriptName = value
	elseif key == "model" then
		self.model = value
	elseif key == "targetname" then
		self.targetname = value
	elseif key == "target" then
		self.target = value
	elseif key == "StartDisabled" then
		self.StartDisabled = value
	elseif key == "spawnflags" then
		self.spawnflags = value
	elseif key == "origin" then
		self.origin = value
	elseif key == "landmark" then
		self.landmark = value
	elseif key == "classname" then
		self.classname = value
	elseif key == "hammerid" then
		self.hammerid = value
	elseif key == "OnStartTouch" then
		self.OnStartTouch = value
	elseif key == "OnEndTouch" then
		self.OnEndTouch = value
	else
		print("!!Unknown Key Value!!")
		print("Key : " .. key .. " -- Value : " .. value)
	end
	
	--[[ Keys and Values for a trigger_teleport
	
	Entity [14][sent_brush] with -- Key : ScriptName     Value : trigger_teleport
	Entity [14][trigger_teleport] with -- Key : model     Value : *2
	Entity [14][trigger_teleport] with -- Key : targetname     Value : Teleport_Top
	Entity [14][trigger_teleport] with -- Key : target     Value : TeleDest_Bottom
	Entity [14][trigger_teleport] with -- Key : StartDisabled     Value : 0
	Entity [14][trigger_teleport] with -- Key : spawnflags     Value : 15
	Entity [14][trigger_teleport] with -- Key : origin     Value : 0 0.01 14946.5
	Entity [14][trigger_teleport] with -- Key : landmark     Value : TeleDest_Top
	Entity [14][trigger_teleport] with -- Key : classname     Value : trigger_teleport
	Entity [14][trigger_teleport] with -- Key : hammerid     Value : 4934
	Entity [14][trigger_teleport] with -- Key : OnStartTouch     Value : Teleport_Bottom,Disable,,0,-1
	Entity [14][trigger_teleport] with -- Key : OnEndTouch     Value : Teleport_Bottom,Enable,,0.01,-1
	--]]

end
[/lua]

[lua]function ENT:KeyValue( key, value )
self.KeyValues = self.KeyValues or {};
self.KeyValues[key] = value;
end[/lua]

Sorry, it just saddens me to see people doing ridiculous amounts of work which using tables could simplify to a few lines.