Garry's Mod Wiki

ents.Create

  Entity ents.Create( string class )

Description

Creates an entity. This function will fail and return NULL if the networked-edict limit is hit (around 8176), or the provided entity class doesn't exist.

Do not use before GM:InitPostEntity has been called, otherwise the server will crash! If you need to perform entity creation when the game starts, create a hook for GM:InitPostEntity and do it there.

Arguments

1 string class
The classname of the entity to create.

Returns

1 Entity
The created entity, or NULL if failed.

Example

Creates a gmod_button entity at Vector(0, 0, 0).

local button = ents.Create( "gmod_button" ) button:SetModel( "models/dav0r/buttons/button.mdl" ) button:SetPos( Vector( 0, 0, 0 ) ) button:Spawn()

Example

Make the Half Life Jeep appear where the player is looking

concommand.Add("spawn_vehicle_test", function(pPlayer) if not IsValid(pPlayer) or not pPlayer:IsSuperAdmin() then return end local sClass = "Jeep" local tData = list.Get("Vehicles")[sClass] // Get the vehicle data from the list local eVehicle = ents.Create("prop_vehicle_jeep") if not IsValid(eVehicle) then return end eVehicle:SetModel(tData.Model) // Set the vehicle model eVehicle:SetPos(pPlayer:GetEyeTrace().HitPos + Vector(0, 0, 10)) eVehicle:SetKeyValue("vehiclescript", tData["KeyValues"]["vehiclescript"]) // Set the vehicle script eVehicle:Spawn() eVehicle:Activate() hook.Run("PlayerSpawnedVehicle", pPlayer, eVehicle) // Call the hook for spawning a vehicle. This hook is used for all addons that wish to make modifications to the vehicle, such as adding a props to the vehicle. end)