Game Development Community

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

#1
11/11/2008 (12:15 pm)
Ok these are the changes:

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!
#2
11/20/2008 (11:19 am)
FYI. This patch was merged into TGEA 1.8 and should be in the next beta release.
#3
11/22/2008 (1:05 pm)
Tnx Tom! Nice to have it in the base ;-)