Posting Events (24.1) - alessandrofama/wwise-godot-integration GitHub Wiki
We'll explore two methods for posting Wwise Events in Godot: using the AkEvent
nodes provided by the integration and by writing GDScript code.
Before posting any events, it's essential to incorporate a listener into the scene. Achieve this by integrating an AkListener3D
or AkListener2D
node within the Scene Tree:
To play events using the AkEvent node, follow these steps:
- Add an AkEvent3D or AkEvent2D node to the scene tree:
2. Click on the AkEvent node to access various settings in the inspector that can influence event playback:
- Event: Select the desired Event from a list of available Events.
- Trigger On and Stop On: Choose when the Event should be posted and stopped (e.g., Enter Tree, Ready, Exit Tree).
- Stop Fade Time: Set the duration in milliseconds for the event fade-out when stopped.
- Interpolation Mode: Define the curve used for the fade-out.
- Is Environment Aware: Activate this option if the event should be assigned to the specified aux bus when the corresponding AkEnvironment node is active.
Select the desired Event, Trigger Onn Stop On callbacks, and try posting an Event by playing the scene in the editor. Don't forget to add an AkListener node to the tree.
Callbacks in AkEvent nodes are exposed as Signals. To connect a Signal to a receiving node, follow these steps:
- Identify the desired Signal/callback in the AkEvent Node and double click on it to open the signal connection window:
- Connect the signal to a receiving node that has a script attached:
- Godot will automatically create a function in the receiving node's script:
In this example, the dictionary containing information about the callback is printed. The output will look like this:
{ "callback_type": 256, "gameObjID": 26726106446, "musicSyncType": 256, "playingID": 1, "pszUserCueName": "", "segmentInfo": { "fBarDuration": 2, "fBeatDuration": 0.5, "fGridDuration": 8, "fGridOffset": 0, "iActiveDuration": 1000, "iCurrentPosition": 0, "iPostExitDuration": 8769, "iPreEntryDuration": 0, "iRemainingLookAheadTime": 0 } }
To post events using GDScript, you can use the WwiseEvent
type in a script, or call directly the exposed post_event
function in the Wwise singleton
.
Export a WwiseEvent
variable in a script. A new button will appear in the inspector, allowing you to select an Event from the Wwise Picker:
You can post this Event by calling post
on the exported WwiseEvent
variable:
extends Node3D
@export var event:WwiseEvent
func _enter_tree() -> void:
event.post(self)
post
expects a game_object
argument. GameObjects are automatically registered when you post an Event. Choosing what to define as a GameObject is your own choice, but remember that the Event will update its position based on the assigned game_objects
's position.
You can stop the Event by posting an additional Event that has a Stop
action assigned, or for convenience, call stop
on the exported WwiseEvent
:
extends Node3D
@export var event:WwiseEvent
func _enter_tree() -> void:
event.post(self)
await get_tree().create_timer(3).timeout
event.stop(self, 2000, AkUtils.AK_CURVE_LINEAR)
Similar to posting Events without callbacks, you can call post_callback
on an exported WwiseEvent
variable:
extends Node3D
@export var event:WwiseEvent
func _enter_tree() -> void:
event.post_callback(self, AkUtils.AK_END_OF_EVENT, _on_end_of_event)
func _on_end_of_event(data):
print("Event ended!")
post_callback
expects a game_object
, the desired callback flag, and a Godot Callable
as arguments.
Alternatively, you can call post_event
or post_event_id
on the Wwise
singleton to post an Event. Pass the name or ID of the event and the game object on which the event should be posted.
Example using post_event
:
func _ready():
Wwise.post_event("Music", self)
Example using post_event_id
:
func _ready():
Wwise.post_event_id(AK.EVENTS.MUSIC, self)
It is important to remember here that this will only work for Events assigned to User-Defined SoundBanks. Use the WwiseEvent
type when using Auto-Defined SoundBanks.
Is it possible to post MIDI Events on an Event by calling the post_midi
function on an exported WwiseEvent
type:
extends Node
@export var event:WwiseEvent
func _ready() -> void:
var array:Array[AkMidiPost]
var m1:AkMidiPost = AkMidiPost.new()
m1.by_type = AkMidiPost.MIDI_EVENT_TYPE_NOTE_ON
m1.by_note = 42
m1.by_velocity = 127
m1.u_offset = 0
array.push_back(m1)
event.post_midi(self, array)
Call stop_midi
on the WwiseEvent
to stop MIDI Events associated with a GameObject.
If an error occurs while posting an event, Godot will print an error message in the Errors
dock, providing information about the nature of the error. Here is an example of such an error message:
WwiseGodot: Post Event failed. This Event is in a User Defined Soundbank. Make sure to add an AkBank Node to the Scene Tree and load the corresponding SoundBank before posting the Event.
By examining this error message, you can gain insight into the specific issue that occurred.