Revision Difference
Entity_Creating_Custom_Entities#561283
<cat>Dev.Entities</cat>
<title>Creating Custom Entities</title>
#Creating Custom Entities in Garry's Mod
## 1. Introduction
Custom entities in Garry's Mod offer a way to enhance gameplay and developer flexibility by allowing creators to introduce unique elements, behaviors, and interactions into their games. These entities can range from simple, static models in the game world to complex, interactive objects that can interact with players and other entities in various ways. The ability to create custom entities opens up endless possibilities for game customization, making it a vital skill for any Garry's Mod developer aiming to craft engaging and dynamic game environments.
## 2. Fundamentals
### Basic Definitions
- Entities: In Garry's Mod, entities are objects in the game world. (For the most part)
### Key Components
- Initialization: The process of setting up an entity's default state.
- Logic Handling: The code that determines an entity's behavior and responses to game events.
- Shared Configuration: Defines properties across server and client.
## 3. Setting Up the Environment
### Workspace Preparation
- Familiarize yourself with the game's file system, especially the lua/entities directory where custom entities are stored.
##### Gamemode:
```
gamemodes/
└── mygamemode/
├── entities/
│ ├── entities/
│ │ └── testent/
│ │ ├── cl_init.lua
│ │ ├── init.lua
│ │ └── shared.lua
```
##### Addon:
```
addons/
└── myaddon/
├── lua/
│ ├── autorun/
│ │ └── myaddon_autorun.lua
│ ├── entities/
│ │ └── testent/
│ │ ├── cl_init.lua
│ │ ├── init.lua
│ │ └── shared.lua
```
### Tools and Resources
- [VSCode](https://code.visualstudio.com) or any preferred Lua editor for writing and editing code.
- [Video: "Ep. 13 Creating An Entity #1" by Codeblue](https://www.youtube.com/watch?v=uE5A4AwwIK8)
- [Video: "Ep. 14 Creating An Entity #2" by Codeblue](https://www.youtube.com/watch?v=rjgXF9gqiCU)
- [Video: "Ep. 25 Advanced Entity Example" by Codeblue](https://www.youtube.com/watch?v=Kr3xM6sgduA)
## 4. Creating an entity
- Create the Entity Files: Start by creating a folder with the addon or gamemode folder, `lua/entities` for your entity. Inside, create another folder, of the name of your entity e.g `testent`. It should look like this `lua/entities/testent`, then create three lua files: `shared.lua`, `cl_init.lua`, and `init.lua`.
- Define the Entity: In shared.lua, define basic properties like entity type, base, and print name.
- Server-Side Setup: Use init.lua to define server-side logic, such as model, physics, and initialization behavior.
- Client-Side Setup: cl_init.lua handles client-side aspects, primarily drawing the entity model.
### shared.lua example:
```lua
-- Defines the entity's type, base, printable name, and author for shared access (both server and client)
ENT.Type = "anim" -- Sets the entity type to 'anim', indicating it's an animated entity.
ENT.Base = "base_gmodentity" -- Specifies that this entity is based on the 'base_gmodentity', inheriting its functionality.
ENT.PrintName = "Test Entity" -- The name that will appear in the spawn menu.
ENT.Author = "YourName" -- The author's name for this entity.
ENT.Category = "Test entities" -- The category for this entity in the spawn menu.
ENT.Contact = "STEAM_0:1:12345678" -- The contact details for the author of this entity.\⤶
ENT.Contact = "STEAM_0:1:12345678" -- The contact details for the author of this entity.⤶
ENT.Purpose = "To test the creation of entities." -- The purpose of this entity.
ENT.Spawnable = true -- Specifies whether this entity can be spawned by players in the spawn menu.
```
#### init.lua example:
```lua
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
-- Server-side initialization function for the entity
function ENT:Initialize()
self:SetModel( "models/hunter/blocks/cube025x025x025.mdl" ) -- Sets the model for the entity.
self:PhysicsInit( SOLID_VPHYSICS ) -- Initializes physics for the entity, making it solid and interactable.
self:SetMoveType( MOVETYPE_VPHYSICS ) -- Sets how the entity moves, using physics.
self:SetSolid( SOLID_VPHYSICS ) -- Makes the entity solid, allowing for collisions.
local phys = self:GetPhysicsObject() -- Retrieves the physics object of the entity.
if phys:IsValid() then -- Checks if the physics object is valid.
phys:Wake() -- Activates the physics object, making the entity subject to physics (gravity, collisions, etc.).
end
end
```
### cl_init.lua example:
```lua
include("shared.lua")
-- Client-side draw function for the entity
function ENT:Draw()
self:DrawModel() -- Draws the model of the entity. This function is called every frame.
end
```
The structure should look like this
```
└── lua
└── entities
└── testent
├── cl_init.lua
├── init.lua
└── shared.lua
```
### Result:
<upload src="b5453/8dc30024d6e1a37.png" size="15705" name="image.png" />