Changing on_use for a tool

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

Changing on_use for a tool

by sirken » Post

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
GitHub: HeroOfTheWinds
IRC: WindHero
Location: Hawaii

Re: Changing on_use for a tool

by HeroOfTheWinds » Post

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: 4649
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: Changing on_use for a tool

by Krock » Post

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.
Look, I programmed a bug for you. >> 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 » Post

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: 16
Joined: Thu Oct 09, 2014 15:18
GitHub: sirken

Re: Changing on_use for a tool

by sirken » Post

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 » Post

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

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

Re: Changing on_use for a tool

by sirken » Post

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 » Post

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: 16
Joined: Thu Oct 09, 2014 15:18
GitHub: sirken

Re: Changing on_use for a tool

by sirken » Post

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: 16
Joined: Thu Oct 09, 2014 15:18
GitHub: sirken

Re: Changing on_use for a tool

by sirken » Post

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

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests