Gambit37 wrote:I can't see a property for this in the object definitions for monsters. Could this be added to DSB?
There are a few functions that can help you.
on_damage(arch, id, dmg_amount, weapon_arch, damage_type) is executed any time a monster takes any sort of damage. Often, you only want to respond to melee hits, so there is also the
on_take_melee_damage(arch, id, ppos, who, what, method_name, hit_power) event. Weapons also have the
on_melee_damage_monster(weapon_arch, weapon_id, ppos, who, monster_id, hit_power) event. I don't think any of this is very well documented, as with many things in DSB, so it's probably not that hard to miss.
For example, here is some code to play whatever sound is contained in the
pain_sound property. All you have to do to use it is make sure to set
on_damage or
on_take_melee_damage to
monster_hit_sound. It delays the sound by 1 tick so it doesn't disappear into any weapon attack sound.
Code: Select all
function monster_hit_sound(arch, id)
dsb_delay_func(1, function () local_sound(id, arch.pain_sound) end)
end
Gambit37 wrote:Also, related, when specifying values for an objects properties, can any value also be a function? For example, if I called a function on the
attack_sound property, could the function generate a random sound and pass it back, something like this? (Note, this is my first attempt at Lua in DSB and may be all sorts of wrong

)
I've tried to make many object properties able to be value-returning functions. This one wasn't set up to be... but will be in DSB 0.48, so you'll be able to do exactly that.
As to how to actually do this, you had the basic right idea, but there are two caveats. I'll take this chance to correct your code at the same time.

First, never use
math.random in DSB; always use
dsb_rand.

Second, you should load all of your sounds ahead of time. Then just do something like this:
Code: Select all
function random_orc_attack_sound(arch, id)
return snd.orc_attack[dsb_rand(1, 4)]
end
Your load code (somewhere in your own custom
graphics.lua) could look like:
Code: Select all
snd.orc_attack = { } -- Declare it as a table
for num=1,4 do
snd.orc_attack[num] = dsb_get_sound("ORCATTACK" .. num, "files/sfx/foe/orc/orc_attack" .. num .. ".wav")
end