Understanding weapons/scripted_ents.Register

I found out about these functions the other day and they seem like a way to load new SENTs into the game on the go without having to restart the map. Is my fundamental understanding of this wrong?

If not, my next question pertains to how locals are handled. The func accepts arguments for a swep/sent table, but if a script contains locals defined outside of functions, how would they be handled?

scripted_ents.Register lets you register your entity manually. If you have your code in lua/entities/entity_name/ they are registered automatically using the folder name as the entity name; you can take all the same code and put into a file outside of the entities folder and call scripted_ents.Register with the ENT table and it’s exactly the same.

In regards to loading new entities on the go, you can definitely do it that way, as long as it’s refreshed on the server and client.

Local variables are handled the way they always are as per lua scoping. Every instance of the entity would have access to that 1 variable. (assuming it was done like this)
[lua]local SharedVar = 1 --SharedVar can be changed by all instances of new_ent

local ENT = {}
–add functions for ENT:Initialize, ENT:Draw,
–those functions can all access the local SharedVar

scripted_ents.Register(ENT, “new_ent”)
[/lua]



_G.ENT
local ENT=ENT
DEFINE_BASECLASS("base_anim")

-- etc

scripted_ents.Register(ENT, "props")
_G.ENT = nil


Alright, so I’d just pass a table to _G.ENT and then register it. Locals would have to be defined outside of this but would still be usable by the SENT.
Now suppose I wanted to make this work with something such as text files under /data so I could add stuff dynamically. How would I make it so that entities with similar locals wouldn’t overwrite each other?

how is this even possible

[editline]29th March 2014[/editline]



function ENT:Initialize()
	self.Mine = 0
end

function ENT:Think()
	self.Mine = self.Mine + 1
end


[editline]29th March 2014[/editline]



function ENT:Initialize()
	self.Mine = 0
end

function ENT:Think()
	self.Mine = self.Mine + 1
end


The idea is that not all the sweps or sents would be my own. I don’t want to replace all the locals with globally indexed vars.

I might as well just explicitly say that this is for me to make a proof of concept for a third party Toybox.