Advertisement
NoneApplicableGames

MusicEvent

Aug 3rd, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @tool
  2. ## Event that can change the currently playing background music.
  3. ## This event won't play new music if it's already playing.
  4. class_name DialogicMusicEvent
  5. extends DialogicEvent
  6.  
  7.  
  8. ### Settings
  9.  
  10. ## The file to play. If empty, the previous music will be faded out.
  11. var file_path: String = ""
  12. ## The length of the fade. If 0 (by default) it's an instant change.
  13. var fade_length: float = 0
  14. ## The volume the music will be played at.
  15. var volume: float = 0
  16. ## The audio bus the music will be played at.
  17. var audio_bus: String = ""
  18. ## If true, the audio will loop, otherwise only play once.
  19. var loop: bool = true
  20.  
  21.  
  22. ################################################################################
  23. ##                      EXECUTE
  24. ################################################################################
  25.  
  26. func _execute() -> void:
  27.     if not dialogic.Audio.is_music_playing_resource(file_path):
  28.         dialogic.Audio.update_music(file_path, volume, audio_bus, fade_length, loop)
  29.  
  30.     finish()
  31.  
  32. ################################################################################
  33. ##                      INITIALIZE
  34. ################################################################################
  35.  
  36. func _init() -> void:
  37.     event_name = "Music"
  38.     set_default_color('Color7')
  39.     event_category = "Audio"
  40.     event_sorting_index = 2
  41.  
  42.  
  43. func _get_icon() -> Resource:
  44.     return load(self.get_script().get_path().get_base_dir().path_join('icon_music.png'))
  45.  
  46. ################################################################################
  47. ##                      SAVING/LOADING
  48. ################################################################################
  49.  
  50. func get_shortcode() -> String:
  51.     return "music"
  52.  
  53.  
  54. func get_shortcode_parameters() -> Dictionary:
  55.     return {
  56.         #param_name : property_info
  57.         "path"      : {"property": "file_path",     "default": ""},
  58.         "fade"      : {"property": "fade_length",   "default": 0},
  59.         "volume"    : {"property": "volume",        "default": 0},
  60.         "bus"       : {"property": "audio_bus",     "default": "",
  61.                         "suggestions": get_bus_suggestions},
  62.         "loop"      : {"property": "loop",          "default": true},
  63.     }
  64.  
  65.  
  66. ################################################################################
  67. ##                      EDITOR REPRESENTATION
  68. ################################################################################
  69.  
  70. func build_event_editor() -> void:
  71.     add_header_edit('file_path', ValueType.FILE, {
  72.             'left_text'     : 'Play',
  73.             'file_filter'   : "*.mp3, *.ogg, *.wav; Supported Audio Files",
  74.             'placeholder'   : "No music",
  75.             'editor_icon'   : ["AudioStreamPlayer", "EditorIcons"]})
  76.     add_body_edit('fade_length', ValueType.NUMBER, {'left_text':'Fade Time:'})
  77.     add_body_edit('volume', ValueType.NUMBER, {'left_text':'Volume:', 'mode':2}, '!file_path.is_empty()')
  78.     add_body_edit('audio_bus', ValueType.SINGLELINE_TEXT, {'left_text':'Audio Bus:'}, '!file_path.is_empty()')
  79.     add_body_edit('loop', ValueType.BOOL, {'left_text':'Loop:'}, '!file_path.is_empty() and not file_path.to_lower().ends_with(".wav")')
  80.  
  81.  
  82. func get_bus_suggestions() -> Dictionary:
  83.     var bus_name_list := {}
  84.     for i in range(AudioServer.bus_count):
  85.         bus_name_list[AudioServer.get_bus_name(i)] = {'value':AudioServer.get_bus_name(i)}
  86.     return bus_name_list
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement