SqPlus minimal examples

Started by
10 comments, last by John Schultz 18 years, 6 months ago
A simple/minimal example was missing from SqPlus: simple examples shown below. (Updated versions with the example code and additional info here: http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/SqPlus.html. The code should compile and link with no warnings or errors with VS.NET 2003 (should also work with VS8 beta2). If a GNUMakefile is created for GCC, let me know if there are any issues with GCC builds and I'll fix them). Minimal Hello World example:

#include "sqplus.h"

using namespace SqPlus;

int main(int argc,char * argv[]) {
  SquirrelVM::Init();
  SquirrelObject helloWorld = SquirrelVM::CompileBuffer("print(\"Hello World\");");
  SquirrelVM::RunScript(helloWorld);
  SquirrelVM::Shutdown();
  return 0;
} // main


Minimal example with class binding:

#include <stdio.h>
#include "sqplus.h"

using namespace SqPlus;

class MyClass {
public:
  int classVal;
  // See examples in testSqPlus2.cpp for passing arguments to the constructor (including variable arguments).
  MyClass() : classVal(123) {}
  bool process(int iVal,const char * sVal) {
    printf("classVal: %d, iVal: %d, sVal %s\n",classVal,iVal,sVal);
    classVal += iVal;
    return iVal > 1;
  } // process
};

int main(int argc,char * argv[]) {

  SquirrelVM::Init();

  SQClassDef<MyClass>("MyClass").
    func(MyClass::process,"process").
    var(&MyClass::classVal,"classVal");

// Line continuations removed from this post to simplify formating.
  SquirrelObject helloSqPlus = SquirrelVM::CompileBuffer("
    local myClass = MyClass();
    local rVal = myClass.process(1,\"MyClass1\");
    print(\"Returned: \"+(rVal ? \"true\" : \"false\"));
    rVal = myClass.process(2,\"MyClass2\");
    print(\"Returned: \"+(rVal ? \"true\" : \"false\"));
    print(\"classVal: \"+myClass.classVal);
  ");
  SquirrelVM::RunScript(helloSqPlus);

  SquirrelVM::Shutdown();

  return 0;
} // main


This version also provides easy bindings for class/struct instance variables. See the SqPlus Wiki (link above) for more examples (including example code for using the remote debugger, more class binding examples including constants, static functions and variables, variable number of arguments, custom constructors and destructors, calling Squirrel functions from C/C++, passing class instances to/from Squirrel, etc.). [Edited by - John Schultz on October 21, 2005 1:31:04 PM]
Advertisement
Thanks, however, when I build it, I include the library folder to my library paths, and the squirrel folder and the sqplus folder to the include paths, and it still says it cant find squirrel.h, apparently this is being included in sqplus.h as if it's in the same folder ("squirrel.h"), amd I supposed to put these files in the same folder?
Quote:Original post by blankdev
Thanks, however, when I build it, I include the library folder to my library paths, and the squirrel folder and the sqplus folder to the include paths, and it still says it cant find squirrel.h, apparently this is being included in sqplus.h as if it's in the same folder ("squirrel.h"), amd I supposed to put these files in the same folder?


Load squirrel.sln
Right click on the minimalSqPlus project and select Properties
Select C/C++, then General
You'll see Additional Include Directories showing: ../include;../sqplus

Thus, for you own project, you'll need to set Additional Include Directories to: [your_location_for_squirrel_files]/include;[your_location_for_squirrel_files]/sqplus

Also be sure you have the latest version from: http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/SqPlus.html
So I compile that, and it gives me a library/libraries, link them to my project, but also include the include folder and the sqplus folder to my include paths, okay i'll try that.

EDIT: Thanks, it seems to work now. However, I've got a question. When I added the include paths, and I linked the pragmas, how does it know to use the library instead of the files that I include from the folders? If you even understand what I'm trying to say heh, if not it doesn't matter, just think it's redundant.

Thanks john for the help, you really seem to stand strong next to squirrel, as I hope I will. Hope to have a great time with squirrel, and maybe even write some tutorials.

[Edited by - blankdev on October 20, 2005 9:46:09 PM]
Quote:Original post by blankdev
EDIT: Thanks, it seems to work now. However, I've got a question. When I added the include paths, and I linked the pragmas, how does it know to use the library instead of the files that I include from the folders? If you even understand what I'm trying to say heh, if not it doesn't matter, just think it's redundant.


The include directives are for (uncompiled) header files (only), whereas the pragmas are for linking compiled libraries. You could also skip the pragmas and use the IDE project (effectively a makefile system) to link the libraries instead.

Thanks, it works now like I said, I hope to do much with it, by the way, check your PMs.
To be honest, I would prefer the minimal example if indeed it was actually minimal and not really 3 examples in one!
Quote:Original post by Kylotan
To be honest, I would prefer the minimal example if indeed it was actually minimal and not really 3 examples in one!


Done.
LOL YEAH WHAT KLYOTAN SAID! Anyways, nice job. I had to do it myself lol. Anyways, I've still been wondering what the difference between squirrel and sqplus are. I mean, in your examples, it shows no indication of what part is sqplus and squirrel. If I'm still able to bind classes and functions and variables in the normal squirrel, what makes sqplus better than that? I know it has more advanced features, but if for now all I need is this, what's the difference? Just wondering, I like the format though.

Also, how may I run a script from an external file? Such as lua has doscript or dofile (I forgot), would it be something like:

SquirrelVM::RunScript("script.squirrel");

Also, by the looks of it, I dont know if the extension for squirrel scripts is .squirrel, or could it be anything, thanks bud, just wanna get these off my chest.
Quote:Original post by blankdev
LOL YEAH WHAT KLYOTAN SAID! Anyways, nice job. I had to do it myself lol. Anyways, I've still been wondering what the difference between squirrel and sqplus are. I mean, in your examples, it shows no indication of what part is sqplus and squirrel. If I'm still able to bind classes and functions and variables in the normal squirrel, what makes sqplus better than that? I know it has more advanced features, but if for now all I need is this, what's the difference? Just wondering, I like the format though.

Also, how may I run a script from an external file? Such as lua has doscript or dofile (I forgot), would it be something like:

SquirrelVM::RunScript("script.squirrel");

Also, by the looks of it, I dont know if the extension for squirrel scripts is .squirrel, or could it be anything, thanks bud, just wanna get these off my chest.


Base/vanilla Squirrel can only bind functions with the following signature:
int func(HSQUIRRELVM v). All arguments must be manually processed from the call stack, return values must be pushed on the stack, etc.

All of the template interface code is SqPlus. See squirrel.h for the base Squirrel API, and SqPlus.h for the SqPlus template binding system.

See the Squirrels docs for sqstd_dofile() to load scripts from disk (typically named .nut (source) and .cnut (pre-compiled)).

This topic is closed to new replies.

Advertisement