Show Changes Show Changes
Edit Edit
Print Print
Recent Changes Recent Changes
Subscriptions Subscriptions
Lost and Found Lost and Found
Find References Find References
Rename Rename
Administration Page Administration Page
Topic Locks Topic Locks

Search

History

6/30/2007 1:02:01 PM
ad-remover-84.49.122.229
5/30/2007 12:38:04 PM
-125.72.28.38
5/28/2007 6:50:31 AM
-69.136.148.27
5/20/2006 12:03:14 PM
-206.163.255.3
10/9/2005 4:16:57 PM
info@brightland.com-66.27.77.187
List all versions List all versions

RSS feed for the SquirrelWiki namespace

Passing Class Instances As User Pointers
.

Back to SqPlus

Summary

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); 

Not logged in. Log in

squirrel's community wiki

This is FlexWiki, an open source wiki engine.

Change Style

Recent Topics