• Advertisement
Sign in to follow this  

Unity SqPlus minimal examples

This topic is 4475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks, it works now like I said, I hope to do much with it, by the way, check your PMs.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)).

Share this post


Link to post
Share on other sites
woo sexy extensions, thanks bud. But then how would I know what is from squirrel and what isn't? Also, could you give an example on binding a function, variable, and class, er, scratch class off, just function or variable, sicne you've shown us the class. I dont know if it's the same way but safe to be sure.

Share this post


Link to post
Share on other sites
From testSqPlus2.cpp:

Register a function:

RegisterGlobal(getPlayerManager,"getPlayerManager");

Bind a variable:

BindVariable(&playerManager,"playerManagerVar");

The above examples register/bind to the root table (global). Additional variants bind to any table (it's also possible to bind pre-allocated class member functions).

More examples here.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By NDraskovic
      Hey guys,
      I have a really weird problem. I'm trying to get some data from a REST service. I'm using the following code:
       
      private void GetTheScores() { UnityWebRequest GetCommand = UnityWebRequest.Get(url); UnityWebRequestAsyncOperation operation = GetCommand.SendWebRequest(); if (!operation.webRequest.isNetworkError) { ResultsContainer rez = JsonUtility.FromJson<ResultsContainer>(operation.webRequest.downloadHandler.text); Debug.Log("Text: " + operation.webRequest.downloadHandler.text); } } The problem is that when I'm in Unity's editor, the request doesn't return anything (operation.webRequest.downloadHandler.text is empty, the Debug.Log command just prints "Text: "), but when I enter the debug mode and insert a breakpoint on that line, then it returns the text properly. Does anyone have an idea why is this happening?
      The real problem I'm trying to solve is that when I receive the text, I can't get the data from the JSON. The markup is really simple:
      [{"id":1,"name":"Player1"},{"id":2,"name":"Player2"}] and I have an object that should accept that data:
      [System.Serializable] public class ResultScript { public int id; public string name; } There is also a class that should accept the array of these objects (which the JSON is returning):
      [System.Serializable] public class ResultsContainer { public ResultScript[] results; } But when I run the code (in the debug mode, to get any result) I get an error: ArgumentException: JSON must represent an object type. I've googled it but none of the proposed solutions work for me.
      Also (regardless if I'm in the debug mode or not) when I try to do some string operations like removing or adding characters to the GET result, the functions return an empty string as a result
      Can you help me with any of these problems?
      Thank you
    • By nihitori
      The Emotional Music Vol. I pack focuses on beautiful and esoteric orchestral music, capable of creating truly emotive and intimate moods. It features detailed chamber strings, cello and piano as the main instruments, resulting in a subtle and elegant sound never before heard in video game royalty-free music assets.

      The pack includes 5 original tracks, as well as a total of 47 loops based on these tracks (long loops for simple use and short loops for custom / complex music layering).

      Unity Asset Store link: https://www.assetstore.unity3d.com/en/#!/content/107032
      Unreal Engine Marketplace link: https://www.unrealengine.com/marketplace/emotional-music-vol-i

      A 15 seconds preview of each main track is available on Soundcloud:
       
    • By RoKabium Games
      Another one of our new UI for #screenshotsaturday. This is the inventory screen for showing what animal fossils you have collected so far. #gamedev #indiedev #sama
    • By eldwin11929
      We're looking for programmers for our project.
      Our project is being made in Unity
      Requirements:
      -Skills in Unity
      -C#
      -Javascript
      -Node.js
      We're looking for programmers who can perform a variety of functions on our project.
      Project is a top-down hack-and-slash pvp dungeon-crawler like game. Game is entirely multiplayer based, using randomized dungeons, and a unique combat system with emphasis on gameplay.
      We have a GDD to work off of, and a Lead Programmer you would work under.
      Assignments may include:
      -Creating new scripts of varying degrees specific to the project (mostly server-side, but sometimes client-side)
      -Assembling already created monsters/characters with existing or non-existing code.
      -Creating VFX
      -Assembling already created environment models
      If interested, please contact: eldwin11929@yahoo.com
      This project is unpaid, but with royalties.
       
      ---
      Additional Project Info:
      Summary:
      Bassetune Reapers is a Player-verus-Player, competitive dungeon crawler. This basically takes on aspects of dungeon crawling, but with a more aggressive setting. Players will have the option to play as the "dungeon-crawlers" (called the 'Knights', or "Knight Class", in-game) or as the "dungeon" itself (literally called the 'Bosses', or "Boss Class", in-game). What this means is that players can choose to play as the people invading the dungeon, or as the dungeon-holders themselves.
      Key Features:
      -Intense, fast-paced combat
      -Multiple skills, weapons, and ways to play the game
      -Tons of different Bosses, Minibosses, creatures and traps to utilize throughout the dungeon
      -Multiple unique environments
      -Interesting, detailed lore behind both the game and world
      -Intricate RPG system
      -Ladder and ranking system
      -Lots of customization for both classes s of customization for both classes
    • By RoKabium Games
      Custom coffee mugs have arrived... More caffeine!
      Have a great weekend everyone! 
      #gamedev #indiedev #sama #caffeine
  • Advertisement