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:
lua/entities/prop_vehicle_mr.lua
First step: the header
The headers prepares the structure of the fake scripted
entity.
ENT.Type = "anim"
local ENT = {}
local BaseClass = FindMetaTable( "Entity" )
local BaseClassName = "prop_dynamic"
local ClassName = "prop_vehicle_mr"
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
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.
local old_SetMaterial = BaseClass.SetMaterial
function ENT:SetMaterial ( materialName )
if
materialName != nil and materialName != "" then
return
old_SetMaterial( self, materialName )
else
if
self.DefaultMaterial != nil then
return
old_SetMaterial( self, self.DefaultMaterial )
else
return
old_SetMaterial( self, "" )
end
end
end
No comments:
Post a Comment