Fake Scripted Entity Creation
Sometimes you may need to create a new entity based on an engine entity (this means that it is not scripted). You may want to do this if you want to keep interesting properties of an engine entity but you want to modify it by adding or modifying methods.
In my example, you will not be able to use ENTITY hooks. Any hook has to be hard-coded in the footer of the code so it is triggered, like for any other engine entity. For example, you will be unable to simply add an ENTITY:Use or an ENTITY:Think method for them to work: you have to make the code that will trigger them. (I may add some examples in the future.)
In this example I create a new class
prop_vehicle_mr based on a
prop_dynamic. I could not create a real scripted entity because if I do so and I parent it to another entity (to a
prop_vehicle_jeep for example), the physics object does not move with it! So I needed to use a new class derivated from
prop_dynamic, because the physics object will by glued the entity itself.
In addition, this fake SENT footer is written in a way that can replace any method included in the metatable and that cannot be replaced in a standard SENT. In this example I replace the Entity:SetMaterial method in order to change the default material when resetting it.
Please note: the method and properties are only available serverside! This means that it will be seen clientside simply as an entity of its base class, except the classname: the methods and the properties will be that of the base class. (I may modify this in the future through a hook.)
Here I show you step by step how to write a fake scripted entity. This may to be done in a scripted entity file but it is supposed to work in any other place. The file of this example is:
First step: the header
The headers prepares the structure of the fake scripted entity.
First we define the Type of the fake SENT. It is certainly useless.
Then we create a clean ENT table, so nothing will be inherited from the SENT system.
We load the BaseClass which contains all default methods and all default properties for entities. It will be modified in the footer.
We specify the BaseClassName and the ClassName so the script knows what it has to deal with.
Second step: the classic AddCSLuaFile
Third step: the content
Here is where you will put default properties, methods and hooks for your fake SENT.
Stay aware of what you are doing while replacing methods from the metatable (BaseClass): it is dangerous and it might conflict with something else.
Somewhere else, after creating an entity of this class, I may change the default material for this entity:
Here are some examples of hooks that you may use. Remember: hooks that are not in this example are not implemented, it is your job.
Fourth step: the footer
The footer is the most important part of the fake SENT: it modifies the Lua functions to create the expected entity.
When calling ents.FindByClass("prop_vehicle_mr") we want to get a table containing all entities of the class
prop_vehicle_mr. Without the code, the table would be always empty.
When calling ents.FindByClass("prop_dynamic") we want to get a table containing all entities of the class
prop_dynamic, except that of the class
prop_vehicle_mr. Without the code, the table would also contain entities of the class
In other cases, ents.FindByClass will work like before.
We change the Entity:GetClass method in the Entity datatable so the proper value is returned for fake SENTs.
For some reason, the fake SENT class may need not to be shared with the client. In this case, use comment and uncomment the proper lines here and in the
ents.Create block below.
We modify all methods that exist in the Entity datatable that are defined in the content.
We add all methods that do not exist in the Entity datatable in a table to be added by ents.Create.
We add all properties in a table to be added by ents.Create.
We modify the ents.Create function. It adds a flag to mark the name of the fake SENT and it adds all properties and methods that are in the
Here is the complete example, free to use: