Creating Custom Entities/Entity
Creating a Custom Entity in Garry's Mod
Introduction
What are Entities?
Nearly everything you can see and touch in Garry's Mod is an Entity. Any object with a position in the game world is an Entity. The Player is an Entity, props are Entities, even the the Game World itself is an Entity—albiet a special and unique one.
Entities are able to have custom, developer-defined behaviors that can control things like the way they look and the way they can be interacted with. Entities are often foundational parts of Gamemodes and Addons
What are the components of an Entity?
Entities have aspects in all 3 Realms:
- Client - Drawing/Rendering the Entity.
- Server - Controling the Entity's behavior and interactions.
- Shared - Configuration and properties available to both Server and Client Realms.
Shared isn't really a Realm. It only means that both Client and Server will run the same code that's in a Shared file.
Setup
File Location
The file(s) that make up an Entity should be placed either in an Addon or in a Gamemode.
Addon Entity Location
Gamemode Entity Location
File Structure
Entities can be created either using
- 3 separate files that each contain one Realm's code and configuration.
- This approach helps with code organization.
- The name of the folder containing these files (
my-entity-name
in the example below) will be used as the Entity's Class Name.
- The files are:
cl_init.lua
- The Client Realm
init.lua
- The Server Realm
shared.lua
- The Shared Realm
- 1 file containing all the code and configuration for all 3 Realms.
- This approach is primarily useful when creating simple entities.
- The file's name (
my-entity-name
in the example below) will be used as the Entity's Class Name.
Creating an Entity
Creating an Entity with separate files
shared.lua example:
init.lua example:
cl_init.lua example:
Creating an Entity with a single file
Not sure what any of these do? Feel free to click on any of the blue functions() to find out.
ENTITY:Initialize() and ENTITY:Draw() are considered Hooks. These aren't clickable in the above examples, but you can search for hooks by typing is:event
(two spaces) into the top left of this page.
Whenever these Hooks are called by the game, your Entity runs the code you’ve defined for them. They're typed ENT here in order to register your entity's object class.
Results
Regardless of whether you've created the Entity with one file or several, the final result should be the same.