ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: Dewrot on May 11, 2021, 06:53 AM

Title: Speaker wont wait and plays sound endlessly
Post by: Dewrot on May 11, 2021, 06:53 AM
I am trying to make an alarm button to base my other creations on. It has many elements that I can use as an example for my other projects...but my alarm wont function properly... It is supposed to play a sound and wait 1.05 seconds and then play it again... but it plays a new one every tick instead. I cant wrap my head around it...Ive tried sleep and alarms. What do I do?!

Modpack: All of Fabric 3

Code: https://pastebin.com/z416SvAR

Needed: Advanced Monitor
        Speaker

Monitor needs to be on top and the speaker can be anywhere.
Title: Speaker wont wait and plays sound endlessly
Post by: Lupus590 on May 11, 2021, 04:26 PM
You are not using the timer event, I would look at how the sleep (https://github.com/SquidDev-CC/CC-Tweaked/blob/mc-1.15.x/src/main/resources/data/computercraft/lua/bios.lua#L84-L90) function works.

Also, speaker.playSound has no idea how long the sound is and returns instantly, so you keep playing the sound every time you pull an event.
Title: Speaker wont wait and plays sound endlessly
Post by: Dewrot on May 11, 2021, 05:52 PM
I think one of my main issues is that I dont fully understand what I am trying to write.
Im very new to LUA (havent written CC code in YEARS), and I'm being too ambitious. I figured out that when I use sleep the program becomes unresponsive to input and and the alarm event simply doesnt work the way i thought.

Im looking over timer events and without a good example Im still a little baffled. Dont write my code for me... but perhaps an example that could help? I learn from example and I havent found anything too terribly helpful... I know i am close but not quite there.
Title: Speaker wont wait and plays sound endlessly
Post by: Dewrot on May 11, 2021, 06:17 PM
OMG I figured out a solution! Im going to polish it up a bit and reupload... Im still having an issue with my character quit event being buggy. It requires me to press it twice and if I press it once I have to do any other input twice.