4aiman wrote:Why not use get_player_controls() ?
You get the controls and if a LMB was "pressed" and your player:get_wielded_item():get_name == <name of your sword> then play your sound. ;)
*Names of the functions I've mentioned are not 100% exact.
Thanks for the suggestion. I am looking through docs to try and find how to use this. Maybe this will be my ticket...
Regarding other methods, it seems that using after_use to play the sound is about the closest I've gotten to making this work like I was hoping. However, it works only as long as the sword is used on a block. Hitting a mob or swinging at nothing does not play the sound which doesn't make any sense from a "it should make a sound when you swing it" standpoint.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
after_use = function(itemstack, user, node, digparams)
minetest.sound_play("laser_mod")
itemstack:add_wear(digparams.wear)
return itemstack
end
So, I've been trying the other way around, using on_use to play the sound, and after_use to duplicate the default on_use functionality, but I must be doing something wrong. If I use both on_use and after_use, only the on_use function is executed. The after_use function doesn't seem to do anything at all.
According to the lua api docs, after_use needs to return itemstack or nil (
https://github.com/minetest/minetest/bl ... .txt#L2398), so I initially thought, "ah-ha, this is my problem", but even after adding this return the problem persists. Any ideas? This is what I have so far, just a basic test, but after_use does not execute:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
on_use = function(itemstack, user, pointed_thing)
print("on_use, play sound")
minetest.sound_play("laser_mod")
return nil
end,
after_use = function(itemstack, user, node, digparams)
print("after_use, use sword")
itemstack:add_wear(digparams.wear)
return itemstack
end,