Apologies for it being unclear - this the intended purpose of “user data” set on your event with Studio::EventInstance::setUserData
. For the programmer sound scripting example, it’s used to solely to pass a pointer to a key in, but since you’re passing a pointer, you can set the data being pointed to to effectively pass data between the callback and class instance, which is what it’s used for in the timeline callback scripting example. The scope of the data you set as user data can basically be anything you want - while it’s best to keep it simple, you could pass a handle to your entire ScriptReader class instance if you wanted to.
In your case, you’ll likely want to do the following:
- Create a class to hold the info you need to pass between the static callback and your non-static class instance:
class ScriptReaderInfo
{
public string key;
public bool keyExists;
public float length;
}
private ScriptReaderInfo scriptReaderInfo;
- Initialize an instance of the ScriptReaderInfo class and set a pointer to it as your event instance’s user data:
// After creating your event instance...
// Initialize class
scriptReaderInfo = new ScriptReaderInfo();
scriptReaderInfo.key = key;
scriptReaderInfo.keyExists = false;
scriptReaderInfo.length = -1;
// Pin the class in memory and pass a pointer through the user data
GCHandle structHandle = GCHandle.Alloc(scriptReaderInfo);
dialogueInstance.setUserData(GCHandle.ToIntPtr(structHandle));
- Instead of retrieving just the key in the callback, get the whole class:
// Start of callback...
FMOD.Studio.EventInstance instance = new FMOD.Studio.EventInstance(instancePtr);
// Retrieve the user data
IntPtr structPtr;
instance.getUserData(out structPtr);
// Get the string object
GCHandle structHandle = GCHandle.FromIntPtr(structPtr);
ScriptReaderInfo scriptReaderInfo = (ScriptReaderInfo)structHandle.Target;
You can then directly set scriptReaderInfo
members inside the callback and the data will be accessible outside of the callback, and vice-versa.
This is possible in a few ways. One possibility is, on recieving a specific callback type, setting a bool in the aforementioned ScriptReaderInfo class and checking it from Update(). A more direct way would be to pass a delegate as a part of your class:
public delegate void EventStoppedDelegate();
class ScriptReaderInfo
{
public string key;
public bool keyExists;
public float length;
// delegate to be called when event is stopped
public EventStoppedDelegate onEventStopped;
}
void EventStopped()
{
Debug.Log("Event stopped!");
}
scriptReaderInfo = new ScriptReaderInfo();
scriptReaderInfo.key = key;
scriptReaderInfo.keyExists = false;
scriptReaderInfo.length = -1;
scriptReaderInfo.onEventStopped = new EventStoppedDelegate(EventStopped);
Then, in your callback, you can check for a specific callback type, and call the delegate:
case FMOD.Studio.EVENT_CALLBACK_TYPE.STOPPED:
{
scriptReaderInfo.onEventStopped();
break;
}
It’s worth noting that, if you are calling a delegate from the callback, you’ll want to keep it simple since it’ll be called from the FMOD Studio thread, as has the potential to slow/block.
This is a bit of a wall of text, so please let me know if you have any questions.