Garry's Mod Wiki

Revision Difference

BaseClasses#561725

<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>. ⤶ # Terms and Definitions⤶ A **Class** is a collection of Functions and Variables defined in code that acts as a template. Examples include <page text="The Player">player</page>, <page text="Entities">Entity</page>, <page text="Gamemodes">Structures/GM</page>, <page text="VGUI Elements">VGUI_Element_List</page>, and more. ⤶ An **Instance** is a copy of a Class that is active in the game. There can be multiple Instances of a single Class active at the same time. ⤶ For example: When a prop is spawned from the Sandbox Spawn Menu, the game creates an Instance of the Class `prop_physics`. ⤶ The Class `prop_physics` is only defined in code once but it can be used to create any number of Instances. ⤶ To make efficient use of computer hardware, the logic of a Class is not duplicated to each Instance, but instead the Instances all refer back to the same definition in the Class. This is handled via a <page text="Meta Table">meta_tables</page>.⤶ ⤶ A **Base Class** (or **Parent Class**) is the set of Functions and Variables that a Class (the Parent Class's **Child Class**) has by default. The behavior of the Parent Class is modified by overriding, replacing, and adding Functions and Variables to the Child Class. This allows a Class to copy the behavior of another Class without needing to re-create the other Class's logic from scratch. To prevent unnecessary duplication of logic, a Child Class refers to its Parent Class's Meta Table in situations where the Child Class doesn't have an override defined for a Function or Variable. ⤶ **Note:** All Classes must have exactly one (1) Base Class, but can have any number of Child Classes.⤶ # 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 ```