class_name AudioStreamPlayer_StateReceiver extends AudioStreamPlayer export var debug_component: bool = false export var callable_state_machine :NodePath var request_state_change: FuncRef export(Array, Resource) var sound_effects onready var current_state :StateAnimatedActor # Declare member variables here. Examples: # var a = 2 # var b = "text" var sound_effects_dict: Dictionary #class_name FrameSoundEffects #extends Resource # # #export (String) var animation_name #export(int) var frame_number #export (AudioStreamSample) var sound # Called when the node enters the scene tree for the first time. func _ready(): # Trying something new with a custom resourse. for i in sound_effects: if i is SE_StateFrame: sound_effects_dict[i.state_name + str(i.frame_number)] = i if debug_component: print (sound_effects_dict) request_state_change = funcref(get_node(callable_state_machine), 'change_to_known_state') ## Connect signal to get alerted of state change get_node(callable_state_machine).connect("state_changed", self,"_on_state_change") ## A reference to the current state machine state current_state = get_node(callable_state_machine).current_state # Experimenting with sound based resources I did this and it worked #sound_effect_player.stream = sound_effects.sounds["pew"] #print(sounds.sounds["pew"]) #sound_effects.play() #var last_sound_frame_animation: String = '' #var last_sound_frame: int = -1 #func play_sound_frame(animation: String, frame: int ): # ##DEBUG: # #if animation == 'run' and frame == 6: # # print("Do the thign! (", animation + str(frame), ")", " (", last_sound_frame_animation + str(last_sound_frame), ")") # if animation != last_sound_frame_animation or frame != last_sound_frame: # last_sound_frame = frame # last_sound_frame_animation = animation # var sound_index = animation + str(frame) # if sound_effects_dict.has(sound_index): # stream = sound_effects_dict[sound_index] # play() # print(self.name, " Playing SE: ", sound_index) var current_se_index :String # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): var sound_index = current_state.name + str(current_state.animation_frame) if current_se_index != sound_index and sound_effects_dict.has(sound_index): if debug_component: print (get_path()) print("\tPlay sound: ", current_state.name, current_state.animation_frame) current_se_index = sound_index ##TODO: Better to do an is check here or a cast ## We already assure these are SE_StateFrames var se_resource :SE_StateFrame = sound_effects_dict[sound_index] stream = se_resource.sound #var this_sound :AudioStreamSample play() # Not sure what should be called first here. if has_method('_state_process_' + current_state.name): call('_state_process_' + current_state.name) func _on_state_change(old_state_name:String, new_state :State): ###### State machine if new_state is StateAnimatedActor: #Testing this. Update: It works current_state = new_state if has_method('_on_state_change_' + current_state.name): call('_on_state_change_' + current_state.name) else: push_warning("Received non animated Actor state.") #current_state = new_state