What is the data type of the AudioBus and can I export it as a variable?

Godot Version

4.2

Question

I have a scene called AudioTrigger, when the player walks in this box, it plays a sound. It’s just a simple colision box that has an AudioStreamPlayer in it. I like to have everything organized in one place so I want to add all the variables that need to be changed frequently due to experimentation in the top node and then pass them to the child nodes via code. One thing that I need to change via the top node is the AudioBus of the AudioStreamPlayer. Now, I know how to set the bus, but how do I export a variable of type “AudioBus”. Can it be done? I would like to have something like a drop down menu like in AudioStreamPlayer or an empty spot to drag-n-drop the bus into, but I can’t find it anywhere online. Does someone know if this can be done or how to do it? Thank you very much!

Here is a screenshot of what I need:
grafik

Something similar to this.

Cheers,
Benny

Do you have tried this:

@export var bus: AudioBus

Yes, I have, but it says Could not find type "AudioBus" in the current scope.

You need to use StringName but the buses are not available as a hint, instead you need to get the buses from the AudioServer

See this code for how it’s done in the engine:

And the variable would be exported as an enum, and using _validate_property

1 Like

I’ve tried to do this but I just can’t get it to work. Am I doing something wrog?

I’ve just copy-pasted the code from the documentation:

extends Node

@export var is_number_editable: bool:
    set(value):
        is_number_editable = value
        notify_property_list_changed()
@export var number: int

func _validate_property(property: Dictionary):
    if property.name == "number" and not is_number_editable:
        property.usage |= PROPERTY_USAGE_READ_ONLY

but when I save the file and then tick and untick the is_number_editable property in the inspector, no matter the state of the boolean I can still change the value of number. Am I doing something wrong?

That’s not the code I mean, try basing it on the code in c++ not the example in _validate_property

1 Like

Ok, the answer from @athousandships was the solution. I just needed to reload the project

1 Like

After a LOT of fiddling I finally came up with a solution. This is the code for anyone in the future:

enum AudioBus {}
@export var audio_bus: AudioBus

func _validate_property(property: Dictionary):
	if property.name == "audio_bus":
		var busNumber = AudioServer.bus_count
		var options = ""
		for i in busNumber:
			if i > 0:
				options += ","
			var busName = AudioServer.get_bus_name(i)
			options += busName
		property.hint_string = options

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.