Changing on_use for a tool

User avatar
sirken
Member
 
Posts: 15
Joined: Thu Oct 09, 2014 15:18
GitHub: sirken

Changing on_use for a tool

by sirken » Tue Nov 11, 2014 21:32

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:

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?
Last edited by sirken on Thu Nov 13, 2014 06:53, edited 1 time in total.
 

User avatar
HeroOfTheWinds
Member
 
Posts: 470
Joined: Wed Apr 23, 2014 23:16
Location: Hawaii
GitHub: HeroOfTheWinds
IRC: WindHero

Re: Changing on_use for a tool

by HeroOfTheWinds » Tue Nov 11, 2014 22:45

Hmm, it may not be the same, but try using:
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.
Nam ex spatio, omnes res venire possunt.
Why let the ground limit you when you can reach for the sky?
Back to college now, yay for sophomore year schedules. :P
 

User avatar
Krock
Developer
 
Posts: 4346
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: Changing on_use for a tool

by Krock » Wed Nov 12, 2014 18:36

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.
>> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
 

4aiman
Member
 
Posts: 1208
Joined: Mon Jul 30, 2012 05:47

Re: Changing on_use for a tool

by 4aiman » Thu Nov 13, 2014 03:57

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.
 

User avatar
sirken
Member
 
Posts: 15
Joined: Thu Oct 09, 2014 15:18
GitHub: sirken

Re: Changing on_use for a tool

by sirken » Thu Nov 13, 2014 07:48

HeroOfTheWinds wrote:Hmm, it may not be the same, but try using:
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.

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,
   ...
})
 

4aiman
Member
 
Posts: 1208
Joined: Mon Jul 30, 2012 05:47

Re: Changing on_use for a tool

by 4aiman » Thu Nov 13, 2014 09:39

You are to put sound_play into AFTER_use and DO NOT use on_use :)
 

User avatar
sirken
Member
 
Posts: 15
Joined: Thu Oct 09, 2014 15:18
GitHub: sirken

Re: Changing on_use for a tool

by sirken » Thu Nov 13, 2014 15:15

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?
 

4aiman
Member
 
Posts: 1208
Joined: Mon Jul 30, 2012 05:47

Re: Changing on_use for a tool

by 4aiman » Thu Nov 13, 2014 16:04

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.
 

User avatar
sirken
Member
 
Posts: 15
Joined: Thu Oct 09, 2014 15:18
GitHub: sirken

Re: Changing on_use for a tool

by sirken » Sun Nov 23, 2014 04:57

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.
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:

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,
 

User avatar
sirken
Member
 
Posts: 15
Joined: Thu Oct 09, 2014 15:18
GitHub: sirken

Re: Changing on_use for a tool

by sirken » Fri Nov 28, 2014 00:53

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
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 6 guests