Garry's Mod Wiki

Revision Difference

BaseClasses#561708

<cat>don'tlistmeplease</cat> <title>BaseClasses</title> # What is a Base Class? **A Base Class is the template or blueprint that a Class (Like an Entity, a Scripted Weapon, a Gamemode, etc.) is based on.** ⤶ ⤶ Practically, this is a simple form of [Inheretence](https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)) using <page text="Meta Tables">Meta_tables</page>. ⤶ ⤶ # How do you use Base Classes?⤶ ⤶ The following example demonstrates a Parent / Child Entity Class relationship. The Child Entity uses the Parent Entity as its Base Class.⤶ ⤶ The Parent Entity Class creates a pair of functions that start and end a visual effect, then it uses them to render itself with that visual effect. ⤶ The Child Entity Class uses the Parent Entity Class as its Base Class, then overrides the function that starts the visual effect. This allows it to appear quite different from its Base Class without needing to re-write all of the logic from its Parent Base Class.⤶ ⤶ ## sent_parentclass.lua⤶ ```lua⤶ AddCSLuaFile()⤶ ⤶ -- All Entities must have a base class.⤶ -- In this case, the "base_anim" class is being used.⤶ -- This is one of the default Entity classes available in Garry's Mod.⤶ DEFINE_BASECLASS( "base_anim" )⤶ ⤶ ENT.PrintName = "Parent Class"⤶ ENT.Category = "BaseClass Example"⤶ ENT.RenderGroup = RENDERGROUP_TRANSLUCENT⤶ ENT.Spawnable = true⤶ ⤶ -- This variable exists to demonstrate that a Base Class provides both ⤶ -- functions and variables to their children.⤶ -- This is a regular-width crate model.⤶ ENT.ModelToSpawnWith = "models/props_junk/wood_crate001a_damaged.mdl"⤶ ⤶ -- Set up the Entity⤶ function ENT:Initialize()⤶ if SERVER then⤶ self:SetModel( self.ModelToSpawnWith )⤶ self:PhysicsInit( SOLID_VPHYSICS )⤶ self:SetMoveType( MOVETYPE_VPHYSICS )⤶ self:SetSolid( SOLID_VPHYSICS )⤶ end⤶ end⤶ ⤶ -- This is a custom function we'll be using to demonstrate that children can⤶ -- override functions from their Base Class.⤶ function ENT:EnableVisualEffects()⤶ -- Modify upcoming rendering to make it look blue.⤶ render.SetColorModulation( 0, 0, 1 )⤶ end⤶ ⤶ -- This custom function won't be overrideen by the child Entity, but that is ⤶ -- purely for demonstration purposes. Any function from the base class can⤶ -- be overridden by its children.⤶ function ENT:DisableVisualEffects()⤶ -- Reset the color of the rendering system.⤶ render.SetColorModulation( 1, 1, 1 )⤶ end⤶ ⤶ -- Here we use the custom functions above to change how the entity draws.⤶ -- Because we do not override this function in the child, this will be the⤶ -- Draw function for the child Entity. ⤶ function ENT:Draw()⤶ self:EnableVisualEffects()⤶ self:DrawModel()⤶ self:DisableVisualEffects()⤶ end⤶ ```⤶ ⤶ ## sent_childclass.lua⤶ ```lua⤶ AddCSLuaFile()⤶ ⤶ -- Set our Base Class to be the class we want to inhereit functions and values from.⤶ -- It would be equally valid to use DEFINE_BASECLASS.⤶ -- baseclass.Get() is being used here to demonstrate that both options are viable.⤶ ENT.BaseClass = baseclass.Get( "sent_parentclass" )⤶ ⤶ ENT.PrintName = "Child Class"⤶ ENT.Category = "BaseClass Example"⤶ ENT.Spawnable = true⤶ ⤶ -- Override the variable in our Base Class that is used to set the Entity's model⤶ -- Because we haven't overridden the ENT:Initialize() function, that logic will run⤶ -- exactly as it appears in our Base Class, but with this value instead of the value⤶ -- defined in the Base Class.⤶ -- This is an extra-wide crate model.⤶ ENT.ModelToSpawnWith = "models/props_junk/wood_crate002a.mdl"⤶ ⤶ -- Override the visual effects function to randomize the entity's color.⤶ function ENT:EnableVisualEffects()⤶ local red = math.Remap( math.sin( CurTime() * 2 + 1 ), -1, 1, 0, 1 )⤶ local green = math.Remap( math.cos( CurTime() * 3 + 2 ), -1, 1, 0, 1 )⤶ local blue = math.Remap( math.sin( CurTime() * 4 + 3 ), -1, 1, 0, 1 )⤶ render.SetColorModulation( red, green, blue )⤶ end⤶ ```⤶ ⤶