Help with creating loop procedure

Started by Wolfboss356 on

Topic category: Help with Minecraft modding (Java Edition)

Joined Jan 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Help with creating loop procedure

I am trying to make a procedure that will (when an entity is spawned) play a song on loop UNTIL the entity dies. I just put the procedure into "on entity initial spawn," so I don't need help with that, but when I tried my hand at making the rest, here is what I put: (trigger) play sound at xyz, repeat 100000 times:  wait 1200 ticks: if: (not) is entity alive do: break out of loop else: play sound at xyz. And other than just ASSUMING I did something wrong, I also got the "your workspace contains mod elements that don't compile properly" error. Can anyone tell me how to make what I'm trying to do?

Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This should... technically…
Thu, 01/25/2024 - 13:27

This should... technically work, but it's a very, very inefficent way to do this. Essentially, the game is going to wait exactly 1200 ticks after the entity spawns, play the song 100000 times, (all at once), and then never plays it again. (The wait function also won't work if you unload the area, or if you save and reenter the game.)

What you want to do is, when your entity spawns, give it a custom NBT number tag, (I'll call it 'song_duration'), and set it to zero. Then, make a procedure that triggers on entity update tick. This procedure should check if the entity's NBT tag is less than or equal to zero, and if it does, should set the tag to 1200, (or however long the music is in ticks), and play the song. If the tag is greater than zero, you should decrease the tag by one.

This will essentially create a timer for your entity that plays the song every 1200 ticks. You will have to make the music fairly short, however, as sounds can only be played at a single position. (So if your entity starts to move around a lot, the song will continue to play at the position it initially started.) There may be a way around this, but I'm not sure how you'd do it.