Log inGet started
World

Procgen Effects Showcase

Branchmain
Branchesmain
Commit99ff90d12c

README.md

BenchGunner

BenchGunner is a gun that fires on its own, the way a player holding the trigger does: rate shots a second, each one a muzzle flash at the gun, a tracer down its aim and an impact where the round lands. The gun aims along its entity's forward (-Z). It holds one effect of each kind for as long as it exists, and a shot plays each of them again from its start.

The bench_mixed scene uses it. Its build script spawns eight guns, BenchGun1 to BenchGun8, as children of BenchRange, each with rate = 11 and distance = 30, to stand in for eight players holding the trigger.

The three effects are procGraph assets played through Generator components, by default effects.combat.muzzleFlash, effects.combat.tracer and effects.combat.impact.

Fields

FieldTypeDefaultDescription
ratenumber11Shots per second.
distancenumber30How far down the aim the rounds land, in metres.
muzzleFlashassetRef (procGraph)effects.combat.muzzleFlashThe flash at the gun on each shot.
tracerassetRef (procGraph)effects.combat.tracerThe round's trail down the aim.
impactassetRef (procGraph)effects.combat.impactWhat a round does where it lands.

All fields are Sync.

Lifecycle

  • start(): spawns one temporary child entity per effect field that names an asset, each with a Generator set to that graph and playOn = "manual". muzzle and tracer stand at the gun's origin, and the tracer's Generator gets the parameter distance. impact stands at { 0, 0, -distance } in the gun's space, turned to face back up the aim toward the gun.
  • update(dt): while rate is above 0, it accumulates dt * rate shots, held at most 2, and fires one shot per whole shot owed. A long frame fires at most one shot more than a short one. A shot calls seek(0) and then play() on each of the three Generators.
  • onDestroy(): despawns the child effects.

Usage

local gun = entity.spawn("BenchGun1")
gun.component.add("BenchGunner", { rate = 11, distance = 30 })