Adding Muzzle Flashes that lights up the world

From Valve Developer Community
Jump to: navigation, search

This tutorial will show how to add dlight muzzle flash that lights up the world around the player dynamically in GoldSrc.

Coding

First navigate to ev_hldm.h and scroll to the bottom. After that, add this

void EV_HLDM_MuzzleFlash( vec3_t pos, float amount );

Then, open ev_hldm.cpp and find:

float EV_HLDM_PlayTextureSound( int idx, pmtrace_t *ptr, float *vecSrc, float *vecEnd, int iBulletType )

Below that, add this:

void EV_HLDM_MuzzleFlash(vec3_t pos, float amount)
{
    // make a dlight first
    dlight_t *dl = gEngfuncs.pEfxAPI->CL_AllocDlight(0);

    // Original color values
    int originalR = 231;
    int originalG = 219;
    int originalB = 14;

    // Randomize color components within the range of +/- 20
    dl->color.r = originalR + gEngfuncs.pfnRandomLong(-20, 20);
    dl->color.g = originalG + gEngfuncs.pfnRandomLong(-20, 20);
    dl->color.b = originalB + gEngfuncs.pfnRandomLong(0, 0);

    // Randomize the die value by +/- 0.01
    dl->die = gEngfuncs.GetClientTime() + 0.05 + gEngfuncs.pfnRandomFloat(-0.01, 0.01);

    // Randomize the radius based on amount
    dl->radius = gEngfuncs.pfnRandomFloat(245.0f, 256.0f);

    // Randomize the decay value
    dl->decay = gEngfuncs.pfnRandomFloat(400.0f, 600.0f);
}

Then, find the function definition:

void EV_FireWeaponName(event_args_t *args) // replace WeaponName with your weapon’s name

Find this in that function:

EV_GetGunPosition( args, vecSrc, origin );

Just after that, add this:

EV_HLDM_MuzzleFlash( vecSrc, 1.0 + gEngfuncs.pfnRandomFloat( -0.2, 0.2 ) );

Credits and Reference

  • This guide is a modified version of the guide from VERC and TWHL.
  1. https://web.archive.org/web/20061027233737/http://collective.valve-erc.com/index.php?doc=1031137353-13157900
  2. https://twhl.info/wiki/page/VERC%3A_Adding_muzzle_flashes_that_also_light_up_world_brushes
  3. This article's randomizer code was used here
  • Also, there’s something called elights which doesn’t light up the world but entities and models.