Hi there, the script wasn't really working on my end, played the default SE normally, but stopped playing after trying to change the SE with DialogueSound.set_sound_effect("SE_NAME"), even if trying DialogueSound.set_sound_effect("boopSINE") made it stop playing.
I have revised the code and rewritten some stuff and got it working on my end, happy to share my findings here.
#===============================================================================
# A script that plays a Sound Effect (SE) for each letter in a message,
# similar to dialogue boxes in games like Undertale.
#
# Usage:
# DialogueSound.set_sound_effect("SE_NAME") # Use this to change the sound.
# DialogueSound.enable_sound # Enables the typing sound.
# DialogueSound.disable_sound # Disables the typing sound.
#===============================================================================
module DialogueSound
# --- Configuration ---
@sound_effect_name = "boopSINE" # Default SE file name (must exist in Audio/SE).
@sound_enabled = true
# --- Internal State ---
@sound_interval = 2
@letter_count = 0
@previous_position = 0
# Sets the interval based on player's text speed
def self.set_sound_interval
case $PokemonSystem.textspeed
when 0 then @sound_interval = 2 # Slow
when 1 then @sound_interval = 3 # Normal
when 2 then @sound_interval = 5 # Fast
else @sound_interval = 2
end
end
# Resets counters for a new message
def self.reset
@letter_count = 0
@previous_position = 0
set_sound_interval
end
# Plays the sound effect if conditions are met
def self.play_sound_effect(current_position, char)
return unless @sound_enabled
# Ignore spaces, blank characters, and control characters
return if char.strip.empty? || char == "\\" || char == "[" || char == "]"
if current_position > @previous_position
if @letter_count % @sound_interval == 0
if @sound_effect_name && !@sound_effect_name.empty?
# Play the sound with default volume (100) and pitch (100)
pbSEPlay(@sound_effect_name, 100, 100)
end
end
@letter_count += 1
@previous_position = current_position
end
end
# --- Script Commands ---
# Sets the sound file name and ensures the input string is clean.
def self.set_sound_effect(name)
# CRITICAL FIX: Use gsub to remove ALL forms of whitespace
# (spaces, tabs, newlines) from the input string.
cleaned_name = name.to_s.gsub(/[\s\n\r\t]/, "")
@sound_effect_name = cleaned_name
end
def self.enable_sound
@sound_enabled = true
end
def self.disable_sound
@sound_enabled = false
end
end
#===============================================================================
# Override for pbMessageDisplay (Hooks into the core message function)
#===============================================================================
alias original_pbMessageDisplay pbMessageDisplay
def pbMessageDisplay(msgwindow, message, letterbyletter = true, commandProc = nil)
DialogueSound.reset
original_pbMessageDisplay(msgwindow, message, letterbyletter, commandProc) do
if letterbyletter && msgwindow && msgwindow.waitcount == 0 && msgwindow.text
current_position = msgwindow.position
current_char = msgwindow.text[current_position] || ""
DialogueSound.play_sound_effect(current_position, current_char)
end
yield if block_given?
end
end