With the latest version of SqPlus you can pass instances of classes freely between C++ and Squirrel by value/copy or by reference/pointer (must use pointer syntax on the C++ side for function declarations). If the function to be bound uses reference syntax, the function must be cast to a version using pointer arguments:
// play(SoundEffect & se) is defined using pass value by reference. We'll cast it to pass a pointer,
// which is what the compiler does during compilation (thus the code works as intended when called back). This is required by the template system design as pass by reference behaves as pass by copy.
// SSoundSystem is created from the actual sound system and a custom sound system for script access via the binding method:
#define USE_USER_POINTERS
#ifndef USE_USER_POINTERS
typedef int (SoundSystem::*PlaySoundEffectFunc)(SoundEffect * soundEffect);
SQClassDef sSoundSystem("SoundSystem");
sSoundSystem.staticFunc(*SoundSystem::getSoundSystem(),(PlaySoundEffectFunc)SoundSystem::play,"play");
#else // If the class instance does not need to be derefenced in script, it can be passed as an SQUserPointer to improve performance or to keep the instance opaque to script:
typedef int (SoundSystem::*PlaySoundEffectFunc)(SQUserPointer soundEffect);
SQClassDef<SSoundSystem> sSoundSystem("SoundSystem");
sSoundSystem.staticFunc(*SoundSystem::getSoundSystem(),(PlaySoundEffectFunc)SoundSystem::play,"play");
#endif
SQClassDef<SGameSystem> sGameSystem("GameSystem");
#ifndef USE_USER_POINTERS // Use pointers to SoundEffect class/struct
sGameSystem.var(&SGameSystem::waitGameSoundEffect,"waitGameSoundEffect");
sGameSystem.var(&SGameSystem::startGameSoundEffect,"startGameSoundEffect");
sGameSystem.var(&SGameSystem::endGameSoundEffect,"endGameSoundEffect");
#else // Use user pointers
sGameSystem.varAsUserPointer(&SGameSystem::waitGameSoundEffect,"waitGameSoundEffect");
sGameSystem.varAsUserPointer(&SGameSystem::startGameSoundEffect,"startGameSoundEffect");
sGameSystem.varAsUserPointer(&SGameSystem::endGameSoundEffect,"endGameSoundEffect");
#endif
// === In script ===
// Create instances during init:
soundSystem <- SoundSystem();
gameSystem <- GameSystem();
// At runtime:
soundSystem.play(gameSystem.waitGameSoundEffect);