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.

Adding a Listener to the Scene Tree

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:

Screenshot 2023-08-30 at 16 32 57

Posting Events with the AkEvent Nodes

To play events using the AkEvent node, follow these steps:

  1. Add an AkEvent3D or AkEvent2D node to the scene tree:

Screenshot 2023-08-30 at 16 39 03

2. Click on the AkEvent node to access various settings in the inspector that can influence event playback:



wwise-godot-ak-event-inspector

  • 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.

Using Callbacks with the AkEvent Node

Callbacks in AkEvent nodes are exposed as Signals. To connect a Signal to a receiving node, follow these steps:

  1. Identify the desired Signal/callback in the AkEvent Node and double click on it to open the signal connection window:

wwise-godot-ak-event-callbacks

  1. Connect the signal to a receiving node that has a script attached:



wwise-godot-ak-event-signal-connection

  1. Godot will automatically create a function in the receiving node's script:
wwise-godot-ak-event-generated-callback-function

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 } }

Posting Events with GDScript

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.

Posting Events with the WwiseEvent type

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)

Posting Events with callbacks using the WwiseEvent Type

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.

Posting Events using the post_event functions in the Wwise singleton:

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.

Posting MIDI using the WwiseEvent type:

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.

Handling Errors

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.

⚠️ **GitHub.com Fallback** ⚠️