Project 02: Ped spawner
In Project 01, you altered a global environment setting. In Project 02, you create your very first physical living entity inside Los Santos. Your goal is to build a robust keyboard spawner: stream an actor model into memory, place the pedestrian directly ahead of the player using spatial vector math, guard strictly against double spawning, and guarantee that the entity is deleted cleanly both on demand and when reloading the script.
The mission
Build a resilient pedestrian spawner satisfying five essential requirements:
- Stream a specific NPC character model asynchronously into memory with timeout safety.
- Calculate a 3D position 2 meters directly ahead of the player using vector arithmetic.
- Spawn the pedestrian facing the player's direction when pressing
NumPad 1. - Guard strictly against duplicate spawns: refuse creation and notify the player if an entity is already active.
- Delete the entity immediately when pressing
NumPad 0, and guarantee zero entity leakage when reloading.
Specifications, constraints
- Memory hygiene: Always release the model handle with
MarkAsNoLongerNeeded()inside afinallyblock so streaming assets do not leak in RAM. - Single active entity: Track the pedestrian using a private field
_spawnedand verify both_spawned != nulland_spawned.Exists()before any action. - Spatial vector math: Compute world spawn coordinates using
player.Position + player.ForwardVector * 2f. - Zero entity leakage: Implement clean teardown in
OnAbortedso reloading the script never leaves ghost NPCs behind in the world.
Implementation steps
- Track entity state: Define a private field
private Ped _spawned;in your script class. - Wire lifecycle events: In the constructor, register event handlers for
KeyDownandAborted. - Guard against duplicates: At the start of your spawn routine, check if
_spawnedis non-null and currently exists. If so, post a ticker notification and return early. - Stream the asset model: Instantiate a
ModelwithPedHash.Trevorand callmodel.Request(2000). If the request times out or the asset is invalid, release the model and exit. - Calculate offset and instantiate: In a
try-finallyblock, obtainGame.Player.Character, calculate the position 2 meters ahead usingplayer.ForwardVector, and callWorld.CreatePed. - Release the model: In the
finallyblock, callmodel.MarkAsNoLongerNeeded(). - Implement deletion: Build a helper method that checks if
_spawnedexists, calls_spawned.Delete(), and sets_spawned = null. - Wire controls and teardown: Bind
Keys.NumPad1to spawn,Keys.NumPad0to delete, and call your deletion helper inOnAborted.
APIs, tools to explore
GTA.Model: Wrapper representing a game model asset in the game's CD image archives.Model.Request(int timeout): Asynchronously streams model geometry, textures, and skeletons into RAM.Model.MarkAsNoLongerNeeded(): Marks the asset as disposable so the engine can free memory when needed.GTA.World.CreatePed(Model model, Vector3 position, float heading): Spawns a physical pedestrian into the game world.Ped.ForwardVector: A normalized unit vector pointing directly forward along the entity's current orientation.Entity.Exists(): Validates that an entity's native handle is currently valid, allocated, and present in the world.Entity.Delete(): Removes the entity from the physical world and deallocates its native resources immediately.
Validation checklist
Your mod is validated when:
- Pressing
NumPad 1: Trevor appears exactly 2 meters in front of the player, facing the same direction. - Pressing
NumPad 1a second time: A notification announces that a ped is already active, and no duplicate ped spawns. - Pressing
NumPad 0: Trevor vanishes instantly, and the reference is cleared. - Spawning Trevor and pressing
Insertto reload: Trevor disappears cleanly without leaving an orphan NPC in Los Santos.
Solution, explanations
Partner
Verified solution and code explanations
The mission, specifications, and guided steps remain 100% free and open for everyone. The complete verified reference code and production explanations are reserved for Partner members.