Page 1 of 1

Changing on_use for a tool

PostPosted: Tue Nov 11, 2014 21:32
by sirken
I came across this old laser sword mod: viewtopic.php?id=2632

... and wanted to add a laser sound when the weapon is used. That is now working with the following code:

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
minetest.register_tool("laser_mod:red_sword", {
   ...
   on_use = function()
      minetest.sound_play("laser_mod")
   end,
   ...
})


However, the the sword no longer hits anything else. So, I'm assuming that by changing the on_use function I have replaced the default behaviour. How do I retain the default behaviour and use the sound in addition?

Re: Changing on_use for a tool

PostPosted: Tue Nov 11, 2014 22:45
by HeroOfTheWinds
Hmm, it may not be the same, but try using:
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)


If the problem persists, you must handle the punching code manually by doing operations on the pointed_thing passed by the function.

Re: Changing on_use for a tool

PostPosted: Wed Nov 12, 2014 18:36
by Krock
on_use overrides the normal function of tools. Use a craftitem and add the wear manually or use (as above said) the after_use function.

Re: Changing on_use for a tool

PostPosted: Thu Nov 13, 2014 03:57
by 4aiman
Krock wrote:on_use overrides the normal function of tools. Use a craftitem and add the wear manually or use (as above said) the after_use function.

If a craftitem is to be used then it'll be very tricky to get the damage amount if there are more damage groups than just 'fleshy'...
So, after use is better than nothing in terms of ease.

Re: Changing on_use for a tool

PostPosted: Thu Nov 13, 2014 07:48
by sirken
HeroOfTheWinds wrote:Hmm, it may not be the same, but try using:
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)


If the problem persists, you must handle the punching code manually by doing operations on the pointed_thing passed by the function.


I've been looking through other lua examples, trying to understand how to use the function but not quite sure how to implement it. I've been doing lots of trial and error, but I have been unable to get it to work. Do you have an example? Or can point me to documentation? Thanks.

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
minetest.register_tool("laser_mod:red_sword", {
   ...
   on_use = function()
      minetest.sound_play("laser_mod")
   end,
   after_use = function(itemstack, user, node, digparams)
      what to do here?
   end,
   ...
})

Re: Changing on_use for a tool

PostPosted: Thu Nov 13, 2014 09:39
by 4aiman
You are to put sound_play into AFTER_use and DO NOT use on_use :)

Re: Changing on_use for a tool

PostPosted: Thu Nov 13, 2014 15:15
by sirken
4aiman wrote:You are to put sound_play into AFTER_use and DO NOT use on_use :)

Ok, I tried this, and it works! The only downside is that the sound doesn't play if you punch nothing. Is there a way to make the sound play on every swing?

Re: Changing on_use for a tool

PostPosted: Thu Nov 13, 2014 16:04
by 4aiman
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.

Re: Changing on_use for a tool

PostPosted: Sun Nov 23, 2014 04:57
by sirken
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,

Re: Changing on_use for a tool

PostPosted: Fri Nov 28, 2014 00:53
by sirken
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.


It is now working using get_player_controls(). Thanks everyone for your input. Mod here: viewtopic.php?f=9&t=10642