Allow a callback to be set on a per-SFXSource basis...
by Giorgio Zanetti ( JoZ ) · in Torque Game Engine Advanced · 11/06/2008 (4:48 pm) · 3 replies
@ Tom Spilman:
Hi there! So, following this,
how to allow a callback to be set on a per-SFXSource basis ?
Many thanks for sharing! ;-)
JoZ
Hi there! So, following this,
how to allow a callback to be set on a per-SFXSource basis ?
Many thanks for sharing! ;-)
JoZ
About the author
Associate Tom Spilman
Sickhead Games
In SimObject.cpp:
void SimObject::setClassNamespace( const char *classNamespace ) { mClassName = StringTable->insert( classNamespace ); if ( isProperlyAdded() ) // ADDED! linkNamespaces(); // ADDED! } void SimObject::setSuperClassNamespace( const char *superClassNamespace ) { mSuperClassName = StringTable->insert( superClassNamespace ); if ( isProperlyAdded() ) // ADDED! linkNamespaces(); // ADDED! }Then in SfxSource.cpp at the bottom of the constructor:
SFXSource::SFXSource( const SFXProfile *profile ) // blah, blah, blah { // blah, blah, blah mChannel = desc->mChannel; // Allow namespace linkage. mNSLinkMask = LinkSuperClassName | LinkClassName; // ADDED! }... and...
void SFXSource::initPersistFields() { addField( "statusCallback", TypeString, Offset(mStatusCallback, SFXSource) ); // ADDED }... and ...
bool SFXSource::_setStatus( SFXStatus status ) { // blah, blah, blah // Do the callback if we have it. if ( mStatusCallback && mStatusCallback[0] ) Con::executef( mStatusCallback, getIdString(), statusString ); // CHANGED! else if ( getNamespace() ) Con::executef( this, "onStatus", statusString ); return true; }... and finally...
SFXStatus SFXSource::_updateStatus() { // If we have a voice... it has full // control over the status. if ( mVoice ) { _setStatus( mVoice->getStatus() ); // CHANGE! return mStatus; // CHANGE! }Ok so these changes allow two different ways to get source status info.
First you can assign a callback function to the source:
function MySourceStatusCallback( %source, %status ) { echo( "MySourceStatusCallback - " @ %source SPC %status ); } %source = sfxCreateSource( AudioButtonOver ); %source.statusCallback = MySourceStatusCallback; %source.play();Second you can assign namespaces via the superclass or class:
function MySource::onStatus( %this, %status ) { echo( "MySource::onStatus - " @ %this SPC %status ); } %source = sfxCreateSource( AudioButtonOver ); %source.setClassNamespace( MySource ); // or use setSuperClassNamespace %source.play();I'm not sure if the changes in SimObject::setClassNamespace() and SimObject::setSuperClassNamespace() will have any negative affects on existing code. Let me know if you see any issues there.
Hope this helps!