OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: Squirrel Scripting Language
View page
Source of version: 3
(current)
In [http://www.ogre3d.org/phpBB2/viewtopic.php?p=115508|this forum post] __morez__ posted the following helpful bit on the Squirrel Scripting Language... {maketoc} !!Intro Hi guys, In this topic I would like to explain how to set up a very simple application and use Squirrel for scripting (instead of Lua for example :D ) [http://www.squirrel-lang.org/|Squirrel] is very nice high level imperative/OO programming language with syntax similar to C/C++ developed by Alberto Demichelis. I'm going to use [http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/SqPlus.html|SQPlus], developed by John Schultz, to bind Squirrel to C++. This code example has been inspired by the ((Scripting with LuaBind in Ogre|LuaBind example in wiki page)). !!What do you need? Note that I'm using Microsoft Visual Studio .NET 2003 (7.1). You need Ogre (SDK or source) Squirrel and SQPlus of course :). SQPlus contains the last stable version of Squirrel. So, download it and compile. !!Create the Project I used the wizard to create a simple Standard Ogre Application. I called it ''SquirrelOgre''. !!Let's Include some stuff Note that this simple example is made in debug mode. So in __C++ general (Project properties) add__: * path/to/Squirrel/sqplus * path/to/Squirrel/include __In Linker general add:__ * path/to/Squirrel/lib __In Linker dependencies add:__ * squirrelD.lib * sqstdlibD.lib * sqplusD.lib __If you are running *nix__ gcc will need the following options: * -Ipath/to/Squirrel/sqplus * -Ipath/to/Squirrel/include * -Lpath/to/Squirrel/lib * -lsquirrel * -lsqstdlib * -lsqplus !!Let's code This is main.cpp. We do not change this file, I'll do everything in the header file. This is not a clear style code programming lesson :) {CODE(wrap="1", colors="c++")} #include "SquirrelOgre.h" #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN #include "windows.h" #endif #ifdef __cplusplus extern "C" { #endif #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) #else int main(int argc, char *argv[]) #endif { // Create application object SquirrelOgre app; SET_TERM_HANDLER; try { app.go(); } catch( Ogre::Exception& e ) { #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else std::cerr << "An exception has occured: " << e.getFullDescription().c_str() << std::endl; #endif } return 0; } #ifdef __cplusplus } #endif {CODE} Ok so let's see how to make our header file SquirrelOgre.h {CODE(wrap="1", colors="c++")} #ifndef __SquirrelOgre_h_ #define __SquirrelOgre_h_ #include "sqplus.h" // Include the SQPlus header #include "ExampleApplication.h" #include <stdarg.h> // needed for redirecting squirrel output using namespace SqPlus; // SqPlus namespace{CODE} Simple and clear. {CODE(wrap="1", colors="c++")} // ==================================================================== // Define a custom constructor for Vector 3 // ==================================================================== int constructVector3(int x, int y, int z, HSQUIRRELVM v) { return PostConstruct<Vector3>(v, new Vector3(x, y, z), ReleaseClassPtr<Vector3>::release); } SQ_DECLARE_RELEASE(Vector3) // This is required when using a custom constructor{CODE} Here we have declared a custom constructor for the Ogre::Vector3 class. {CODE(wrap="1", colors="c++")} // ==================================================================== // Actor class this is a cut & paste from LuaBind wiki tutorial ;) // ==================================================================== class Actor { public: Actor() {} Actor( std::string sName, SceneManager *pSceneMgr, Vector3 v3Position, Vector3 v3Scale ) { mName = sName; mSceneMgr = pSceneMgr; std::stringstream ss; ss << mName << "_ControlNode"; mControlNode = pSceneMgr->getRootSceneNode()->createChildSceneNode( ss.str(), v3Position ); ss.flush(); ss << mName << "_Entity"; mEntity = pSceneMgr->createEntity( ss.str(), "ogrehead.mesh" ); mControlNode->attachObject( mEntity ); mControlNode->setScale( v3Scale ); } ~Actor(){} void setMaterialName( const SQChar* sMaterialName ) { mEntity->setMaterialName( sMaterialName ); } void setScale( Vector3* v3Scale ) { mControlNode->setScale( *v3Scale ); } void setPosition( Vector3* v3Position ) { mControlNode->setPosition( *v3Position ); } void setMesh( const SQChar* sMeshResourceName ) { if( mEntity ) { mControlNode->detachObject( mEntity->getName() ); mSceneMgr->removeEntity( mEntity ); mEntity = 0; } mEntity = mSceneMgr->createEntity( mName, sMeshResourceName ); mControlNode->attachObject( mEntity ); } protected: std::string mName; SceneNode *mControlNode; Entity *mEntity; SceneManager *mSceneMgr; }; // Declare both Actor and Vector3 class DECLARE_INSTANCE_TYPE(Actor) DECLARE_INSTANCE_TYPE(Vector3){CODE} Here we have declared the Actor class and called the macro DECLARE_INSTANCE_TYPE for both Actor and Ogre::Vector3 classes. Note that the Actor class methods parameters are passed by reference in setScale and setPosition methods and I've used const SQChar* instead of std::string in setMaterialName and setMesh. {CODE(wrap="1", colors="c++")} class SquirrelOgre : public ExampleApplication { virtual void createScene(void) { SquirrelVM::Init(); // Start the virtual machine Ogre::LogManager::getSingletonPtr()->createLog("squirrel.log"); sq_setprintfunc(SquirrelVM::GetVMPtr(), SquirrelOgre::printFunc); // Redirect the VM output, print func is declared below // The following reads the test.nut script using Ogres Resource manager: DataStreamPtr pStream = ResourceGroupManager::getSingleton().openResource( "Test.nut", "General"); // open the script file String filestream = pStream->getAsString(); SquirrelObject testSquirrel = SquirrelVM::CompileBuffer(filestream.c_str()); // Compile the script // This is if you instead want to open the test.nut file in the current directory and avoid the Ogre resource manager: // SquirrelObject testSquirrel = SquirrelVM::CompileScript("Test.nut"); try { SquirrelVM::RunScript(testSquirrel); // run the script } catch(SquirrelError & e) { // catch exceptions and print them out through the custom print function sq_getprintfunc(SquirrelVM::GetVMPtr()) (SquirrelVM::GetVMPtr(),_T("Error: %s, %s\n"),e.desc); } // Bind the Vector3 class SQClassDef<Vector3>(_T("Vector3")). staticFunc(constructVector3,_T("constructor")). // here's our custom cunstructor var(&Vector3::x,_T("x")). var(&Vector3::y,_T("y")). var(&Vector3::z,_T("z")); // Bind the Actor class SQClassDef<Actor>(_T("Actor")). func(&Actor::setMaterialName,_T("setMaterialName")). func(&Actor::setScale,_T("setScale")). func(&Actor::setPosition,_T("setPosition")). func(&Actor::setMesh,_T("setMesh")); // Create an Actor object Actor *pActor = new Actor( "TestActor", mSceneMgr, Vector3( 0, 0, 0 ), Vector3( 1, 1, 1 ) ); // Call initActor function and pass object by reference SquirrelFunction<void>(_T("initActor"))(pActor); // Shutdown the VM when finish SquirrelVM::Shutdown(); // Delete Actor object delete pActor; // Set ambient light mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); // Create a light Light* l = mSceneMgr->createLight("MainLight"); l->setPosition(20,80,50); }{CODE} I've put everything in the createScene method. First of all we create the Virtual Machine. Then we compile and run the script "Test.nut". The Test.nut file is in a directory where the ResourceGroupManager can read it, or the same directory of the exe file. It depends on which method you use to read it. After that we bind the classes Actor and Vector3. Note how the constructor is bind. Finally we create an Actor object and the initActor (define in Test.nut) function is called. Note that pActor is passed by reference. (NOTE: You probably should have a constructor and destructor in the SquirrelOgre class.) {CODE(wrap="1", colors="c++")} static void printFunc(HSQUIRRELVM v,const SQChar * s,...) { std::string tempStr = ""; static SQChar temp[2048]; va_list vl; va_start(vl,s); scvsprintf( temp,s,vl); va_end(vl); Ogre::LogManager::getSingletonPtr()->getLog("squirrel.log")->logMessage(temp); } }; //ends the class #endif //ends #ifndef {CODE} This function must be a member of the same class as create scene above (SquirrelOgre) This function requires the stdarg.h header, which allows you to do the magic required to make a function behave like printf, that is taking many arguements relating to the prototype you also send ( something like "Days %d: Name: %s" with additional integer and string arguements). Now all the output from the VM, including exceptions and the ::print() function, is written to squirrel.log through the Ogre log manager. Finally let's see Test.nut {CODE(wrap="1", colors="c++")} // ========================================================== // Test.nut // ========================================================== function initActor( pActor ) { pActor.setMesh("robot.mesh"); pActor.setScale( Vector3(3, 3, 3) ); pActor.setPosition( Vector3( 0, 0, 0 ) ); pActor.setMaterialName( "Examples/BumpyMetal" ); } {CODE} Ok that's all. I hope it could be useful for someone. ;) !!Some Links [http://www.squirrel-lang.org|The Squirrel home page] [http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/SqPlus.html|The SQPlus home page] [http://www.ogre3d.org/phpBB2/viewtopic.php?t=15768&sid=35eb5cb2261f07d3c970155a27d1dace|The thread that started it all, with lots of useful info]
Search by Tags
Search Wiki by Freetags
Latest Changes
IDE Eclipse
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
...more
Search
Find
Advanced
Search Help
Online Users
40 online users