alessandrofama / wwise-godot-integration

Wwise Integration for the Godot Engine
Other
288 stars 14 forks source link

Simplify Callbacks creation and processing by using Godot's FuncRef #41

Closed alessandrofama closed 2 years ago

alessandrofama commented 2 years ago

This PR aims to simplify the usage of Wwise callbacks in Godot. It removes the signal processing for callbacks from the C++ codebase and implements FuncRefs as callbacks cookies.

With FuncRefs we can create a reference to a function in a given object and pass the reference around, i.e. in this implementation as a cookie to the respective Wwise callbacks. We then can call the referenced function directly from the Wwise callbacks, bypassing the signal processing in _process().

post_event_callback, post_event_id_callback, load_bank_async, load_bank_async_id, unload_bank_async and unload_bank_async_id now require an Object argument, which should be a FuncRef created in GDScript.

Event callback example:

export(AK.EVENTS._enum) var event = AK.EVENTS.MUSIC
export(AkUtils.AkCallbackType) var callback_type = AkUtils.AkCallbackType.AK_MusicSyncBeat

var cookie:FuncRef

func _ready():
    var registerResult = Wwise.register_game_obj(self, "Beat Callback Test")
    print("Registering GameObject: ", registerResult)

    cookie = FuncRef.new() # needs to be an instance variable
    cookie.set_instance(self) # instance in which the function should be called
    cookie.set_function("beat_callback") # name of the function

    Wwise.post_event_id_callback(event, callback_type, self, cookie)

func beat_callback(data):
    print(data)

Bank callback example:

var cookie:FuncRef 

func _ready():
    cookie = FuncRef.new()
    cookie.set_instance(self)
    cookie.set_function("on_bank_callback")
    Wwise.load_bank_async_id(AK.BANKS.INIT, cookie)
    Wwise.load_bank_async_id(AK.BANKS.TESTBANK, cookie)

func on_bank_callback(data):
    print(data)

The AkEvent node was modified to allow this new implementation to work with the existing signals present in the custom node.

jorgegarcia commented 2 years ago

I like it's less lines of code now 👍 Thank you Alessandro!