OnScriptHook_ variants of Precache and OnPostSpawn
These hooks are only called when the entity is spawned with the vscripts keyvalue set to the file that contains this function. This behavior is fantastic for entity spawns the mapper/scripter can control, but it would be nice if there was a generic hook like OnScriptHook_OnTakeDamage, where the params table contains information about the entity such a handle, entindex, classname, etc.
Currently the only way to achieve a generic entity spawning detection is by iterating through every entindex in a think function, or narrowing the think down to a few FindByClassname loops. A generic entity spawn hook similar to OnTakeDamage would be a much more elegant way to handle this, not to mention the methods mentioned only really work as an alternative to OnPostSpawn. The most common use case for this would be detecting player projectile spawns, but this could also be used for tracking entity spawns with a server-wide script using mapspawn.nut (also see https://github.com/ValveSoftware/Source-1-Games/issues/6356).
Potential implementation:
function OnScriptHook_Precache(params)
{
// entity info here
local ent = params.entity
local idx = params.entindex
local classname = params.classname
// Precache/ConnectOutputs hooks could optionally return false to stop the entity spawn completely, similar to OnTakeDamage
return false
}
function OnScriptHook_OnPostSpawn(params)
{
printf("%s Has spawned at %s. entindex: %d\n", params.classname, params.entity.GetOrigin().ToKVString(), params.entindex)
//Example output, rocket spawn at map origin:
// tf_projectile_rocket Has spawned at 0 0 0. entindex: 94
}