How to add sound to node?

How to add sound to node?

Postby gigomaf » Fri Apr 12, 2019 6:00 pm

I try to make a mod with few decoration blocks, but I dont know how to add sounds and stop them.
Can someone help me with code below?

This is simple decorative 'fireplace' block. I tried to add sound into 'right click' function. It's working but only when I trigger funcion by right click. If I restart the game, the sound isn't playing anymore.
I tried to study original 'fire' mod from Minitest game but it's a bit complicated.
Code: Select all

minetest.register_node("my_mod:fireplace", {
   description = "Fireplace",
   tiles = {"fireplace_top.png", "fireplace_top.png", "fireplace_side.png", "fireplace_top.png", "fireplace_top.png", "fireplace_front.png"},
   groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},

   on_rightclick = function(pos,node, player, itemstack, pointed_thing)
      minetest.swap_node(pos, {name="my_mod:fireplace_active"})
   end
})

minetest.register_node("my_mod:fireplace_active", {
   description = "Fireplace",
   tiles = {
   "fireplace_top.png", "fireplace_top.png", "fireplace_side.png", "fireplace_top.png", "fireplace_top.png",
      {
         image = "fireplace_active.png",
--         backface_culling = false,
         animation = {
            type = "vertical_frames",
            aspect_w = 16,
            aspect_h = 16,
            length = 1.5
         },
      }
   },
   groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
   light_source = 8,
   minetest.sound_play("fire",
      {pos = pos, max_hear_distance = 16, gain = 1, loop = true}),
   on_rightclick = function(pos,node, player, itemstack, pointed_thing)
      minetest.swap_node(pos, {name="my_mod:fireplace"})
   end
})

minetest.register_craft({
   output = 'my_mod:fireplace',
   recipe = {
      {'default:wood', 'default:wood', 'default:wood'},
      {'default:stone', 'default:coalblock', 'default:stone'},
      {'default:stone', 'default:stone', 'default:stone'},
   }
})

Image
gigomaf
Member
 
Posts: 25
Joined: Sat Mar 02, 2019 2:57 pm

Re: How to add sound to node?

Postby texmex » Fri Apr 12, 2019 6:15 pm

Yeah, I too am interested in a best practice to solve this. Probably a node times needs to be involved.
texmex
Member
 
Posts: 1290
Joined: Mon Jul 11, 2016 9:08 pm
GitHub: tacotexmex
In-game: texmex

Re: How to add sound to node?

Postby gigomaf » Fri Apr 12, 2019 6:23 pm

Btw. This is my first steps at modding. This example contains two nodes for different states (active and inactive). I assume this should be done by register only one node, like original chest or furnance, but I don't know how to do it so far ;)
gigomaf
Member
 
Posts: 25
Joined: Sat Mar 02, 2019 2:57 pm

Re: How to add sound to node?

Postby texmex » Fri Apr 12, 2019 6:51 pm

Nope, default furnace registers two nodes as well. :)
texmex
Member
 
Posts: 1290
Joined: Mon Jul 11, 2016 9:08 pm
GitHub: tacotexmex
In-game: texmex

Re: How to add sound to node?

Postby Astrobe » Sat Apr 13, 2019 7:12 am

In the "fire mod", what you're interested in is what is in the "if flame_sound then ..." clause. Extract it, replace the node names with your node name, and it should do the trick.
Astrobe
Member
 
Posts: 221
Joined: Sun Apr 01, 2018 10:46 am

Re: How to add sound to node?

Postby gigomaf » Sat Apr 13, 2019 9:54 am

Thanks for the tips :)
In meantime I have another problem. I set paramtype 2 = "facedir" on first node, but I don't know how to set the same param on second node. After right click, the front faces of my fireplace are setted in wrong direction.
gigomaf
Member
 
Posts: 25
Joined: Sat Mar 02, 2019 2:57 pm

Re: How to add sound to node?

Postby texmex » Sat Apr 13, 2019 10:24 am

Astrobe wrote:In the "fire mod", what you're interested in is what is in the "if flame_sound then ..." clause. Extract it, replace the node names with your node name, and it should do the trick.

I’m not sure the same solution applies here. The fire mod sound playing function is oriented around the player, not around the fire node(s) themselves. This is in order to not play 200 fire sound sources when fire spreads. This fireplace node doesn’t face the same complexity it seems, so solution should be much simpler.
texmex
Member
 
Posts: 1290
Joined: Mon Jul 11, 2016 9:08 pm
GitHub: tacotexmex
In-game: texmex

Re: How to add sound to node?

Postby gigomaf » Sun Apr 14, 2019 10:21 am

Update:
Code: Select all
minetest.register_node("my_mod:fireplace", {
   description = "Fireplace",
   tiles = {"fireplace_top.png", "fireplace_top.png", "fireplace_side.png", "fireplace_top.png", "fireplace_top.png", "fireplace_front.png"},
   groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
   paramtype2 = "facedir",
   on_rightclick = function(pos, node, clicker, itemstack)
      node.name = "my_mod:fireplace_active"
      minetest.set_node(pos, node)
--      return itemstack
      handle = minetest.sound_play("fire3", {
      pos = pos,
      max_hear_distance = 10,
      gain = 1,
      loop = true,
   })
   end
})

minetest.register_node("my_mod:fireplace_active", {
   --description = "Fireplace",
   tiles = {
   "fireplace_top.png", "fireplace_top.png", "fireplace_side.png", "fireplace_top.png", "fireplace_top.png",
      {
         image = "fireplace_active2.png",
         animation = {
            type = "vertical_frames",
            aspect_w = 16,
            aspect_h = 16,
            length = 1.5
         },
      }
   },
   groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
   paramtype2 = "facedir",
   light_source = 8,
   on_rightclick = function(pos, node, clicker, itemstack)
      node.name = "my_mod:fireplace"
      minetest.set_node(pos, node)
--      return itemstack
      minetest.sound_stop(handle)
   end
})



Sound is playing but there are two problems:

1) minetest.sound_stop(handle) will crash the game if sound_play has not been triggered.
I don't know how to check if sound is playing or not.

2) problems with minetest.sound_stop(handle) if more than one nodes is placed.
gigomaf
Member
 
Posts: 25
Joined: Sat Mar 02, 2019 2:57 pm

Re: How to add sound to node?

Postby Astrobe » Mon Apr 15, 2019 1:29 pm

I think this can be solved by maintaining an activation count. For instance:

Create a counter as a local variable (probably like you did for "handle") at the beginning of the file.

Add one to it each time you start to play a sound.

When you are about to stop a sound from playing, check first that the counter is not zero. If this is the case, stop the sound and subtract one to the counter.

Otherwise, do nothing. Normally, it shouldn't happen if the rest of your mod is correct. Perhaps you ran into the issue because you were able to pick an active fireplace? In this case make it drop an inactive one with drop="my_mod:fireplace". The check is worth having anyway that there are probably other corner cases.
Astrobe
Member
 
Posts: 221
Joined: Sun Apr 01, 2018 10:46 am



Return to Modding Discussion



Who is online

Users browsing this forum: Yandex Bot [Bot] and 0 guests