# Play Sound whenever object is moved?

**URL:** <https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094>\
**Category:** Module Design\
**Created:** [December 11, 2024, 1:56pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094 "2024-12-11T13:56:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saigon](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/saigon/32/10716_2.png) [@Saigon](https://forum.vassalengine.org/u/Saigon)\
**Post date:** [December 11, 2024, 1:56pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/1 "2024-12-11T13:56:30Z")

</div>

I have a tennis ball I’m going to be moving a lot, and every time I move it I’d like it to play a _wack_ sound.  
How could I set this up?

Thanks!

---

<div class="post-metadata">

**Author:** ![DrNostromo](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/drnostromo/32/12593_2.png) [@DrNostromo](https://forum.vassalengine.org/u/DrNostromo)\
**Post date:** [December 11, 2024, 8:25pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/2 "2024-12-11T20:25:48Z")

</div>

I’m guessing this would work. On the map properties, place a keyword under “Key command to apply to all units ending movement on this map”. Like ‘MapSwitch’ or something like that.

Then place a sound trait on the ball piece that triggers on MapSwitch.

It should play the sound every time you move it.

---

<div class="post-metadata">

**Author:** ![Saigon](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/saigon/32/10716_2.png) [@Saigon](https://forum.vassalengine.org/u/Saigon)\
**Post date:** [December 15, 2024, 4:44am UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/3 "2024-12-15T04:44:14Z")

</div>

Hello, I finally had the chance to try this and I can’t follow your instructions. Can you go into more detail?

Do you mean go to MainMap/GlobalProperties?  
Or something different?

Thanks!

---

<div class="post-metadata">

**Author:** ![marktb1961](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/marktb1961/32/231_2.png) [@marktb1961](https://forum.vassalengine.org/u/marktb1961)\
**Post date:** [December 15, 2024, 6:22am UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/4 "2024-12-15T06:22:45Z")

</div>

In the editor, double click on the main map component. This will show the map properties (not to be confused with Global Properties which is a Module, Map and Zone level component).

---

<div class="post-metadata">

**Author:** ![Saigon](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/saigon/32/10716_2.png) [@Saigon](https://forum.vassalengine.org/u/Saigon)\
**Post date:** [December 15, 2024, 7:59pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/5 "2024-12-15T19:59:10Z")

</div>

That did the trick, thanks!

---

<div class="post-metadata">

**Author:** ![Saigon](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/saigon/32/10716_2.png) [@Saigon](https://forum.vassalengine.org/u/Saigon)\
**Post date:** [December 15, 2024, 8:55pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/6 "2024-12-15T20:55:56Z")

</div>

Is there a way to get it to play 1 of 5 different sounds?  
I tried sulecting 5 sounds, then adding {“Tball” + Random(5) + “.mp3”}

 ![image](https://forum.vassalengine.org/uploads/default/original/2X/3/3743066d839ca30e64aeae4e909c775890791041.png)

But it doen’t work. But if I sulect only 1 sound and put it in “sound clip” then it does work, for that one sound.

---

<div class="post-metadata">

**Author:** ![marktb1961](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/marktb1961/32/231_2.png) [@marktb1961](https://forum.vassalengine.org/u/marktb1961)\
**Post date:** [December 15, 2024, 9:41pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/7 "2024-12-15T21:41:21Z")

</div>

Paste up the screenshot of your attempt at Random that doesn’t work, please.

---

<div class="post-metadata">

**Author:** ![Saigon](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/saigon/32/10716_2.png) [@Saigon](https://forum.vassalengine.org/u/Saigon)\
**Post date:** [December 15, 2024, 9:47pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/8 "2024-12-15T21:47:58Z")

</div>

![image](https://forum.vassalengine.org/uploads/default/original/2X/d/d752944bdb61092e7e79d50a5466b7fb8ea89da4.png)

---

<div class="post-metadata">

**Author:** ![marktb1961](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/marktb1961/32/231_2.png) [@marktb1961](https://forum.vassalengine.org/u/marktb1961)\
**Post date:** [December 15, 2024, 9:54pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/9 "2024-12-15T21:54:24Z")

</div>

To judge from that graphic, you appear to have so-called “Smart Quotes” around your text literals. Make sure that you only use plain double-quotes, like this` {"Tball" + Random(5) + ".mp3"}`

If you find that copy and paste converts this example to use Smart Quotes, then I suggest that you hunt down the system setting that allows that on your machine and turn it off.

For example, on Mac, the setting is a Keyboard setting under Text Input, Text Replacements.

 ![image](https://forum.vassalengine.org/uploads/default/original/2X/3/3baf438f9cd27d1764c53f247b7173f3a4dcf206.png)

You can see in the bottom half of the panel, I have “Use smart quotes and dashes” disabled:

 ![image](https://forum.vassalengine.org/uploads/default/original/2X/3/37cba4dfa05210d94749f45b746e6f4f0c3d0608.png)

---

<div class="post-metadata">

**Author:** ![Saigon](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/saigon/32/10716_2.png) [@Saigon](https://forum.vassalengine.org/u/Saigon)\
**Post date:** [December 15, 2024, 10:04pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/10 "2024-12-15T22:04:39Z")

</div>

![image](https://forum.vassalengine.org/uploads/default/original/2X/1/1359b97d0d7c8054707f747e44cb6f110c4f5914.png)  
I changed the quotes, but nothing..  
 ![image](https://forum.vassalengine.org/uploads/default/original/2X/8/8a6e6f3cc94c03cefa6f88b1aa78b61d4cff8295.png)

---

<div class="post-metadata">

**Author:** ![jrwatts](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/jrwatts/32/6972_2.png) [@jrwatts](https://forum.vassalengine.org/u/jrwatts)\
**Post date:** [December 16, 2024, 12:11am UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/12 "2024-12-16T00:11:18Z")

</div>

While I see nothing obviously wrong with your screenshots, the fact that you’re getting an “Expression evaluation error” tells me there is definitely something wrong with your expression. Try typing the expression from scratch, rather than copying it from elsewhere. I suspect there are still some non-ASCII characters somewhere in there (those plus signs look suspiciously large to me, for example, although that might just be the font being used for your chat window).

---

<div class="post-metadata">

**Author:** ![Saigon](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/saigon/32/10716_2.png) [@Saigon](https://forum.vassalengine.org/u/Saigon)\
**Post date:** [December 16, 2024, 12:57am UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/13 "2024-12-16T00:57:40Z")

</div>

Can’t get it to work I’m afraid.

---

<div class="post-metadata">

**Author:** ![marktb1961](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/marktb1961/32/231_2.png) [@marktb1961](https://forum.vassalengine.org/u/marktb1961)\
**Post date:** [December 16, 2024, 10:32am UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/14 "2024-12-16T10:32:50Z")

</div>

Sorry, I’m stumped. A very similar expression works fine when I test it on an [Action Button](https://vassalengine.org/doc/latest/ReferenceManual/DoActionButton.html#top). To rule out some other possible causes, can you get your exact same expression to work on an Action Button ?

---

<div class="post-metadata">

**Author:** ![Saigon](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/saigon/32/10716_2.png) [@Saigon](https://forum.vassalengine.org/u/Saigon)\
**Post date:** [December 16, 2024, 1:44pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/15 "2024-12-16T13:44:51Z")

</div>

I’ve come to the conclusion that this can’t be done. Only one sound can be played with what we’re doing and the way we’re doing it.

I can get it to work with multiple sounds with a button, but doing it the same way I can’t get it to work with this moving object.

---

<div class="post-metadata">

**Author:** ![marktb1961](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/marktb1961/32/231_2.png) [@marktb1961](https://forum.vassalengine.org/u/marktb1961)\
**Post date:** [December 16, 2024, 2:19pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/16 "2024-12-16T14:19:12Z")

</div>

I think it should work. However, you could instead use the [Global Hotkey](https://vassalengine.org/doc/latest/ReferenceManual/GlobalHotKey.html#top) trait from the tennis ball piece to trigger the Action Button to make the sounds.

---

<div class="post-metadata">

**Author:** ![shilinski](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/shilinski/32/6980_2.png) [@shilinski](https://forum.vassalengine.org/u/shilinski)\
**Post date:** [December 17, 2024, 5:43am UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/17 "2024-12-17T05:43:40Z")

</div>

Are the five mp3 files actually inside the module? They will be inside an audio folder. ( I think.) had a similar problem, and I had to copy my mp3 into it via 7zip.

---

<div class="post-metadata">

**Author:** ![shilinski](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/shilinski/32/6980_2.png) [@shilinski](https://forum.vassalengine.org/u/shilinski)\
**Post date:** [December 17, 2024, 12:54pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/18 "2024-12-17T12:54:46Z")

</div>

The more I think about it, the more I believe I’m right. I bet your vmod doesn’t include the mp3 files you need. Open your vmod with something like 7zip, and you should see a folder called “sound” (or “sounds”, (I don’t remember which) at the images level. If not there, create it. Then copy into it the six mp3 files you need.

I noticed when I looked at your error message that it first told you about a syntax error. I think that was a red herring because the very next line said “file not found,” and there it is.

And it makes sense if you think about it. When you specify an image file, you must explicitly state the file name, and there’s no fooling around with properties or file name construction. The editor can easily grab that image file. But with sounds it a different story. There is no way in hell the editor can know from “splat”+random(100)+”.mp3” what it should do. All it can do is throw up its hands.

---

<div class="post-metadata">

**Author:** ![Saigon](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/saigon/32/10716_2.png) [@Saigon](https://forum.vassalengine.org/u/Saigon)\
**Post date:** [December 22, 2024, 11:05pm UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/19 "2024-12-22T23:05:36Z")

</div>

Sorry for the late reply.  
How would I add 5 different sounds with the same file name?  
[I found the folder and the sounds weren’t in there.]

---

<div class="post-metadata">

**Author:** ![JoelCFC25](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/joelcfc25/32/2687_2.png) [@JoelCFC25](https://forum.vassalengine.org/u/JoelCFC25)\
**Post date:** [December 23, 2024, 1:14am UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/20 "2024-12-23T01:14:30Z")

</div>

> [@Saigon](#):
>
> How would I add 5 different sounds with the same file name?

You can’t! But if you have five distinct soundfiles named:

`Tball1.mp3`  
`Tball2.mp3`  
`Tball3.mp3`  
`Tball4.mp3`  
`Tball5.mp3`

…and you use the formulation @marktb1961 gave you a week ago: `{"Tball" + Random(5) + ".mp3"}`

That’s how it’s going to result in playing a random choice of those 5 files.

---

<div class="post-metadata">

**Author:** ![marktb1961](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/marktb1961/32/231_2.png) [@marktb1961](https://forum.vassalengine.org/u/marktb1961)\
**Post date:** [December 23, 2024, 6:18am UTC](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094/21 "2024-12-23T06:18:58Z")

</div>

> [@Saigon](#):
>
> How would I add 5 different sounds with the same file name?

You can add those files using the Sound trait’s “Select” button. Do that for each file, then replace the last entry with the aforemantioned random expression. The files remain in the module, even when no longer referenced directly by the trait.

[Next page](https://forum.vassalengine.org/t/play-sound-whenever-object-is-moved/84094.md?page=2)
