Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Tutorial] Random music/sound selection for ambient_generic  (Read 143 times)

0 Members and 1 Guest are viewing this topic.

Offline Dazza-chan

  • *
  • +1% chance to make useful post
  • Dazza-chan has no influence.
[Tutorial] Random music/sound selection for ambient_generic
« on: March 31, 2021, 03:21:08 AM »
This is a short tutorial on how to randomise what sound or song is chosen for an ambient_generic to play on creation. this is fairly simple and easy to do as all it requires is the creation of a table in the .lua file (or common.lua if you plan on consistently reusing the same set of songs between maps) then the creation of an ambient_generic (same as documented here http://bsnooch.com/forums/index.php?topic=2174.0). This is a great little setup for use with music containing imbedded sounds cues to keep battle music flowing for the entirety of a fight.

.txt section
Code: [Select]
"Entities"
{

"instant_trig"
{
"keyvalues"
{
"timer" "0.1"
"label" "ini"
}
}

"lua"
{
"callfunc" "Initialize"
}

}


"Entities:ini"
{

"lua"
{
"callfunc" "SpawnInitialize"
}

}

.lua section
Code: [Select]
Music_list = { --creates a table containing a group of soundscript names, in this case custom songs found under level_sounds_music.txt

"Conflict",
"ControllingtheIronBeast",
"Encounter",
"LastOrder",
"SoldierBattle",
"TheFaceofLostPride",
"TheSummoned",
"ProtectionOfTheStars",
"CombatMus1",
"CombatMus3",
"Invasion",

}


function Initialize()

local amb = HL2.CreateEntity("ambient_generic", VECTORZERO, VECTORZERO)
if( amb ~= nil ) then
HL2.KeyValue(amb, "message", Music_list[HL2.RandomInt(1, table.getn(Music_list))]) --grabs a song from the music_list table and assigns it to the ambient_generics "message" (soundname) keyfield
HL2.KeyValue(amb, "spawnflags", "16") --just a spawnflag to make the ambient_generic start silent
HL2.KeyValue(amb, "targetname", "Startmusic") --targetname of the ambient_generic
HL2.KeyValue(amb, "health", "10") --volume of the ambient_generic
HL2.SpawnEntity(amb)
end

end

function SpawnInitialize()

HL2.EntFire("Startmusic", nil, "PlaySound", nil, 1) --send an input to the ambient_generic to play its sound after a 1 second delay

end

video of what this actually did:

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal